builtins
Some checks failed
Build / build (push) Failing after 1h2m55s
Test / build (push) Failing after 29m38s

This commit is contained in:
Chuck Smith
2024-03-12 16:35:24 -04:00
parent 1d2c7f0a51
commit e373e9f68a
11 changed files with 354 additions and 125 deletions

View File

@@ -34,9 +34,16 @@ func New() *Compiler {
lastInstruction: EmittedInstruction{},
previousInstruction: EmittedInstruction{},
}
symbolTable := NewSymbolTable()
for i, v := range object.Builtins {
symbolTable.DefineBuiltin(i, v.Name)
}
return &Compiler{
constants: []object.Object{},
symbolTable: NewSymbolTable(),
symbolTable: symbolTable,
scopes: []CompilationScope{mainScope},
scopeIndex: 0,
}
@@ -207,11 +214,7 @@ func (c *Compiler) Compile(node ast.Node) error {
return fmt.Errorf("undefined varible %s", node.Value)
}
if symbol.Scope == GlobalScope {
c.emit(code.OpGetGlobal, symbol.Index)
} else {
c.emit(code.OpGetLocal, symbol.Index)
}
c.loadSymbol(symbol)
case *ast.StringLiteral:
str := &object.String{Value: node.Value}
@@ -425,3 +428,14 @@ type Bytecode struct {
Instructions code.Instructions
Constants []object.Object
}
func (c *Compiler) loadSymbol(s Symbol) {
switch s.Scope {
case GlobalScope:
c.emit(code.OpGetGlobal, s.Index)
case LocalScope:
c.emit(code.OpGetLocal, s.Index)
case BuiltinScope:
c.emit(code.OpGetBuiltin, s.Index)
}
}