diff --git a/src/backend/vm.nim b/src/backend/vm.nim index d443492..dc3a29a 100644 --- a/src/backend/vm.nim +++ b/src/backend/vm.nim @@ -26,6 +26,8 @@ import ../util/multibyte import strutils when debugVM: import std/strformat + import std/terminal + type diff --git a/src/config.nim b/src/config.nim index 27b78bf..8ac8fe9 100644 --- a/src/config.nim +++ b/src/config.nim @@ -24,7 +24,7 @@ const debugMem* {.booldefine.} = false const debugSerializer* {.booldefine.} = false const PeonBytecodeMarker* = "PEON_BYTECODE" 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".} const PeonVersion* = (major: 0, minor: 1, patch: 0) const PeonRelease* = "alpha" @@ -32,7 +32,7 @@ const PeonCommitHash* = "b273cd744883458a4a6354a0cc5f4f5d0f560c31" when len(PeonCommitHash) != 40: {.fatal: "The git commit hash must be exactly 40 characters long".} 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".} 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 diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index a025e3f..6b948ef 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -20,14 +20,14 @@ import ../util/symbols import lexer as l import parser as p -import tables -import strformat -import algorithm -import parseutils -import strutils -import sequtils -import sets -import os +import std/tables +import std/strformat +import std/algorithm +import std/parseutils +import std/strutils +import std/sequtils +import std/sets +import std/os export ast diff --git a/src/frontend/lexer.nim b/src/frontend/lexer.nim index e5a655b..6d24a46 100644 --- a/src/frontend/lexer.nim +++ b/src/frontend/lexer.nim @@ -15,10 +15,10 @@ ## A simple and modular tokenizer implementation with arbitrary lookahead ## using a customizable symbol table -import strutils -import parseutils -import strformat -import tables +import std/strutils +import std/parseutils +import std/strformat +import std/tables import meta/token diff --git a/src/frontend/meta/ast.nim b/src/frontend/meta/ast.nim index 902384f..26df8c8 100644 --- a/src/frontend/meta/ast.nim +++ b/src/frontend/meta/ast.nim @@ -16,8 +16,8 @@ ## top-down parser. For more info, check out docs/grammar.md -import strformat -import strutils +import std/strformat +import std/strutils import token diff --git a/src/frontend/meta/bytecode.nim b/src/frontend/meta/bytecode.nim index 8f87aed..c6b0d59 100644 --- a/src/frontend/meta/bytecode.nim +++ b/src/frontend/meta/bytecode.nim @@ -14,8 +14,8 @@ ## Low level bytecode implementation details -import strutils -import strformat +import std/strutils +import std/strformat import ../../util/multibyte diff --git a/src/frontend/meta/token.nim b/src/frontend/meta/token.nim index fc4ce8f..d84e32d 100644 --- a/src/frontend/meta/token.nim +++ b/src/frontend/meta/token.nim @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import strformat -import strutils +import std/strformat +import std/strutils type diff --git a/src/frontend/parser.nim b/src/frontend/parser.nim index 516a011..3339736 100644 --- a/src/frontend/parser.nim +++ b/src/frontend/parser.nim @@ -14,10 +14,10 @@ ## A recursive-descent top-down parser implementation -import strformat -import strutils -import tables -import os +import std/strformat +import std/strutils +import std/tables +import std/os import meta/token import meta/ast diff --git a/src/main.nim b/src/main.nim index 8255cf9..5e448d3 100644 --- a/src/main.nim +++ b/src/main.nim @@ -1,10 +1,10 @@ # Builtins & external libs -import strformat -import strutils -import terminal -import parseopt -import times -import os +import std/strformat +import std/strutils +import std/terminal +import std/parseopt +import std/times +import std/os # Thanks art <3 import jale/editor as ed diff --git a/src/memory/allocator.nim b/src/memory/allocator.nim index b6d93e8..f0455a5 100644 --- a/src/memory/allocator.nim +++ b/src/memory/allocator.nim @@ -15,29 +15,29 @@ ## Memory allocator from JAPL -import segfaults +import std/segfaults import ../config -when DEBUG_TRACE_ALLOCATION: - import strformat +when debugMem: + import std/strformat proc reallocate*(p: pointer, oldSize: int, newSize: int): pointer = - ## Wrapper around realloc/dealloc + ## Simple wrapper around realloc/dealloc try: if newSize == 0 and p != nil: - when DEBUG_TRACE_ALLOCATION: + when debugMem: if oldSize > 1: echo &"DEBUG - Memory manager: Deallocating {oldSize} bytes" else: echo "DEBUG - Memory manager: Deallocating 1 byte" dealloc(p) return nil - when DEBUG_TRACE_ALLOCATION: + when debugMem: if pointr == nil and newSize == 0: 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: - when DEBUG_TRACE_ALLOCATION: + when debugMem: if oldSize == 0: if newSize > 1: echo &"DEBUG - Memory manager: Allocating {newSize} bytes of memory" @@ -46,40 +46,35 @@ proc reallocate*(p: pointer, oldSize: int, newSize: int): pointer = else: echo &"DEBUG - Memory manager: Resizing {oldSize} bytes of memory to {newSize} bytes" result = realloc(p, newSize) - when DEBUG_TRACE_ALLOCATION: + when debugMem: if oldSize > 0 and pointr == nil: echo &"DEBUG - Memory manager: Warning, asked to realloc() nil pointer from {oldSize} to {newSize} bytes, ignoring request" 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 -template resizeArray*(kind: untyped, pointr: pointer, oldCount, - newCount: int): untyped = - ## Handy macro (in the C sense of macro, not nim's) to resize a dynamic array - cast[ptr UncheckedArray[kind]](reallocate(pointr, sizeof(kind) * oldCount, - sizeof(kind) * newCount)) +template resizeArray*(kind: untyped, p: pointer, oldCount, newCount: int): untyped = + ## Handy template to resize a dynamic array + cast[ptr UncheckedArray[kind]](reallocate(p, 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 - 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 ## size to 0 - reallocate(pointr, sizeof(kind), 0) + reallocate(p, sizeof(kind), 0) 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 ## dynamic arrays - if capacity < 8: - 8 - else: - capacity * ARRAY_GROW_FACTOR + if capacity < 8: 8 else: capacity * HeapGrowFactor template allocate*(castTo: untyped, sizeTo: untyped, count: int): untyped = diff --git a/src/peon.nim b/src/peon.nim deleted file mode 100644 index f8f4af4..0000000 --- a/src/peon.nim +++ /dev/null @@ -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, "
"), "
"), "
")) diff --git a/src/peon/stdlib/arithmetics.pn b/src/peon/stdlib/arithmetics.pn deleted file mode 100644 index 739c02a..0000000 --- a/src/peon/stdlib/arithmetics.pn +++ /dev/null @@ -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] -} diff --git a/src/util/debugger.nim b/src/util/debugger.nim index 63d0589..d69a5af 100644 --- a/src/util/debugger.nim +++ b/src/util/debugger.nim @@ -16,9 +16,9 @@ import ../frontend/meta/bytecode import multibyte -import strformat -import strutils -import terminal +import std/strformat +import std/strutils +import std/terminal type diff --git a/src/util/multibyte.nim b/src/util/multibyte.nim index d003a78..139a871 100644 --- a/src/util/multibyte.nim +++ b/src/util/multibyte.nim @@ -13,7 +13,6 @@ # limitations under the License. ## Utilities to handle multibyte sequences -import nimSHA2 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) -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 = ## Converts a sequence of bytes to ## a string diff --git a/src/util/serializer.nim b/src/util/serializer.nim index 74ca49d..f985e0b 100644 --- a/src/util/serializer.nim +++ b/src/util/serializer.nim @@ -18,9 +18,9 @@ import multibyte import ../config -import strformat -import strutils -import times +import std/strformat +import std/strutils +import std/times export ast