From 8ee3195bef735ecb919d71bcae9016b9e923de04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Sat, 14 Mar 2020 16:44:28 +0100 Subject: [PATCH] Improve callstack() debug function and rename it to stacktrace() --- .../kotlin/io/smnp/environment/Environment.kt | 2 +- .../kotlin/io/smnp/runtime/model/CallStack.kt | 32 +++++++++++-------- .../io/smnp/runtime/model/CallStackFrame.kt | 17 ++++++---- .../io/smnp/environment/DefaultEnvironment.kt | 6 ++-- .../main/kotlin/io/smnp/ext/DebugModule.kt | 4 +-- .../io/smnp/ext/function/CallStackFunction.kt | 15 --------- .../io/smnp/ext/function/StackTraceFuction.kt | 18 +++++++++++ 7 files changed, 53 insertions(+), 41 deletions(-) delete mode 100644 modules/debug/src/main/kotlin/io/smnp/ext/function/CallStackFunction.kt create mode 100644 modules/debug/src/main/kotlin/io/smnp/ext/function/StackTraceFuction.kt diff --git a/api/src/main/kotlin/io/smnp/environment/Environment.kt b/api/src/main/kotlin/io/smnp/environment/Environment.kt index c2cdcdf..09ce8a1 100644 --- a/api/src/main/kotlin/io/smnp/environment/Environment.kt +++ b/api/src/main/kotlin/io/smnp/environment/Environment.kt @@ -10,7 +10,7 @@ interface Environment { fun printModules(printContent: Boolean) fun invokeFunction(name: String, arguments: List): Value fun invokeMethod(obj: Value, name: String, arguments: List): Value - fun printCallStack() + fun printCallStack(scopes: Boolean = false) fun stackTrace(): String fun defineFunction(function: Function) fun defineMethod(method: Method) diff --git a/api/src/main/kotlin/io/smnp/runtime/model/CallStack.kt b/api/src/main/kotlin/io/smnp/runtime/model/CallStack.kt index a65cc39..b09fdf3 100644 --- a/api/src/main/kotlin/io/smnp/runtime/model/CallStack.kt +++ b/api/src/main/kotlin/io/smnp/runtime/model/CallStack.kt @@ -6,24 +6,30 @@ import io.smnp.type.module.Module class CallStack { - private val items = Stack.of() + private val items = Stack.of() - fun push(module: Module, name: String, arguments: List) { - items.push(CallStackFrame(module, name, arguments)) - } + fun push(module: Module, name: String, arguments: List) { + items.push(CallStackFrame(module, name, arguments)) + } - fun push(frame: CallStackFrame) = items.push(frame) + fun push(frame: CallStackFrame) = items.push(frame) - fun pop() = items.pop() + fun pop() = items.pop() - fun top() = items.top() + fun top() = items.top() - val size: Int - get() = items.size + val size: Int + get() = items.size - fun pretty() { - items.asReversed().forEachIndexed { index, item -> println("[${items.size - index - 1}] $item") } - } + fun pretty(scopes: Boolean = false) { + items.asReversed().mapIndexed { index, item -> + println("[${items.size - index - 1}] $item") + if (scopes) { + item.scopes.forEach { println(" $it") } + } + } + } - fun stackTrace() = items.asReversed().mapIndexed { index, item -> "[${items.size - index - 1}] $item" }.joinToString("\n") + val stackTrace: List + get() = items.asReversed().mapIndexed { index, item -> "[${items.size - index - 1}] $item" } } \ No newline at end of file diff --git a/api/src/main/kotlin/io/smnp/runtime/model/CallStackFrame.kt b/api/src/main/kotlin/io/smnp/runtime/model/CallStackFrame.kt index ccf9041..cac6c63 100644 --- a/api/src/main/kotlin/io/smnp/runtime/model/CallStackFrame.kt +++ b/api/src/main/kotlin/io/smnp/runtime/model/CallStackFrame.kt @@ -11,29 +11,32 @@ data class CallStackFrame( val name: String, val arguments: List ) { - private val scopes = Stack.of>(mutableMapOf()) + private val scopesStack = Stack.of>(mutableMapOf()) fun pushScope(scope: MutableMap = mutableMapOf()) { - scopes.push(scope) + scopesStack.push(scope) } - fun popScope() = scopes.pop() + fun popScope() = scopesStack.pop() val scopesCount: Int - get() = scopes.size + get() = scopesStack.size fun setVariable(name: String, value: Value) { - val scope = scopes.lastOrNull { it.containsKey(name) } ?: scopes.top() + val scope = scopesStack.lastOrNull { it.containsKey(name) } ?: scopesStack.top() scope[name] = value } fun getVariable(name: String): Value { - return scopes.lastOrNull { it.containsKey(name) }?.get(name) ?: throw EvaluationException("Undefined variable `$name`") + return scopesStack.lastOrNull { it.containsKey(name) }?.get(name) ?: throw EvaluationException("Undefined variable `$name`") } fun prettyScope() { - scopes.asReversed().forEachIndexed { index, item -> println("[${scopes.size - index - 1}] $item") } + scopes.forEach { println(it) } } + val scopes: List + get() = scopesStack.asReversed().mapIndexed { index, item -> "[${scopesStack.size - index - 1}] $item" } + override fun toString() = "${module.canonicalName}::$name${ActualSignatureFormatter.format(arguments.toTypedArray())}" } \ No newline at end of file diff --git a/app/src/main/kotlin/io/smnp/environment/DefaultEnvironment.kt b/app/src/main/kotlin/io/smnp/environment/DefaultEnvironment.kt index 1310511..fb76325 100644 --- a/app/src/main/kotlin/io/smnp/environment/DefaultEnvironment.kt +++ b/app/src/main/kotlin/io/smnp/environment/DefaultEnvironment.kt @@ -123,12 +123,12 @@ class DefaultEnvironment : Environment { return value } - override fun printCallStack() { - callStack.pretty() + override fun printCallStack(scopes: Boolean) { + callStack.pretty(scopes) } override fun stackTrace(): String { - return callStack.stackTrace() + return callStack.stackTrace.joinToString("\n") } override fun defineFunction(function: Function) { diff --git a/modules/debug/src/main/kotlin/io/smnp/ext/DebugModule.kt b/modules/debug/src/main/kotlin/io/smnp/ext/DebugModule.kt index 9e943b6..dbac87d 100644 --- a/modules/debug/src/main/kotlin/io/smnp/ext/DebugModule.kt +++ b/modules/debug/src/main/kotlin/io/smnp/ext/DebugModule.kt @@ -1,9 +1,9 @@ package io.smnp.ext -import io.smnp.ext.function.CallStackFunction +import io.smnp.ext.function.StackTraceFuction import org.pf4j.Extension @Extension class DebugModule : NativeModuleProvider("smnp.lang.debug") { - override fun functions() = listOf(CallStackFunction()) + override fun functions() = listOf(StackTraceFuction()) } \ No newline at end of file diff --git a/modules/debug/src/main/kotlin/io/smnp/ext/function/CallStackFunction.kt b/modules/debug/src/main/kotlin/io/smnp/ext/function/CallStackFunction.kt deleted file mode 100644 index 24bce6e..0000000 --- a/modules/debug/src/main/kotlin/io/smnp/ext/function/CallStackFunction.kt +++ /dev/null @@ -1,15 +0,0 @@ -package io.smnp.ext.function - -import io.smnp.callable.function.Function -import io.smnp.callable.function.FunctionDefinitionTool -import io.smnp.callable.signature.Signature -import io.smnp.type.model.Value - -class CallStackFunction : Function("callstack") { - override fun define(new: FunctionDefinitionTool) { - new function Signature.simple() body { env, _ -> - env.printCallStack() - Value.void() - } - } -} \ No newline at end of file diff --git a/modules/debug/src/main/kotlin/io/smnp/ext/function/StackTraceFuction.kt b/modules/debug/src/main/kotlin/io/smnp/ext/function/StackTraceFuction.kt new file mode 100644 index 0000000..810daed --- /dev/null +++ b/modules/debug/src/main/kotlin/io/smnp/ext/function/StackTraceFuction.kt @@ -0,0 +1,18 @@ +package io.smnp.ext.function + +import io.smnp.callable.function.Function +import io.smnp.callable.function.FunctionDefinitionTool +import io.smnp.callable.signature.Signature +import io.smnp.type.enumeration.DataType.BOOL +import io.smnp.type.matcher.Matcher.Companion.ofType +import io.smnp.type.matcher.Matcher.Companion.optional +import io.smnp.type.model.Value + +class StackTraceFuction : Function("stacktrace") { + override fun define(new: FunctionDefinitionTool) { + new function Signature.simple(optional(ofType(BOOL))) body { env, args -> + env.printCallStack(args.getOrNull(0)?.value as Boolean? ?: false) + Value.void() + } + } +} \ No newline at end of file