diff --git a/library/src/commonMain/kotlin/net/sergeych/ling/Compiler.kt b/library/src/commonMain/kotlin/net/sergeych/ling/Compiler.kt index 498b228..62460bb 100644 --- a/library/src/commonMain/kotlin/net/sergeych/ling/Compiler.kt +++ b/library/src/commonMain/kotlin/net/sergeych/ling/Compiler.kt @@ -501,10 +501,9 @@ class Compiler { var closure: Context? = null val fnBody = statement(t.pos) { callerContext -> - // remember closure where the function was defined: + // restore closure where the function was defined: val context = closure ?: Context() // load params from caller context - println("calling function $name in context $context <- ${context.parent}") for ((i, d) in params.withIndex()) { if (i < callerContext.args.size) context.addItem(d.name, false, callerContext.args.list[i].value) @@ -523,9 +522,12 @@ class Compiler { fnStatements.execute(context) } return statement(start) { context -> - println("adding function $name to context $context") + // we added fn in the context. now we must save closure + // for the function closure = context context.addItem(name, false, fnBody) + // as the function can be called from anywhere, we have + // saved the proper context in the closure fnBody } } diff --git a/library/src/commonMain/kotlin/net/sergeych/ling/Context.kt b/library/src/commonMain/kotlin/net/sergeych/ling/Context.kt index f0b51da..1d0e08d 100644 --- a/library/src/commonMain/kotlin/net/sergeych/ling/Context.kt +++ b/library/src/commonMain/kotlin/net/sergeych/ling/Context.kt @@ -14,7 +14,6 @@ class Context( fun copy(args: Arguments = Arguments.EMPTY): Context = Context(this, args) fun addItem(name: String, isMutable: Boolean, value: Obj?) { - println("ading item $name=$value in $this <- ${this.parent}") objects.put(name, StoredObj(name, value, isMutable)) } diff --git a/library/src/commonMain/kotlin/net/sergeych/ling/Script.kt b/library/src/commonMain/kotlin/net/sergeych/ling/Script.kt index 28d5bb8..0cee68d 100644 --- a/library/src/commonMain/kotlin/net/sergeych/ling/Script.kt +++ b/library/src/commonMain/kotlin/net/sergeych/ling/Script.kt @@ -8,8 +8,6 @@ class Script( ) : Statement() { override suspend fun execute(context: Context): Obj { - // todo: run script - println("exec script in $context <- ${context.parent}") var lastResult: Obj = ObjVoid for (s in statements) { lastResult = s.execute(context) diff --git a/library/src/commonTest/kotlin/ScriptTest.kt b/library/src/commonTest/kotlin/ScriptTest.kt index a2a9e97..30570f8 100644 --- a/library/src/commonTest/kotlin/ScriptTest.kt +++ b/library/src/commonTest/kotlin/ScriptTest.kt @@ -425,7 +425,6 @@ class ScriptTest { """ val count = 3 val res = if( count > 10 ) "too much" else "just " + count - println(res) res """.trimIndent() ) @@ -438,26 +437,10 @@ class ScriptTest { val count = 3 var res = if( count > 10 ) "too much" else "it's " + count res = if( count > 10 ) "too much" else "just " + count - println(res) res """.trimIndent() ) .toString() ) } - @Test - fun bookTest1() = runTest { -// assertEquals( -// "just 3", - eval( - """ - val count = 3 - println( - if( count > 10 ) "too much" else "just " + count - ) - """.trimIndent() - ) -// .toString() -// ) - } } \ No newline at end of file