Fixed issues when debugSerializer == true

This commit is contained in:
Mattia Giambirtone 2022-07-10 16:31:30 +02:00
parent 9cedc72f68
commit 60028ed664
3 changed files with 25 additions and 17 deletions

View File

@ -136,6 +136,7 @@ type
IdentExpr* = ref object of Expression
name*: Token
depth*: int
GroupingExpr* = ref object of Expression
expression*: Expression
@ -181,6 +182,7 @@ type
isPure*: bool
returnType*: Expression
hasExplicitReturn*: bool
freeVars*: seq[IdentExpr]
SliceExpr* = ref object of Expression
@ -262,6 +264,7 @@ type
isPure*: bool
returnType*: Expression
hasExplicitReturn*: bool
freeVars*: seq[IdentExpr]
TypeDecl* = ref object of Declaration
name*: IdentExpr
@ -395,10 +398,11 @@ proc newCharExpr*(literal: Token): CharExpr =
result.token = literal
proc newIdentExpr*(name: Token): IdentExpr =
proc newIdentExpr*(name: Token, depth: int = 0): IdentExpr =
result = IdentExpr(kind: identExpr)
result.name = name
result.token = name
result.depth = depth
proc newGroupingExpr*(expression: Expression, token: Token): GroupingExpr =
@ -410,7 +414,7 @@ proc newGroupingExpr*(expression: Expression, token: Token): GroupingExpr =
proc newLambdaExpr*(arguments: seq[tuple[name: IdentExpr, valueType: Expression]],
defaults: seq[Expression], body: Statement, isGenerator: bool,
isAsync: bool, token: Token, returnType: Expression, pragmas: seq[Pragma],
generics: seq[tuple[name: IdentExpr, cond: Expression]]): LambdaExpr =
generics: seq[tuple[name: IdentExpr, cond: Expression]], freeVars: seq[IdentExpr] = @[]): LambdaExpr =
result = LambdaExpr(kind: lambdaExpr)
result.body = body
result.arguments = arguments
@ -422,6 +426,7 @@ proc newLambdaExpr*(arguments: seq[tuple[name: IdentExpr, valueType: Expression]
result.isPure = false
result.pragmas = pragmas
result.generics = generics
result.freeVars = freeVars
proc newGetItemExpr*(obj: Expression, name: IdentExpr,
@ -611,7 +616,7 @@ proc newVarDecl*(name: IdentExpr, value: Expression, isConst: bool = false,
proc newFunDecl*(name: IdentExpr, arguments: seq[tuple[name: IdentExpr, valueType: Expression]], defaults: seq[Expression],
body: Statement, isAsync, isGenerator: bool,
isPrivate: bool, token: Token, pragmas: seq[Pragma],
returnType: Expression, generics: seq[tuple[name: IdentExpr, cond: Expression]]): FunDecl =
returnType: Expression, generics: seq[tuple[name: IdentExpr, cond: Expression]], freeVars: seq[IdentExpr] = @[]): FunDecl =
result = FunDecl(kind: funDecl)
result.name = name
result.arguments = arguments
@ -625,6 +630,7 @@ proc newFunDecl*(name: IdentExpr, arguments: seq[tuple[name: IdentExpr, valueTyp
result.returnType = returnType
result.isPure = false
result.generics = generics
result.freeVars = freeVars
proc newTypeDecl*(name: IdentExpr, fields: seq[tuple[name: IdentExpr, valueType: Expression, isPrivate: bool]],

View File

@ -307,7 +307,7 @@ proc primary(self: Parser): Expression =
of Integer:
result = newIntExpr(self.step())
of Identifier:
result = newIdentExpr(self.step())
result = newIdentExpr(self.step(), self.scopeDepth)
of LeftParen:
let tok = self.step()
result = newGroupingExpr(self.expression(), tok)
@ -417,7 +417,7 @@ proc call(self: Parser): Expression =
result = self.makeCall(result)
elif self.match(Dot):
self.expect(Identifier, "expecting attribute name after '.'")
result = newGetItemExpr(result, newIdentExpr(self.peek(-1)), self.peek(-1))
result = newGetItemExpr(result, newIdentExpr(self.peek(-1), self.scopeDepth), self.peek(-1))
elif self.match(LeftBracket):
self.parseGenericArgs() # TODO
result = self.makeCall(result)
@ -647,7 +647,7 @@ proc forEachStmt(self: Parser): Statement =
self.currentLoop = Loop
self.expect(LeftParen, "expecting '(' after 'foreach'")
self.expect(Identifier)
var identifier = newIdentExpr(self.peek(-1))
var identifier = newIdentExpr(self.peek(-1), self.scopeDepth)
self.expect(":")
var expression = self.expression()
self.expect(RightParen)
@ -665,7 +665,7 @@ proc importStmt(self: Parser, fromStmt: bool = false): Statement =
tok = self.peek(-1)
# TODO: New AST node
self.expect(Identifier, "expecting module name(s) after import statement")
result = newImportStmt(newIdentExpr(self.peek(-1)), tok)
result = newImportStmt(newIdentExpr(self.peek(-1), self.scopeDepth), tok)
endOfLine("missing semicolon after import statement")
@ -803,7 +803,7 @@ proc parsePragmas(self: Parser): seq[Pragma] =
if self.peek(-1).lexeme in names:
self.error("duplicate pragmas are not allowed")
names.add(self.peek(-1).lexeme)
name = newIdentExpr(self.peek(-1))
name = newIdentExpr(self.peek(-1), self.scopeDepth)
if not self.match(":"):
if self.match("]"):
result.add(newPragma(name, @[]))
@ -833,7 +833,7 @@ proc varDecl(self: Parser, isLet: bool = false,
var tok = self.peek(-1)
var value: Expression
self.expect(Identifier, &"expecting identifier after '{tok.lexeme}'")
var name = newIdentExpr(self.peek(-1))
var name = newIdentExpr(self.peek(-1), self.scopeDepth)
let isPrivate = not self.match("*")
self.checkDecl(isPrivate)
var valueType: IdentExpr
@ -844,7 +844,7 @@ proc varDecl(self: Parser, isLet: bool = false,
# the compiler may be able to infer
# the type later!
self.expect(Identifier, "expecting type name after ':'")
valueType = newIdentExpr(self.peek(-1))
valueType = newIdentExpr(self.peek(-1), self.scopeDepth)
if self.match("="):
hasInit = true
value = self.expression()
@ -883,7 +883,7 @@ proc parseDeclArguments(self: Parser, arguments: var seq[tuple[name: IdentExpr,
if arguments.len > 255:
self.error("cannot have more than 255 arguments in function declaration")
self.expect(Identifier, "expecting parameter name")
parameter.name = newIdentExpr(self.peek(-1))
parameter.name = newIdentExpr(self.peek(-1), self.scopeDepth)
if self.match(":"):
parameter.valueType = self.expression()
for i in countdown(arguments.high(), 0):
@ -931,7 +931,7 @@ proc parseGenerics(self: Parser, decl: Declaration) =
var gen: tuple[name: IdentExpr, cond: Expression]
while not self.check(RightBracket) and not self.done():
self.expect(Identifier, "expecting generic type name")
gen.name = newIdentExpr(self.peek(-1))
gen.name = newIdentExpr(self.peek(-1), self.scopeDepth)
if self.match(":"):
gen.cond = self.expression()
decl.generics.add(gen)
@ -957,7 +957,7 @@ proc funDecl(self: Parser, isAsync: bool = false, isGenerator: bool = false,
# or an expression. Fortunately anonymous functions
# are nameless, so we can sort the ambiguity by checking
# if there's an identifier after the keyword
self.currentFunction = newFunDecl(newIdentExpr(self.peek(-1)), arguments, defaults, newBlockStmt(@[], Token()),
self.currentFunction = newFunDecl(newIdentExpr(self.peek(-1), self.scopeDepth), arguments, defaults, newBlockStmt(@[], Token()),
isAsync=isAsync,
isGenerator=isGenerator,
isPrivate=true,
@ -1121,7 +1121,7 @@ proc typeDecl(self: Parser): TypeDecl =
self.expect(Identifier, "expecting type name after 'type'")
let isPrivate = not self.match("*")
self.checkDecl(isPrivate)
var name = newIdentExpr(self.peek(-1))
var name = newIdentExpr(self.peek(-1), self.scopeDepth)
var fields: seq[tuple[name: IdentExpr, valueType: Expression, isPrivate: bool]] = @[]
var defaults: seq[Expression] = @[]
var generics: seq[tuple[name: IdentExpr, cond: Expression]] = @[]
@ -1141,7 +1141,7 @@ proc typeDecl(self: Parser): TypeDecl =
argType: Expression
while not self.match(RightBrace) and not self.done():
self.expect(Identifier, "expecting field name")
argName = newIdentExpr(self.peek(-1))
argName = newIdentExpr(self.peek(-1), self.scopeDepth)
argPrivate = not self.match("*")
self.expect(":", "expecting ':' after field name")
argType = self.expression()

View File

@ -35,6 +35,10 @@ const debugSerializer {.booldefine.} = false
const debugRuntime {.booldefine.} = false
when debugSerializer:
import nimSHA2
proc repl(vm: PeonVM = newPeonVM()) =
styledEcho fgMagenta, "Welcome into the peon REPL!"
var
@ -234,9 +238,7 @@ proc runFile(f: string, interactive: bool = false, fromString: bool = false) =
serializer.dumpFile(compiled, f, path)
serialized = serializer.loadFile(path)
when debugSerializer:
var hashMatches = computeSHA256(input).toHex().toLowerAscii() == serialized.fileHash
styledEcho fgCyan, "Serialization step: "
styledEcho fgBlue, &"\t- File hash: ", fgYellow, serialized.fileHash, fgBlue, " (", if hashMatches: fgGreen else: fgRed, if hashMatches: "OK" else: "Fail", fgBlue, ")"
styledEcho fgBlue, "\t- Peon version: ", fgYellow, &"{serialized.version.major}.{serialized.version.minor}.{serialized.version.patch}", fgBlue, " (commit ", fgYellow, serialized.commit[0..8], fgBlue, ") on branch ", fgYellow, serialized.branch
stdout.styledWriteLine(fgBlue, "\t- Compilation date & time: ", fgYellow, fromUnix(serialized.compileDate).format("d/M/yyyy HH:mm:ss"))
stdout.styledWrite(fgBlue, &"\t- Constants segment: ")