assert and test changes
Some checks failed
Build / build (push) Successful in 1m18s
Test / build (push) Failing after 11m19s

This commit is contained in:
Chuck Smith
2024-03-19 08:15:11 -04:00
parent 0b1ed43ae5
commit 60d27f09d7
7 changed files with 164 additions and 142 deletions

View File

@@ -91,9 +91,7 @@ func (c *Compiler) Compile(node ast.Node) error {
return err
}
if !c.lastInstructionIs(code.OpNoop) {
c.emit(code.OpPop)
}
c.emit(code.OpPop)
case *ast.InfixExpression:
if node.Operator == "<" || node.Operator == "<=" {
@@ -182,9 +180,6 @@ func (c *Compiler) Compile(node ast.Node) error {
}
case *ast.IfExpression:
if c.lastInstructionIs(code.OpPop) {
c.removeLastPop()
}
c.l++
err := c.Compile(node.Condition)
c.l--
@@ -240,6 +235,14 @@ func (c *Compiler) Compile(node ast.Node) error {
}
c.l--
if c.lastInstructionIs(code.OpPop) {
c.removeLastPop()
} else {
if !c.lastInstructionIs(code.OpReturn) {
c.emit(code.OpNull)
}
}
case *ast.AssignmentStatement:
symbol, ok := c.symbolTable.Resolve(node.Name.Value)
if !ok {
@@ -366,8 +369,13 @@ func (c *Compiler) Compile(node ast.Node) error {
if c.lastInstructionIs(code.OpPop) {
c.replaceLastPopWithReturn()
}
// If the function doesn't end with a return statement add one with a
// `return null;` and also handle the edge-case of empty functions.
if !c.lastInstructionIs(code.OpReturn) {
c.emit(code.OpNull)
if !c.lastInstructionIs(code.OpNull) {
c.emit(code.OpNull)
}
c.emit(code.OpReturn)
}