Further clean up

This commit is contained in:
Mattia Giambirtone 2022-08-17 19:31:27 +02:00
parent f7733d925f
commit 77fd5931fa
15 changed files with 56 additions and 285 deletions

View File

@ -26,6 +26,8 @@ import ../util/multibyte
import strutils
when debugVM:
import std/strformat
import std/terminal
type

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -14,8 +14,8 @@
## Low level bytecode implementation details
import strutils
import strformat
import std/strutils
import std/strformat
import ../../util/multibyte

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 =

View File

@ -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>"))

View File

@ -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]
}

View File

@ -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

View File

@ -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

View File

@ -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