Skip to content

Commit

Permalink
Add support for import aliasing in interpreter.
Browse files Browse the repository at this point in the history
  • Loading branch information
RZhang05 committed Dec 16, 2024
1 parent efc00c0 commit e430f1b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion interpreter/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func TestInterpretImportWithAlias(t *testing.T) {
import a as a2 from 0x2
access(all) fun test(): Int {
return a() + a()
return a1() + a2()
}
`,
ParseAndCheckOptions{
Expand Down
13 changes: 10 additions & 3 deletions interpreter/interpreter_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func (interpreter *Interpreter) VisitImportDeclaration(declaration *ast.ImportDe
resolvedLocations := interpreter.Program.Elaboration.ImportDeclarationsResolvedLocations(declaration)

for _, resolvedLocation := range resolvedLocations {
interpreter.importResolvedLocation(resolvedLocation)
interpreter.importResolvedLocation(resolvedLocation, &declaration.Aliases)
}

return nil
}

func (interpreter *Interpreter) importResolvedLocation(resolvedLocation sema.ResolvedLocation) {
func (interpreter *Interpreter) importResolvedLocation(resolvedLocation sema.ResolvedLocation, aliases *map[string]string) {
config := interpreter.SharedState.Config

// tracing
Expand All @@ -62,7 +62,14 @@ func (interpreter *Interpreter) importResolvedLocation(resolvedLocation sema.Res
if identifierLength > 0 {
variables = make(map[string]Variable, identifierLength)
for _, identifier := range resolvedLocation.Identifiers {
variables[identifier.Identifier] =
name := identifier.Identifier
alias, ok := (*aliases)[name]
if ok {
name = alias
}

// map alias to original
variables[name] =
subInterpreter.Globals.Get(identifier.Identifier)
}
} else {
Expand Down

0 comments on commit e430f1b

Please sign in to comment.