bind expression (:=) instead of let
This commit is contained in:
@@ -292,23 +292,36 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
return fmt.Errorf("expected identifier or index expression got=%s", node.Left)
|
||||
}
|
||||
|
||||
case *ast.LetStatement:
|
||||
symbol, ok := c.symbolTable.Resolve(node.Name.Value)
|
||||
if !ok {
|
||||
symbol = c.symbolTable.Define(node.Name.Value)
|
||||
}
|
||||
case *ast.BindExpression:
|
||||
var symbol Symbol
|
||||
|
||||
c.l++
|
||||
err := c.Compile(node.Value)
|
||||
c.l--
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ident, ok := node.Left.(*ast.Identifier); ok {
|
||||
symbol, ok = c.symbolTable.Resolve(ident.Value)
|
||||
if !ok {
|
||||
symbol = c.symbolTable.Define(ident.Value)
|
||||
} else {
|
||||
// Local shadowing of previously defined "free" variable in a
|
||||
// function now being rebound to a locally scoped variable.
|
||||
if symbol.Scope == FreeScope {
|
||||
symbol = c.symbolTable.Define(ident.Value)
|
||||
}
|
||||
}
|
||||
|
||||
c.l++
|
||||
err := c.Compile(node.Value)
|
||||
c.l--
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if symbol.Scope == GlobalScope {
|
||||
c.emit(code.OpSetGlobal, symbol.Index)
|
||||
} else {
|
||||
c.emit(code.OpSetLocal, symbol.Index)
|
||||
}
|
||||
|
||||
if symbol.Scope == GlobalScope {
|
||||
c.emit(code.OpSetGlobal, symbol.Index)
|
||||
} else {
|
||||
c.emit(code.OpSetLocal, symbol.Index)
|
||||
return fmt.Errorf("expected identifier got=%s", node.Left)
|
||||
}
|
||||
|
||||
case *ast.Identifier:
|
||||
|
||||
Reference in New Issue
Block a user