bind expression (:=) instead of let
Some checks failed
Build / build (push) Successful in 10m26s
Test / build (push) Failing after 16m44s

This commit is contained in:
Chuck Smith
2024-03-21 17:43:03 -04:00
parent 66d5453ecc
commit 6282075e66
36 changed files with 425 additions and 583 deletions

View File

@@ -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: