Further clean up
This commit is contained in:
parent
f7733d925f
commit
77fd5931fa
|
@ -26,6 +26,8 @@ import ../util/multibyte
|
||||||
import strutils
|
import strutils
|
||||||
when debugVM:
|
when debugVM:
|
||||||
import std/strformat
|
import std/strformat
|
||||||
|
import std/terminal
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
|
@ -24,7 +24,7 @@ const debugMem* {.booldefine.} = false
|
||||||
const debugSerializer* {.booldefine.} = false
|
const debugSerializer* {.booldefine.} = false
|
||||||
const PeonBytecodeMarker* = "PEON_BYTECODE"
|
const PeonBytecodeMarker* = "PEON_BYTECODE"
|
||||||
const HeapGrowFactor* = 2 # How much extra memory to allocate for dynamic arrays and garbage collection when resizing
|
const HeapGrowFactor* = 2 # How much extra memory to allocate for dynamic arrays and garbage collection when resizing
|
||||||
when HEAP_GROW_FACTOR <= 1:
|
when HeapGrowFactor <= 1:
|
||||||
{.fatal: "Heap growth factor must be > 1".}
|
{.fatal: "Heap growth factor must be > 1".}
|
||||||
const PeonVersion* = (major: 0, minor: 1, patch: 0)
|
const PeonVersion* = (major: 0, minor: 1, patch: 0)
|
||||||
const PeonRelease* = "alpha"
|
const PeonRelease* = "alpha"
|
||||||
|
@ -32,7 +32,7 @@ const PeonCommitHash* = "b273cd744883458a4a6354a0cc5f4f5d0f560c31"
|
||||||
when len(PeonCommitHash) != 40:
|
when len(PeonCommitHash) != 40:
|
||||||
{.fatal: "The git commit hash must be exactly 40 characters long".}
|
{.fatal: "The git commit hash must be exactly 40 characters long".}
|
||||||
const PeonBranch* = "unboxed-types"
|
const PeonBranch* = "unboxed-types"
|
||||||
when len(PEON_BRANCH) > 255:
|
when len(PeonBranch) > 255:
|
||||||
{.fatal: "The git branch name's length must be less than or equal to 255 characters".}
|
{.fatal: "The git branch name's length must be less than or equal to 255 characters".}
|
||||||
const PeonVersionString* = &"Peon {PeonVersion.major}.{PeonVersion.minor}.{PeonVersion.patch} {PeonRelease} ({PeonBranch}, {CompileDate}, {CompileTime}, {PeonCommitHash[0..8]}) [Nim {NimVersion}] on {hostOS} ({hostCPU})"
|
const PeonVersionString* = &"Peon {PeonVersion.major}.{PeonVersion.minor}.{PeonVersion.patch} {PeonRelease} ({PeonBranch}, {CompileDate}, {CompileTime}, {PeonCommitHash[0..8]}) [Nim {NimVersion}] on {hostOS} ({hostCPU})"
|
||||||
const HelpMessage* = """The peon programming language, Copyright (C) 2022 Mattia Giambirtone & All Contributors
|
const HelpMessage* = """The peon programming language, Copyright (C) 2022 Mattia Giambirtone & All Contributors
|
||||||
|
|
|
@ -20,14 +20,14 @@ import ../util/symbols
|
||||||
import lexer as l
|
import lexer as l
|
||||||
import parser as p
|
import parser as p
|
||||||
|
|
||||||
import tables
|
import std/tables
|
||||||
import strformat
|
import std/strformat
|
||||||
import algorithm
|
import std/algorithm
|
||||||
import parseutils
|
import std/parseutils
|
||||||
import strutils
|
import std/strutils
|
||||||
import sequtils
|
import std/sequtils
|
||||||
import sets
|
import std/sets
|
||||||
import os
|
import std/os
|
||||||
|
|
||||||
|
|
||||||
export ast
|
export ast
|
||||||
|
|
|
@ -15,10 +15,10 @@
|
||||||
## A simple and modular tokenizer implementation with arbitrary lookahead
|
## A simple and modular tokenizer implementation with arbitrary lookahead
|
||||||
## using a customizable symbol table
|
## using a customizable symbol table
|
||||||
|
|
||||||
import strutils
|
import std/strutils
|
||||||
import parseutils
|
import std/parseutils
|
||||||
import strformat
|
import std/strformat
|
||||||
import tables
|
import std/tables
|
||||||
|
|
||||||
|
|
||||||
import meta/token
|
import meta/token
|
||||||
|
|
|
@ -16,8 +16,8 @@
|
||||||
## top-down parser. For more info, check out docs/grammar.md
|
## top-down parser. For more info, check out docs/grammar.md
|
||||||
|
|
||||||
|
|
||||||
import strformat
|
import std/strformat
|
||||||
import strutils
|
import std/strutils
|
||||||
|
|
||||||
|
|
||||||
import token
|
import token
|
||||||
|
|
|
@ -14,8 +14,8 @@
|
||||||
|
|
||||||
## Low level bytecode implementation details
|
## Low level bytecode implementation details
|
||||||
|
|
||||||
import strutils
|
import std/strutils
|
||||||
import strformat
|
import std/strformat
|
||||||
|
|
||||||
import ../../util/multibyte
|
import ../../util/multibyte
|
||||||
|
|
||||||
|
|
|
@ -12,8 +12,8 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import strformat
|
import std/strformat
|
||||||
import strutils
|
import std/strutils
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
|
@ -14,10 +14,10 @@
|
||||||
|
|
||||||
## A recursive-descent top-down parser implementation
|
## A recursive-descent top-down parser implementation
|
||||||
|
|
||||||
import strformat
|
import std/strformat
|
||||||
import strutils
|
import std/strutils
|
||||||
import tables
|
import std/tables
|
||||||
import os
|
import std/os
|
||||||
|
|
||||||
import meta/token
|
import meta/token
|
||||||
import meta/ast
|
import meta/ast
|
||||||
|
|
12
src/main.nim
12
src/main.nim
|
@ -1,10 +1,10 @@
|
||||||
# Builtins & external libs
|
# Builtins & external libs
|
||||||
import strformat
|
import std/strformat
|
||||||
import strutils
|
import std/strutils
|
||||||
import terminal
|
import std/terminal
|
||||||
import parseopt
|
import std/parseopt
|
||||||
import times
|
import std/times
|
||||||
import os
|
import std/os
|
||||||
|
|
||||||
# Thanks art <3
|
# Thanks art <3
|
||||||
import jale/editor as ed
|
import jale/editor as ed
|
||||||
|
|
|
@ -15,29 +15,29 @@
|
||||||
## Memory allocator from JAPL
|
## Memory allocator from JAPL
|
||||||
|
|
||||||
|
|
||||||
import segfaults
|
import std/segfaults
|
||||||
import ../config
|
import ../config
|
||||||
|
|
||||||
when DEBUG_TRACE_ALLOCATION:
|
when debugMem:
|
||||||
import strformat
|
import std/strformat
|
||||||
|
|
||||||
|
|
||||||
proc reallocate*(p: pointer, oldSize: int, newSize: int): pointer =
|
proc reallocate*(p: pointer, oldSize: int, newSize: int): pointer =
|
||||||
## Wrapper around realloc/dealloc
|
## Simple wrapper around realloc/dealloc
|
||||||
try:
|
try:
|
||||||
if newSize == 0 and p != nil:
|
if newSize == 0 and p != nil:
|
||||||
when DEBUG_TRACE_ALLOCATION:
|
when debugMem:
|
||||||
if oldSize > 1:
|
if oldSize > 1:
|
||||||
echo &"DEBUG - Memory manager: Deallocating {oldSize} bytes"
|
echo &"DEBUG - Memory manager: Deallocating {oldSize} bytes"
|
||||||
else:
|
else:
|
||||||
echo "DEBUG - Memory manager: Deallocating 1 byte"
|
echo "DEBUG - Memory manager: Deallocating 1 byte"
|
||||||
dealloc(p)
|
dealloc(p)
|
||||||
return nil
|
return nil
|
||||||
when DEBUG_TRACE_ALLOCATION:
|
when debugMem:
|
||||||
if pointr == nil and newSize == 0:
|
if pointr == nil and newSize == 0:
|
||||||
echo &"DEBUG - Memory manager: Warning, asked to dealloc() nil pointer from {oldSize} to {newSize} bytes, ignoring request"
|
echo &"DEBUG - Memory manager: Warning, asked to dealloc() nil pointer from {oldSize} to {newSize} bytes, ignoring request"
|
||||||
if oldSize > 0 and p != nil or oldSize == 0:
|
if oldSize > 0 and p != nil or oldSize == 0:
|
||||||
when DEBUG_TRACE_ALLOCATION:
|
when debugMem:
|
||||||
if oldSize == 0:
|
if oldSize == 0:
|
||||||
if newSize > 1:
|
if newSize > 1:
|
||||||
echo &"DEBUG - Memory manager: Allocating {newSize} bytes of memory"
|
echo &"DEBUG - Memory manager: Allocating {newSize} bytes of memory"
|
||||||
|
@ -46,40 +46,35 @@ proc reallocate*(p: pointer, oldSize: int, newSize: int): pointer =
|
||||||
else:
|
else:
|
||||||
echo &"DEBUG - Memory manager: Resizing {oldSize} bytes of memory to {newSize} bytes"
|
echo &"DEBUG - Memory manager: Resizing {oldSize} bytes of memory to {newSize} bytes"
|
||||||
result = realloc(p, newSize)
|
result = realloc(p, newSize)
|
||||||
when DEBUG_TRACE_ALLOCATION:
|
when debugMem:
|
||||||
if oldSize > 0 and pointr == nil:
|
if oldSize > 0 and pointr == nil:
|
||||||
echo &"DEBUG - Memory manager: Warning, asked to realloc() nil pointer from {oldSize} to {newSize} bytes, ignoring request"
|
echo &"DEBUG - Memory manager: Warning, asked to realloc() nil pointer from {oldSize} to {newSize} bytes, ignoring request"
|
||||||
except NilAccessDefect:
|
except NilAccessDefect:
|
||||||
stderr.write("JAPL: could not manage memory, segmentation fault\n")
|
stderr.write("Peon: could not manage memory, segmentation fault\n")
|
||||||
quit(139) # For now, there's not much we can do if we can't get the memory we need, so we exit
|
quit(139) # For now, there's not much we can do if we can't get the memory we need, so we exit
|
||||||
|
|
||||||
|
|
||||||
template resizeArray*(kind: untyped, pointr: pointer, oldCount,
|
template resizeArray*(kind: untyped, p: pointer, oldCount, newCount: int): untyped =
|
||||||
newCount: int): untyped =
|
## Handy template to resize a dynamic array
|
||||||
## Handy macro (in the C sense of macro, not nim's) to resize a dynamic array
|
cast[ptr UncheckedArray[kind]](reallocate(p, sizeof(kind) * oldCount, sizeof(kind) * newCount))
|
||||||
cast[ptr UncheckedArray[kind]](reallocate(pointr, sizeof(kind) * oldCount,
|
|
||||||
sizeof(kind) * newCount))
|
|
||||||
|
|
||||||
|
|
||||||
template freeArray*(kind: untyped, pointr: pointer, oldCount: int): untyped =
|
template freeArray*(kind: untyped, p: pointer, oldCount: int): untyped =
|
||||||
## Frees a dynamic array
|
## Frees a dynamic array
|
||||||
reallocate(pointr, sizeof(kind) * oldCount, 0)
|
reallocate(p, sizeof(kind) * oldCount, 0)
|
||||||
|
|
||||||
|
|
||||||
template free*(kind: untyped, pointr: pointer): untyped =
|
template free*(kind: untyped, p: pointer): untyped =
|
||||||
## Frees a pointer by reallocating its
|
## Frees a pointer by reallocating its
|
||||||
## size to 0
|
## size to 0
|
||||||
reallocate(pointr, sizeof(kind), 0)
|
reallocate(p, sizeof(kind), 0)
|
||||||
|
|
||||||
|
|
||||||
template growCapacity*(capacity: int): untyped =
|
template growCapacity*(capacity: int): untyped =
|
||||||
## Handy macro used to calculate how much
|
## Handy template used to calculate how much
|
||||||
## more memory is needed when reallocating
|
## more memory is needed when reallocating
|
||||||
## dynamic arrays
|
## dynamic arrays
|
||||||
if capacity < 8:
|
if capacity < 8: 8 else: capacity * HeapGrowFactor
|
||||||
8
|
|
||||||
else:
|
|
||||||
capacity * ARRAY_GROW_FACTOR
|
|
||||||
|
|
||||||
|
|
||||||
template allocate*(castTo: untyped, sizeTo: untyped, count: int): untyped =
|
template allocate*(castTo: untyped, sizeTo: untyped, count: int): untyped =
|
||||||
|
|
11
src/peon.nim
11
src/peon.nim
|
@ -1,11 +0,0 @@
|
||||||
import backend/vm
|
|
||||||
import frontend/lexer
|
|
||||||
import frontend/parser
|
|
||||||
import frontend/compiler
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
proc runPeon*(s: string) =
|
|
||||||
## Executes a string containing
|
|
||||||
## peon code
|
|
||||||
newPeonVM().run(newCompiler().compile(newParser().parse(newLexer().lex(s, "<main>"), "<main>"), "<main>"))
|
|
|
@ -1,207 +0,0 @@
|
||||||
## Builtin arithmetic operators for Peon
|
|
||||||
|
|
||||||
# Note: These do nothing on their own. All they do
|
|
||||||
# is serve as placeholders for emitting specific VM
|
|
||||||
# instructions. They're implemented this way because:
|
|
||||||
# - They tie into the existing type system nicely
|
|
||||||
# - It makes the implementation easier and more flexible
|
|
||||||
|
|
||||||
|
|
||||||
operator `+`(a, b: int): int {
|
|
||||||
#pragma[magic: "AddInt64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `+`(a, b: uint64): uint64 {
|
|
||||||
#pragma[magic: "AddUInt64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `+`(a, b: int32): int32 {
|
|
||||||
#pragma[magic: "AddInt32", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `+`(a, b: uint32): uint32 {
|
|
||||||
#pragma[magic: "AddUInt32", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `+`(a, b: int16): int16 {
|
|
||||||
#pragma[magic: "AddInt16", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `+`(a, b: uint16): uint16 {
|
|
||||||
#pragma[magic: "AddUInt16", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `+`(a, b: int8): int8 {
|
|
||||||
#pragma[magic: "AddInt8", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `+`(a, b: uint8): uint8 {
|
|
||||||
#pragma[magic: "AddUInt8", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `+`(a, b: float64): float64 {
|
|
||||||
#pragma[magic: "AddFloat64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `+`(a, b: float32): float32 {
|
|
||||||
#pragma[magic: "AddFloat32", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `-`(a, b: int): int {
|
|
||||||
#pragma[magic: "SubInt64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `-`(a, b: uint64): uint64 {
|
|
||||||
#pragma[magic: "SubUInt64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `-`(a, b: int32): int32 {
|
|
||||||
#pragma[magic: "SubInt32", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `-`(a, b: uint32): uint32 {
|
|
||||||
#pragma[magic: "SubUInt32", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `-`(a, b: int16): int16 {
|
|
||||||
#pragma[magic: "SubInt16", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `-`(a, b: uint16): uint16 {
|
|
||||||
#pragma[magic: "SubUInt16", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `-`(a, b: int8): int8 {
|
|
||||||
#pragma[magic: "SubInt8", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `-`(a, b: uint8): uint8 {
|
|
||||||
#pragma[magic: "SubUInt8", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `-`(a, b: float64): float64 {
|
|
||||||
#pragma[magic: "SubFloat64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `-`(a, b: float32): float32 {
|
|
||||||
#pragma[magic: "SubFloat32", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `*`(a, b: int): int {
|
|
||||||
#pragma[magic: "MulInt64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `*`(a, b: uint64): uint64 {
|
|
||||||
#pragma[magic: "MulUInt64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `*`(a, b: int32): int32 {
|
|
||||||
#pragma[magic: "MulInt32", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `*`(a, b: uint32): uint32 {
|
|
||||||
#pragma[magic: "MulUInt32", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `*`(a, b: int16): int16 {
|
|
||||||
#pragma[magic: "MulInt16", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `*`(a, b: uint16): uint16 {
|
|
||||||
#pragma[magic: "MulUInt16", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `*`(a, b: int8): int8 {
|
|
||||||
#pragma[magic: "MulInt8", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `*`(a, b: uint8): uint8 {
|
|
||||||
#pragma[magic: "MulUInt8", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `*`(a, b: float64): float64 {
|
|
||||||
#pragma[magic: "MulFloat64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `*`(a, b: float32): float32 {
|
|
||||||
#pragma[magic: "MulFloat32", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `/`(a, b: int): int {
|
|
||||||
#pragma[magic: "DivInt64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `/`(a, b: uint64): uint64 {
|
|
||||||
#pragma[magic: "DivUInt64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `/`(a, b: int32): int32 {
|
|
||||||
#pragma[magic: "DivInt32", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `/`(a, b: uint32): uint32 {
|
|
||||||
#pragma[magic: "DivUInt32", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `/`(a, b: int16): int16 {
|
|
||||||
#pragma[magic: "DivInt16", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `/`(a, b: uint16): uint16 {
|
|
||||||
#pragma[magic: "DivUInt16", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `/`(a, b: int8): int8 {
|
|
||||||
#pragma[magic: "DivInt8", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `/`(a, b: uint8): uint8 {
|
|
||||||
#pragma[magic: "DivUInt8", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `/`(a, b: float64): float64 {
|
|
||||||
#pragma[magic: "DivFloat64", pure]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
operator `/`(a, b: float32): float32 {
|
|
||||||
#pragma[magic: "DivFloat32", pure]
|
|
||||||
}
|
|
|
@ -16,9 +16,9 @@ import ../frontend/meta/bytecode
|
||||||
import multibyte
|
import multibyte
|
||||||
|
|
||||||
|
|
||||||
import strformat
|
import std/strformat
|
||||||
import strutils
|
import std/strutils
|
||||||
import terminal
|
import std/terminal
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
## Utilities to handle multibyte sequences
|
## Utilities to handle multibyte sequences
|
||||||
import nimSHA2
|
|
||||||
|
|
||||||
|
|
||||||
proc toDouble*(input: int | uint | uint16): array[2, uint8] =
|
proc toDouble*(input: int | uint | uint16): array[2, uint8] =
|
||||||
|
@ -73,13 +72,6 @@ proc toBytes*(s: int): array[8, uint8] =
|
||||||
result = cast[array[8, uint8]](s)
|
result = cast[array[8, uint8]](s)
|
||||||
|
|
||||||
|
|
||||||
proc toBytes*(d: SHA256Digest): seq[byte] =
|
|
||||||
## Converts a SHA256 hash digest to
|
|
||||||
## a sequence of bytes
|
|
||||||
for b in d:
|
|
||||||
result.add(b)
|
|
||||||
|
|
||||||
|
|
||||||
proc fromBytes*(input: seq[byte]): string =
|
proc fromBytes*(input: seq[byte]): string =
|
||||||
## Converts a sequence of bytes to
|
## Converts a sequence of bytes to
|
||||||
## a string
|
## a string
|
||||||
|
|
|
@ -18,9 +18,9 @@ import multibyte
|
||||||
import ../config
|
import ../config
|
||||||
|
|
||||||
|
|
||||||
import strformat
|
import std/strformat
|
||||||
import strutils
|
import std/strutils
|
||||||
import times
|
import std/times
|
||||||
|
|
||||||
|
|
||||||
export ast
|
export ast
|
||||||
|
|
Loading…
Reference in New Issue