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

View File

@@ -836,6 +836,24 @@ func TestLetStatementScopes(t *testing.T) {
code.Make(code.OpPop),
},
},
{
input: `
let a = 0;
let a = a + 1;
`,
expectedConstants: []interface{}{
0,
1,
},
expectedInstructions: []code.Instructions{
code.Make(code.OpConstant, 0),
code.Make(code.OpSetGlobal, 0),
code.Make(code.OpGetGlobal, 0),
code.Make(code.OpConstant, 1),
code.Make(code.OpAdd),
code.Make(code.OpSetGlobal, 0),
},
},
}
runCompilerTests(t, tests)
@@ -854,7 +872,7 @@ func TestBuiltins(t *testing.T) {
code.Make(code.OpArray, 0),
code.Make(code.OpCall, 1),
code.Make(code.OpPop),
code.Make(code.OpGetBuiltin, 5),
code.Make(code.OpGetBuiltin, 6),
code.Make(code.OpArray, 0),
code.Make(code.OpConstant, 0),
code.Make(code.OpCall, 2),
@@ -950,6 +968,33 @@ func TestRecursiveFunctions(t *testing.T) {
runCompilerTests(t, tests)
}
func TestIteration(t *testing.T) {
tests := []compilerTestCase{
{
input: `
while (true) { 10 };
`,
expectedConstants: []interface{}{10},
expectedInstructions: []code.Instructions{
// 0000
code.Make(code.OpTrue),
// 0001
code.Make(code.OpJumpNotTruthy, 11),
// 0004
code.Make(code.OpConstant, 0),
// 0007
code.Make(code.OpPop),
// 0008
code.Make(code.OpJump, 0),
// 0011
code.Make(code.OpNoop),
},
},
}
runCompilerTests(t, tests)
}
func runCompilerTests(t *testing.T, tests []compilerTestCase) {
t.Helper()