Extra Features
Some checks failed
Build / build (push) Failing after 1m40s
Test / build (push) Failing after 11m47s

This commit is contained in:
Chuck Smith
2024-03-14 21:25:47 -04:00
parent 36f04713bd
commit 997f0865f4
20 changed files with 757 additions and 128 deletions

View File

@@ -75,7 +75,10 @@ func (c *Compiler) Compile(node ast.Node) error {
if err != nil {
return err
}
c.emit(code.OpPop)
if !c.lastInstructionIs(code.OpNoop) {
c.emit(code.OpPop)
}
case *ast.InfixExpression:
if node.Operator == "<" {
@@ -196,7 +199,11 @@ func (c *Compiler) Compile(node ast.Node) error {
}
case *ast.LetStatement:
symbol := c.symbolTable.Define(node.Name.Value)
symbol, ok := c.symbolTable.Resolve(node.Name.Value)
if !ok {
symbol = c.symbolTable.Define(node.Name.Value)
}
err := c.Compile(node.Value)
if err != nil {
return err
@@ -328,6 +335,27 @@ func (c *Compiler) Compile(node ast.Node) error {
c.emit(code.OpCall, len(node.Arguments))
case *ast.WhileExpression:
jumpConditionPos := len(c.currentInstructions())
err := c.Compile(node.Condition)
if err != nil {
return err
}
// Emit an `OpJump`with a bogus value
jumpIfFalsePos := c.emit(code.OpJumpNotTruthy, 0xFFFF)
err = c.Compile(node.Consequence)
if err != nil {
return err
}
c.emit(code.OpJump, jumpConditionPos)
afterConsequencePos := c.emit(code.OpNoop)
c.changeOperand(jumpIfFalsePos, afterConsequencePos)
}
return nil