From bf3967544aed4d06e5ccf053c757a5942a993b40 Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Sun, 3 Sep 2023 02:41:02 -0700 Subject: [PATCH] Complex import sample and support for import dependency cycles. --- examples/complex/a.pork | 3 +++ examples/complex/b.pork | 6 ++++++ examples/complex/c.pork | 6 ++++++ examples/complex/d.pork | 6 ++++++ examples/complex/main.pork | 5 +++++ src/main/kotlin/gay/pizza/pork/eval/EvaluationContext.kt | 6 ++++++ src/main/kotlin/gay/pizza/pork/eval/EvaluationVisitor.kt | 2 +- src/main/kotlin/gay/pizza/pork/eval/ImportLoader.kt | 7 ------- src/main/kotlin/gay/pizza/pork/eval/NullImportLoader.kt | 9 --------- 9 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 examples/complex/a.pork create mode 100644 examples/complex/b.pork create mode 100644 examples/complex/c.pork create mode 100644 examples/complex/d.pork create mode 100644 examples/complex/main.pork delete mode 100644 src/main/kotlin/gay/pizza/pork/eval/ImportLoader.kt delete mode 100644 src/main/kotlin/gay/pizza/pork/eval/NullImportLoader.kt diff --git a/examples/complex/a.pork b/examples/complex/a.pork new file mode 100644 index 0000000..800170c --- /dev/null +++ b/examples/complex/a.pork @@ -0,0 +1,3 @@ +export func a() { + println("A") +} diff --git a/examples/complex/b.pork b/examples/complex/b.pork new file mode 100644 index 0000000..de0cc07 --- /dev/null +++ b/examples/complex/b.pork @@ -0,0 +1,6 @@ +import "c.pork" + +export func b() { + c() + println("B") +} diff --git a/examples/complex/c.pork b/examples/complex/c.pork new file mode 100644 index 0000000..f1750b1 --- /dev/null +++ b/examples/complex/c.pork @@ -0,0 +1,6 @@ +import "a.pork" + +export func c() { + a() + println("C") +} diff --git a/examples/complex/d.pork b/examples/complex/d.pork new file mode 100644 index 0000000..103db93 --- /dev/null +++ b/examples/complex/d.pork @@ -0,0 +1,6 @@ +import "b.pork" + +export func d() { + b() + println("D") +} diff --git a/examples/complex/main.pork b/examples/complex/main.pork new file mode 100644 index 0000000..cd40019 --- /dev/null +++ b/examples/complex/main.pork @@ -0,0 +1,5 @@ +import "d.pork" + +export func main() { + d() +} diff --git a/src/main/kotlin/gay/pizza/pork/eval/EvaluationContext.kt b/src/main/kotlin/gay/pizza/pork/eval/EvaluationContext.kt index 8d5752a..8b0c818 100644 --- a/src/main/kotlin/gay/pizza/pork/eval/EvaluationContext.kt +++ b/src/main/kotlin/gay/pizza/pork/eval/EvaluationContext.kt @@ -8,12 +8,18 @@ class EvaluationContext( val evaluationContextProvider: EvaluationContextProvider, rootScope: Scope ) { + private var isAlreadySetup = false + val internalScope = rootScope.fork() val externalScope = rootScope.fork() private val evaluationVisitor = EvaluationVisitor(internalScope) fun setup() { + if (isAlreadySetup) { + return + } + isAlreadySetup = true val imports = compilationUnit.declarations.filterIsInstance() for (import in imports) { val evaluationContext = evaluationContextProvider.provideEvaluationContext(import.path.text) diff --git a/src/main/kotlin/gay/pizza/pork/eval/EvaluationVisitor.kt b/src/main/kotlin/gay/pizza/pork/eval/EvaluationVisitor.kt index 46c105a..005e779 100644 --- a/src/main/kotlin/gay/pizza/pork/eval/EvaluationVisitor.kt +++ b/src/main/kotlin/gay/pizza/pork/eval/EvaluationVisitor.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.eval import gay.pizza.pork.ast.NodeVisitor import gay.pizza.pork.ast.nodes.* -class EvaluationVisitor(val root: Scope) : NodeVisitor { +class EvaluationVisitor(root: Scope) : NodeVisitor { private var currentScope: Scope = root override fun visitIntLiteral(node: IntLiteral): Any = node.value diff --git a/src/main/kotlin/gay/pizza/pork/eval/ImportLoader.kt b/src/main/kotlin/gay/pizza/pork/eval/ImportLoader.kt deleted file mode 100644 index 08f7d59..0000000 --- a/src/main/kotlin/gay/pizza/pork/eval/ImportLoader.kt +++ /dev/null @@ -1,7 +0,0 @@ -package gay.pizza.pork.eval - -import gay.pizza.pork.ast.nodes.CompilationUnit - -interface ImportLoader { - fun load(path: String): CompilationUnit -} diff --git a/src/main/kotlin/gay/pizza/pork/eval/NullImportLoader.kt b/src/main/kotlin/gay/pizza/pork/eval/NullImportLoader.kt deleted file mode 100644 index 48c02b8..0000000 --- a/src/main/kotlin/gay/pizza/pork/eval/NullImportLoader.kt +++ /dev/null @@ -1,9 +0,0 @@ -package gay.pizza.pork.eval - -import gay.pizza.pork.ast.nodes.CompilationUnit - -object NullImportLoader : ImportLoader { - override fun load(path: String): CompilationUnit { - throw RuntimeException("NullImportLoader cannot import compilation units.") - } -}