Extra Features
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user