module support
Some checks failed
Build / build (push) Successful in 10m22s
Test / build (push) Failing after 15m54s

This commit is contained in:
Chuck Smith
2024-03-26 16:49:38 -04:00
parent 6d234099d1
commit 110152a139
21 changed files with 541 additions and 100 deletions

View File

@@ -513,6 +513,16 @@ func (c *Compiler) Compile(node ast.Node) error {
afterConsequencePos := c.emit(code.OpNull)
c.changeOperand(jumpIfFalsePos, afterConsequencePos)
case *ast.ImportExpression:
c.l++
err := c.Compile(node.Name)
if err != nil {
return err
}
c.l--
c.emit(code.OpLoadModule)
}
return nil

View File

@@ -1031,6 +1031,18 @@ func TestIteration(t *testing.T) {
runCompilerTests(t, tests)
}
func TestImportExpressions(t *testing.T) {
tests := []compilerTestCase2{
{
input: `import("foo")`,
constants: []interface{}{"foo"},
instructions: "0000 OpConstant 0\n0003 OpLoadModule\n0004 OpPop\n",
},
}
runCompilerTests2(t, tests)
}
func runCompilerTests(t *testing.T, tests []compilerTestCase) {
t.Helper()

View File

@@ -19,7 +19,7 @@ type Symbol struct {
type SymbolTable struct {
Outer *SymbolTable
store map[string]Symbol
Store map[string]Symbol
numDefinitions int
FreeSymbols []Symbol
@@ -34,7 +34,7 @@ func NewEnclosedSymbolTable(outer *SymbolTable) *SymbolTable {
func NewSymbolTable() *SymbolTable {
s := make(map[string]Symbol)
free := []Symbol{}
return &SymbolTable{store: s, FreeSymbols: free}
return &SymbolTable{Store: s, FreeSymbols: free}
}
func (s *SymbolTable) Define(name string) Symbol {
@@ -45,13 +45,13 @@ func (s *SymbolTable) Define(name string) Symbol {
symbol.Scope = LocalScope
}
s.store[name] = symbol
s.Store[name] = symbol
s.numDefinitions++
return symbol
}
func (s *SymbolTable) Resolve(name string) (Symbol, bool) {
obj, ok := s.store[name]
obj, ok := s.Store[name]
if !ok && s.Outer != nil {
obj, ok = s.Outer.Resolve(name)
if !ok {
@@ -72,7 +72,7 @@ func (s *SymbolTable) Resolve(name string) (Symbol, bool) {
func (s *SymbolTable) DefineBuiltin(index int, name string) Symbol {
symbol := Symbol{Name: name, Index: index, Scope: BuiltinScope}
s.store[name] = symbol
s.Store[name] = symbol
return symbol
}
@@ -82,12 +82,12 @@ func (s *SymbolTable) DefineFree(original Symbol) Symbol {
symbol := Symbol{Name: original.Name, Index: len(s.FreeSymbols) - 1}
symbol.Scope = FreeScope
s.store[original.Name] = symbol
s.Store[original.Name] = symbol
return symbol
}
func (s *SymbolTable) DefineFunctionName(name string) Symbol {
symbol := Symbol{Name: name, Index: 0, Scope: FunctionScope}
s.store[name] = symbol
s.Store[name] = symbol
return symbol
}