Bugfixes to name resolution and scopes

This commit is contained in:
Nocturn9x 2021-11-17 20:39:29 +01:00
parent 352af30bee
commit 24c7b30271
3 changed files with 19 additions and 17 deletions

View File

@ -363,7 +363,7 @@ proc declareName(self: Compiler, node: ASTNode) =
of varDecl:
var node = VarDecl(node)
if not node.isStatic:
# This emits code for dynamically-resolved variables (i.e. just globals declared as dynamic)
# This emits code for dynamically-resolved variables (i.e. globals declared as dynamic and unresolvable names)
self.emitByte(DeclareName)
self.emitBytes(self.identifierConstant(IdentExpr(node.name)))
self.names.add(Name(depth: self.scopeDepth, name: IdentExpr(node.name),
@ -423,18 +423,21 @@ proc resolveStatic(self: Compiler, name: IdentExpr, depth: int = self.scopeDepth
proc getStaticIndex(self: Compiler, name: IdentExpr): int =
## Gets the predicted stack position of the given variable
## if it is static, returns -1 if it is to be bound dynamically
for i, variable in self.staticNames:
var i: int = self.staticNames.high()
for variable in reversed(self.staticNames):
if name.name.lexeme == variable.name.name.lexeme:
return i
dec(i)
return -1
proc identifier(self: Compiler, node: IdentExpr) =
## Compiles access to identifiers
let r = self.resolveDynamic(node)
if r == nil and self.scopeDepth == 0:
let s = self.resolveStatic(node)
let d = self.resolveDynamic(node)
if s == nil and d == nil and self.scopeDepth == 0:
# Usage of undeclared globals is easy to detect at the top level
self.error(&"reference to undeclared name '{r.name.name.lexeme}'")
self.error(&"reference to undeclared name '{node.name.lexeme}'")
let index = self.getStaticIndex(node)
if index != -1:
self.emitByte(LoadNameFast) # Scoping/static resolution
@ -492,8 +495,8 @@ proc blockStmt(self: Compiler, node: BlockStmt) =
## Compiles block statements, which create a new
## local scope.
self.beginScope()
while not self.done():
self.declaration(self.step())
for decl in node.code:
self.declaration(decl)
self.endScope()

View File

@ -154,9 +154,8 @@ const simpleInstructions* = {Return, BinaryAdd, BinaryMultiply,
InPlaceFloorDiv, InPlaceMod, InPlaceMultiply,
InPlaceSubtract, BinaryFloorDiv, BinaryOf, Raise,
ReRaise, BeginTry, FinishTry, Yield, Await}
const constantInstructions* = {LoadConstant, DeclareName, LoadName, UpdateName, DeleteName,
UpdateNameFast, DeleteNameFast}
const byteInstructions* = {Call}
const constantInstructions* = {LoadConstant, DeclareName, LoadName, UpdateName, DeleteName}
const stackInstructions* = {Call, UpdateNameFast, DeleteNameFast, LoadNameFast}
const jumpInstructions* = {JumpIfFalse, Jump}
const collectionInstructions* = {BuildList, BuildDict, BuildSet, BuildTuple}

View File

@ -53,21 +53,21 @@ proc simpleInstruction(instruction: OpCode, offset: int): int =
return offset + 1
proc byteInstruction(instruction: OpCode, chunk: Chunk, offset: int): int =
var slot = chunk.code[offset + 1]
proc stackInstruction(instruction: OpCode, chunk: Chunk, offset: int): int =
var slot = [chunk.code[offset + 1], chunk.code[offset + 2], chunk.code[offset + 3]].fromTriple()
printInstruction(instruction)
stdout.write(&", points to slot ")
stdout.write(&", points to stack index ")
setForegroundColor(fgYellow)
stdout.write(&"{slot}")
nl()
return offset + 2
return offset + 4
proc constantInstruction(instruction: OpCode, chunk: Chunk, offset: int): int =
# Rebuild the index
var constant = [chunk.code[offset + 1], chunk.code[offset + 2], chunk.code[offset + 3]].fromTriple()
printInstruction(instruction)
stdout.write(&", points to slot ")
stdout.write(&", points to constant at position ")
setForegroundColor(fgYellow)
stdout.write(&"{constant}")
nl()
@ -132,8 +132,8 @@ proc disassembleInstruction*(chunk: Chunk, offset: int): int =
result = simpleInstruction(opcode, offset)
of constantInstructions:
result = constantInstruction(opcode, chunk, offset)
of byteInstructions:
result = byteInstruction(opcode, chunk, offset)
of stackInstructions:
result = stackInstruction(opcode, chunk, offset)
of jumpInstructions:
result = jumpInstruction(opcode, chunk, offset)
of collectionInstructions: