From e430f1b9e7052feaed40e7708bb768077978501c Mon Sep 17 00:00:00 2001 From: Raymond Zhang Date: Mon, 16 Dec 2024 15:39:21 -0500 Subject: [PATCH] Add support for import aliasing in interpreter. --- interpreter/import_test.go | 2 +- interpreter/interpreter_import.go | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/interpreter/import_test.go b/interpreter/import_test.go index 6c8206b1a0..c261b56560 100644 --- a/interpreter/import_test.go +++ b/interpreter/import_test.go @@ -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{ diff --git a/interpreter/interpreter_import.go b/interpreter/interpreter_import.go index 4d92962417..3d7f169833 100644 --- a/interpreter/interpreter_import.go +++ b/interpreter/interpreter_import.go @@ -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 @@ -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 {