From 236072395ce056d2d2cccf950d3f243f099a178f Mon Sep 17 00:00:00 2001 From: imkiva Date: Sat, 8 May 2021 19:04:01 +0800 Subject: [PATCH] MISC: even more flatten! --- .../java/io/neolang/ast/NeoLangEOFToken.kt | 6 - .../main/java/io/neolang/ast/NeoLangToken.kt | 13 - .../java/io/neolang/ast/NeoLangTokenType.kt | 19 -- .../java/io/neolang/ast/base/NeoLangAst.kt | 12 - .../io/neolang/ast/base/NeoLangBaseNode.kt | 3 - .../io/neolang/ast/node/NeoLangArrayNode.kt | 12 - .../neolang/ast/node/NeoLangAstBasedNode.kt | 12 - .../neolang/ast/node/NeoLangAttributeNode.kt | 13 - .../io/neolang/ast/node/NeoLangBlockNode.kt | 14 -- .../io/neolang/ast/node/NeoLangDummyNode.kt | 8 - .../io/neolang/ast/node/NeoLangGroupNode.kt | 19 -- .../io/neolang/ast/node/NeoLangNumberNode.kt | 8 - .../io/neolang/ast/node/NeoLangProgramNode.kt | 21 -- .../io/neolang/ast/node/NeoLangStringNode.kt | 8 - .../neolang/ast/node/NeoLangTokenBasedNode.kt | 18 -- .../java/io/neolang/ast/visitor/AstVisitor.kt | 17 -- .../neolang/ast/visitor/IVisitorCallback.kt | 18 -- .../ast/visitor/IVisitorCallbackAdapter.kt | 24 -- .../io/neolang/ast/visitor/VisitorFactory.kt | 18 -- .../abstract-visitors.kt} | 76 +++++- .../NeoLangLexer.kt => frontend/frontend.kt} | 225 ++++++++++++++++-- .../main/java/io/neolang/frontend/nodes.kt | 114 +++++++++ .../tokens.kt} | 39 ++- .../ConfigVisitor.kt => frontend/visitors.kt} | 49 +++- NeoLang/src/main/java/io/neolang/main/Main.kt | 40 ---- .../neolang/parser/InvalidTokenException.kt | 6 - .../java/io/neolang/parser/NeoLangParser.kt | 209 ---------------- .../java/io/neolang/parser/ParseException.kt | 6 - .../{context/NeoLangContext.kt => context.kt} | 4 +- .../runtime/type/NeoLangArrayElement.kt | 18 -- .../io/neolang/runtime/type/NeoLangValue.kt | 30 --- .../{type/NeoLangArray.kt => types.kt} | 52 +++- .../neolang/visitor/DisplayProcessVisitor.kt | 42 ---- .../src/test/java/io/neolang/NeoLangTest.kt | 17 -- .../io/neoterm/component/NeoInitializer.kt | 41 ---- .../io/neoterm/component/colorscheme/comp.kt | 2 +- .../io/neoterm/component/colorscheme/data.kt | 2 +- .../main/java/io/neoterm/component/comp.kt | 38 +++ .../java/io/neoterm/component/config/comp.kt | 4 +- .../io/neoterm/component/config/loaders.kt | 4 +- .../main/java/io/neoterm/component/data.kt | 2 +- .../io/neoterm/component/extrakey/comp.kt | 2 +- .../io/neoterm/component/extrakey/data.kt | 2 +- .../java/io/neoterm/component/profile/comp.kt | 2 +- .../java/io/neoterm/component/profile/data.kt | 2 +- .../io/neoterm/component/session/shell.kt | 2 +- 46 files changed, 565 insertions(+), 728 deletions(-) delete mode 100644 NeoLang/src/main/java/io/neolang/ast/NeoLangEOFToken.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/NeoLangToken.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/NeoLangTokenType.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/base/NeoLangAst.kt delete mode 100755 NeoLang/src/main/java/io/neolang/ast/base/NeoLangBaseNode.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/node/NeoLangArrayNode.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/node/NeoLangAstBasedNode.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/node/NeoLangAttributeNode.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/node/NeoLangBlockNode.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/node/NeoLangDummyNode.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/node/NeoLangGroupNode.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/node/NeoLangNumberNode.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/node/NeoLangProgramNode.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/node/NeoLangStringNode.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/node/NeoLangTokenBasedNode.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/visitor/AstVisitor.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/visitor/IVisitorCallback.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/visitor/IVisitorCallbackAdapter.kt delete mode 100644 NeoLang/src/main/java/io/neolang/ast/visitor/VisitorFactory.kt rename NeoLang/src/main/java/io/neolang/{ast/visitor/AstVisitorImpl.kt => frontend/abstract-visitors.kt} (70%) rename NeoLang/src/main/java/io/neolang/{parser/NeoLangLexer.kt => frontend/frontend.kt} (53%) mode change 100755 => 100644 create mode 100644 NeoLang/src/main/java/io/neolang/frontend/nodes.kt rename NeoLang/src/main/java/io/neolang/{ast/NeoLangTokenValue.kt => frontend/tokens.kt} (64%) rename NeoLang/src/main/java/io/neolang/{visitor/ConfigVisitor.kt => frontend/visitors.kt} (64%) delete mode 100644 NeoLang/src/main/java/io/neolang/main/Main.kt delete mode 100644 NeoLang/src/main/java/io/neolang/parser/InvalidTokenException.kt delete mode 100755 NeoLang/src/main/java/io/neolang/parser/NeoLangParser.kt delete mode 100644 NeoLang/src/main/java/io/neolang/parser/ParseException.kt rename NeoLang/src/main/java/io/neolang/runtime/{context/NeoLangContext.kt => context.kt} (92%) delete mode 100644 NeoLang/src/main/java/io/neolang/runtime/type/NeoLangArrayElement.kt delete mode 100644 NeoLang/src/main/java/io/neolang/runtime/type/NeoLangValue.kt rename NeoLang/src/main/java/io/neolang/runtime/{type/NeoLangArray.kt => types.kt} (67%) delete mode 100644 NeoLang/src/main/java/io/neolang/visitor/DisplayProcessVisitor.kt delete mode 100644 NeoLang/src/test/java/io/neolang/NeoLangTest.kt delete mode 100644 app/src/main/java/io/neoterm/component/NeoInitializer.kt diff --git a/NeoLang/src/main/java/io/neolang/ast/NeoLangEOFToken.kt b/NeoLang/src/main/java/io/neolang/ast/NeoLangEOFToken.kt deleted file mode 100644 index ea79c4e..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/NeoLangEOFToken.kt +++ /dev/null @@ -1,6 +0,0 @@ -package io.neolang.ast - -/** - * @author kiva - */ -class NeoLangEOFToken : NeoLangToken(NeoLangTokenType.EOF, NeoLangTokenValue.EOF) \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/ast/NeoLangToken.kt b/NeoLang/src/main/java/io/neolang/ast/NeoLangToken.kt deleted file mode 100644 index 81d7272..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/NeoLangToken.kt +++ /dev/null @@ -1,13 +0,0 @@ -package io.neolang.ast - -/** - * @author kiva - */ - -open class NeoLangToken(val tokenType: NeoLangTokenType, val tokenValue: NeoLangTokenValue) { - var lineNumber = 0 - - override fun toString(): String { - return "Token { tokenType: $tokenType, tokenValue: $tokenValue };" - } -} diff --git a/NeoLang/src/main/java/io/neolang/ast/NeoLangTokenType.kt b/NeoLang/src/main/java/io/neolang/ast/NeoLangTokenType.kt deleted file mode 100644 index d70d5e8..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/NeoLangTokenType.kt +++ /dev/null @@ -1,19 +0,0 @@ -package io.neolang.ast - -/** - * @author kiva - */ - -enum class NeoLangTokenType { - NUMBER, - ID, - STRING, - BRACKET_START, - BRACKET_END, - ARRAY_START, - ARRAY_END, - COLON, - COMMA, - EOL, - EOF, -} diff --git a/NeoLang/src/main/java/io/neolang/ast/base/NeoLangAst.kt b/NeoLang/src/main/java/io/neolang/ast/base/NeoLangAst.kt deleted file mode 100644 index 3f080be..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/base/NeoLangAst.kt +++ /dev/null @@ -1,12 +0,0 @@ -package io.neolang.ast.base - -import io.neolang.ast.visitor.VisitorFactory - -/** - * @author kiva - */ -open class NeoLangAst { - fun visit(): VisitorFactory { - return VisitorFactory(this) - } -} \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/ast/base/NeoLangBaseNode.kt b/NeoLang/src/main/java/io/neolang/ast/base/NeoLangBaseNode.kt deleted file mode 100755 index d8b690f..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/base/NeoLangBaseNode.kt +++ /dev/null @@ -1,3 +0,0 @@ -package io.neolang.ast.base - -open class NeoLangBaseNode : NeoLangAst() diff --git a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangArrayNode.kt b/NeoLang/src/main/java/io/neolang/ast/node/NeoLangArrayNode.kt deleted file mode 100644 index 458fd6c..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangArrayNode.kt +++ /dev/null @@ -1,12 +0,0 @@ -package io.neolang.ast.node - -import io.neolang.ast.base.NeoLangBaseNode - -/** - * @author kiva - */ -class NeoLangArrayNode(val arrayNameNode: NeoLangStringNode, val elements: Array) : NeoLangBaseNode() { - companion object { - class ArrayElement(val index: Int, val block: NeoLangBlockNode) - } -} \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangAstBasedNode.kt b/NeoLang/src/main/java/io/neolang/ast/node/NeoLangAstBasedNode.kt deleted file mode 100644 index f98847f..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangAstBasedNode.kt +++ /dev/null @@ -1,12 +0,0 @@ -package io.neolang.ast.node - -import io.neolang.ast.base.NeoLangBaseNode - -/** - * @author kiva - */ -open class NeoLangAstBasedNode(val ast: NeoLangBaseNode) : NeoLangBaseNode() { - override fun toString(): String { - return "${javaClass.simpleName} { ast: $ast }" - } -} \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangAttributeNode.kt b/NeoLang/src/main/java/io/neolang/ast/node/NeoLangAttributeNode.kt deleted file mode 100644 index 45843e4..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangAttributeNode.kt +++ /dev/null @@ -1,13 +0,0 @@ -package io.neolang.ast.node - -import io.neolang.ast.base.NeoLangBaseNode - -/** - * @author kiva - */ -class NeoLangAttributeNode(val stringNode: NeoLangStringNode, val blockNode: NeoLangBlockNode) : NeoLangBaseNode() { - - override fun toString(): String { - return "NeoLangAttributeNode { stringNode: $stringNode, block: $blockNode }" - } -} diff --git a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangBlockNode.kt b/NeoLang/src/main/java/io/neolang/ast/node/NeoLangBlockNode.kt deleted file mode 100644 index d666465..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangBlockNode.kt +++ /dev/null @@ -1,14 +0,0 @@ -package io.neolang.ast.node - -import io.neolang.ast.base.NeoLangBaseNode - -/** - * @author kiva - */ -class NeoLangBlockNode(blockElement: NeoLangBaseNode) : NeoLangAstBasedNode(blockElement) { - companion object { - fun emptyNode(): NeoLangBlockNode { - return NeoLangBlockNode(NeoLangDummyNode()) - } - } -} \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangDummyNode.kt b/NeoLang/src/main/java/io/neolang/ast/node/NeoLangDummyNode.kt deleted file mode 100644 index 886ef63..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangDummyNode.kt +++ /dev/null @@ -1,8 +0,0 @@ -package io.neolang.ast.node - -import io.neolang.ast.base.NeoLangBaseNode - -/** - * @author kiva - */ -class NeoLangDummyNode : NeoLangBaseNode() \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangGroupNode.kt b/NeoLang/src/main/java/io/neolang/ast/node/NeoLangGroupNode.kt deleted file mode 100644 index b4771bd..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangGroupNode.kt +++ /dev/null @@ -1,19 +0,0 @@ -package io.neolang.ast.node - -import io.neolang.ast.base.NeoLangBaseNode - -/** - * @author kiva - */ -class NeoLangGroupNode(val attributes: Array) : NeoLangBaseNode() { - - override fun toString(): String { - return "NeoLangGroupNode { attrs: $attributes }" - } - - companion object { - fun emptyNode(): NeoLangGroupNode { - return NeoLangGroupNode(arrayOf()) - } - } -} \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangNumberNode.kt b/NeoLang/src/main/java/io/neolang/ast/node/NeoLangNumberNode.kt deleted file mode 100644 index 106c333..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangNumberNode.kt +++ /dev/null @@ -1,8 +0,0 @@ -package io.neolang.ast.node - -import io.neolang.ast.NeoLangToken - -/** - * @author kiva - */ -class NeoLangNumberNode(token: NeoLangToken) : NeoLangTokenBasedNode(token) \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangProgramNode.kt b/NeoLang/src/main/java/io/neolang/ast/node/NeoLangProgramNode.kt deleted file mode 100644 index 10e73df..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangProgramNode.kt +++ /dev/null @@ -1,21 +0,0 @@ -package io.neolang.ast.node - -import io.neolang.ast.base.NeoLangBaseNode - -/** - * @author kiva - */ - -class NeoLangProgramNode(val groups: List) : NeoLangBaseNode() { - - override fun toString(): String { - return "NeoLangProgramNode { groups: $groups }" - } - - companion object { - fun emptyNode(): NeoLangProgramNode { - return NeoLangProgramNode(listOf()) - } - } -} - diff --git a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangStringNode.kt b/NeoLang/src/main/java/io/neolang/ast/node/NeoLangStringNode.kt deleted file mode 100644 index 5d89e49..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangStringNode.kt +++ /dev/null @@ -1,8 +0,0 @@ -package io.neolang.ast.node - -import io.neolang.ast.NeoLangToken - -/** - * @author kiva - */ -class NeoLangStringNode(token: NeoLangToken) : NeoLangTokenBasedNode(token) \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangTokenBasedNode.kt b/NeoLang/src/main/java/io/neolang/ast/node/NeoLangTokenBasedNode.kt deleted file mode 100644 index b89d793..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/node/NeoLangTokenBasedNode.kt +++ /dev/null @@ -1,18 +0,0 @@ -package io.neolang.ast.node - -import io.neolang.ast.NeoLangToken -import io.neolang.ast.base.NeoLangBaseNode -import io.neolang.runtime.type.NeoLangValue - -/** - * @author kiva - */ -open class NeoLangTokenBasedNode(val token: NeoLangToken) : NeoLangBaseNode() { - override fun toString(): String { - return "${javaClass.simpleName} { token: $token }" - } - - fun eval(): NeoLangValue { - return token.tokenValue.value - } -} \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/ast/visitor/AstVisitor.kt b/NeoLang/src/main/java/io/neolang/ast/visitor/AstVisitor.kt deleted file mode 100644 index daccf73..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/visitor/AstVisitor.kt +++ /dev/null @@ -1,17 +0,0 @@ -package io.neolang.ast.visitor - -import io.neolang.ast.base.NeoLangAst - -/** - * @author kiva - */ -class AstVisitor(private val ast: NeoLangAst, private val visitorCallback: IVisitorCallback) { - fun start() { - AstVisitorImpl.visitStartAst(ast, visitorCallback) - } - - @Suppress("UNCHECKED_CAST") - fun getCallback(): T { - return visitorCallback as T - } -} diff --git a/NeoLang/src/main/java/io/neolang/ast/visitor/IVisitorCallback.kt b/NeoLang/src/main/java/io/neolang/ast/visitor/IVisitorCallback.kt deleted file mode 100644 index a83e295..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/visitor/IVisitorCallback.kt +++ /dev/null @@ -1,18 +0,0 @@ -package io.neolang.ast.visitor - -import io.neolang.runtime.context.NeoLangContext - -/** - * @author kiva - */ -interface IVisitorCallback { - fun onStart() - - fun onFinish() - - fun onEnterContext(contextName: String) - - fun onExitContext() - - fun getCurrentContext(): NeoLangContext -} diff --git a/NeoLang/src/main/java/io/neolang/ast/visitor/IVisitorCallbackAdapter.kt b/NeoLang/src/main/java/io/neolang/ast/visitor/IVisitorCallbackAdapter.kt deleted file mode 100644 index 8412efb..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/visitor/IVisitorCallbackAdapter.kt +++ /dev/null @@ -1,24 +0,0 @@ -package io.neolang.ast.visitor - -import io.neolang.runtime.context.NeoLangContext - -/** - * @author kiva - */ -open class IVisitorCallbackAdapter : IVisitorCallback { - override fun onStart() { - } - - override fun onFinish() { - } - - override fun onEnterContext(contextName: String) { - } - - override fun onExitContext() { - } - - override fun getCurrentContext(): NeoLangContext { - throw RuntimeException("getCurrentContext() not supported in this IVisitorCallback!") - } -} \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/ast/visitor/VisitorFactory.kt b/NeoLang/src/main/java/io/neolang/ast/visitor/VisitorFactory.kt deleted file mode 100644 index b6a8487..0000000 --- a/NeoLang/src/main/java/io/neolang/ast/visitor/VisitorFactory.kt +++ /dev/null @@ -1,18 +0,0 @@ -package io.neolang.ast.visitor - -import io.neolang.ast.base.NeoLangAst - -/** - * @author kiva - */ - -class VisitorFactory(private val ast: NeoLangAst) { - - fun getVisitor(callbackInterface: Class): AstVisitor? { - try { - return AstVisitor(ast, callbackInterface.newInstance()) - } catch (e: Exception) { - return null - } - } -} diff --git a/NeoLang/src/main/java/io/neolang/ast/visitor/AstVisitorImpl.kt b/NeoLang/src/main/java/io/neolang/frontend/abstract-visitors.kt similarity index 70% rename from NeoLang/src/main/java/io/neolang/ast/visitor/AstVisitorImpl.kt rename to NeoLang/src/main/java/io/neolang/frontend/abstract-visitors.kt index 0475497..74c6c1b 100644 --- a/NeoLang/src/main/java/io/neolang/ast/visitor/AstVisitorImpl.kt +++ b/NeoLang/src/main/java/io/neolang/frontend/abstract-visitors.kt @@ -1,18 +1,21 @@ -package io.neolang.ast.visitor - -import io.neolang.ast.base.NeoLangAst -import io.neolang.ast.node.* -import io.neolang.runtime.type.NeoLangValue +package io.neolang.frontend +import io.neolang.runtime.NeoLangContext +import io.neolang.runtime.NeoLangValue /** - * grammar: [ - * program: group (group)* - * group: attribute (attribute)* - * attribute: ID COLON block - * block: STRING | NUMBER | (BRACKET_START [group|] BRACKET_END) | (ARRAY_START [block(<,block>)+|] ARRAY_END) - * ] + * @author kiva */ +class AstVisitor(private val ast: NeoLangAst, private val visitorCallback: IVisitorCallback) { + fun start() { + AstVisitorImpl.visitStartAst(ast, visitorCallback) + } + + @Suppress("UNCHECKED_CAST") + fun getCallback(): T { + return visitorCallback as T + } +} /** * @author kiva @@ -102,3 +105,54 @@ internal object AstVisitorImpl { } } } + +/** + * @author kiva + */ +interface IVisitorCallback { + fun onStart() + + fun onFinish() + + fun onEnterContext(contextName: String) + + fun onExitContext() + + fun getCurrentContext(): NeoLangContext +} + +/** + * @author kiva + */ +open class IVisitorCallbackAdapter : IVisitorCallback { + override fun onStart() { + } + + override fun onFinish() { + } + + override fun onEnterContext(contextName: String) { + } + + override fun onExitContext() { + } + + override fun getCurrentContext(): NeoLangContext { + throw RuntimeException("getCurrentContext() not supported in this IVisitorCallback!") + } +} + +/** + * @author kiva + */ + +class VisitorFactory(private val ast: NeoLangAst) { + + fun getVisitor(callbackInterface: Class): AstVisitor? { + try { + return AstVisitor(ast, callbackInterface.newInstance()) + } catch (e: Exception) { + return null + } + } +} diff --git a/NeoLang/src/main/java/io/neolang/parser/NeoLangLexer.kt b/NeoLang/src/main/java/io/neolang/frontend/frontend.kt old mode 100755 new mode 100644 similarity index 53% rename from NeoLang/src/main/java/io/neolang/parser/NeoLangLexer.kt rename to NeoLang/src/main/java/io/neolang/frontend/frontend.kt index 7175877..c31d513 --- a/NeoLang/src/main/java/io/neolang/parser/NeoLangLexer.kt +++ b/NeoLang/src/main/java/io/neolang/frontend/frontend.kt @@ -1,18 +1,4 @@ -package io.neolang.parser - -import io.neolang.ast.NeoLangEOFToken -import io.neolang.ast.NeoLangToken -import io.neolang.ast.NeoLangTokenType -import io.neolang.ast.NeoLangTokenValue - -/** - * grammar: [ - * program: group (group)* - * group: attribute (attribute)* - * attribute: ID COLON block - * block: STRING | NUMBER | (BRACKET_START [group|] BRACKET_END) | (ARRAY_START [block(<,block>)+|] ARRAY_END) - * ] - */ +package io.neolang.frontend /** * @author kiva @@ -201,9 +187,7 @@ class NeoLangLexer { var value = numberValue var loop = moveToNextChar() // skip 'x' or 'X' while (loop && (currentChar.isHexNumber())) { - value *= 16 - +(currentChar.toInt().and(15)) - +if (currentChar >= 'A') 9 else 0 + value *= 16 + (currentChar.toInt().and(15)) + if (currentChar >= 'A') 9 else 0 loop = moveToNextChar() } return value @@ -270,3 +254,208 @@ class NeoLangLexer { || this in ('A'..'F') } } + +/** + * @author kiva + */ +class NeoLangParser { + private val lexer = NeoLangLexer() + private var tokens = mutableListOf() + private var currentPosition: Int = 0 + private var currentToken: NeoLangToken? = null + + fun setInputSource(programCode: String?) { + lexer.setInputSource(programCode) + } + + fun parse(): NeoLangAst { + return updateParserStatus(lexer.lex()) ?: throw ParseException("AST is null") + } + + private fun updateParserStatus(tokens: List): NeoLangAst? { + if (tokens.isEmpty()) { + // Allow empty program + return NeoLangProgramNode.emptyNode() + } + + this.tokens.clear() + this.tokens.addAll(tokens) + currentPosition = 0 + currentToken = tokens[currentPosition] + return program() + } + + private fun match(tokenType: NeoLangTokenType, errorThrow: Boolean = false): Boolean { + val currentToken = this.currentToken ?: throw InvalidTokenException("Unexpected token: null") + + if (currentToken.tokenType === tokenType) { + currentPosition++ + if (currentPosition >= tokens.size) { + this.currentToken = NeoLangToken(NeoLangTokenType.EOF, NeoLangTokenValue.EOF) + } else { + this.currentToken = tokens[currentPosition] + } + return true + + } else if (errorThrow) { + throw InvalidTokenException( + "Unexpected token `${currentToken.tokenValue}' typed " + + "`${currentToken.tokenType}' near line ${currentToken.lineNumber}, " + + "expected $tokenType", + ) + } + + return false + } + + private fun program(): NeoLangProgramNode { + val token = currentToken + + var group = group() + if (group != null) { + val groups = mutableListOf(group) + while (token?.tokenType !== NeoLangTokenType.EOF) { + group = group() + if (group == null) { + break + } + groups.add(group) + } + return NeoLangProgramNode(groups) + } + + return NeoLangProgramNode.emptyNode() + } + + /** + * @param attrName Only available when group is a attribute value + */ + private fun group(): NeoLangGroupNode? { + val token = currentToken ?: throw InvalidTokenException("Unexpected token: null") + + var attr = attribute() + if (attr != null) { + val attributes = mutableListOf(attr) + + while (token.tokenType !== NeoLangTokenType.EOF + && token.tokenType !== NeoLangTokenType.BRACKET_END + && token.tokenType !== NeoLangTokenType.ARRAY_END + ) { + attr = attribute() + if (attr == null) { + break + } + attributes.add(attr) + } + return NeoLangGroupNode(attributes.toTypedArray()) + } + + return null + } + + private fun attribute(): NeoLangAttributeNode? { + val token = currentToken ?: throw InvalidTokenException("Unexpected token: null") + if (match(NeoLangTokenType.ID)) { + match(NeoLangTokenType.COLON, errorThrow = true) + + val attrName = NeoLangStringNode(token) + + val block = block(attrName) ?: NeoLangBlockNode.emptyNode() + return NeoLangAttributeNode(attrName, block) + } + return null + } + + private fun array(arrayName: NeoLangStringNode): NeoLangArrayNode? { + val token = currentToken ?: throw InvalidTokenException("Unexpected token: null") + + // TODO: Multiple Array + var block = blockNonArrayElement(arrayName) + var index = 0 + + if (block != null) { + val elements = mutableListOf(NeoLangArrayNode.Companion.ArrayElement(index++, block)) + + if (match(NeoLangTokenType.COMMA)) { + // More than one elements + while (token.tokenType !== NeoLangTokenType.EOF + && token.tokenType !== NeoLangTokenType.ARRAY_END + ) { + block = blockNonArrayElement(arrayName) + if (block == null) { + break + } + elements.add(NeoLangArrayNode.Companion.ArrayElement(index++, block)) + + // Meet the last element + if (!match(NeoLangTokenType.COMMA)) { + break + } + } + } + + return NeoLangArrayNode(arrayName, elements.toTypedArray()) + } + + return null + } + + + /** + * @param attrName The block holder's name + */ + private fun block(attrName: NeoLangStringNode): NeoLangBlockNode? { + val block = blockNonArrayElement(attrName) + if (block != null) { + return block + } + + val token = currentToken ?: throw InvalidTokenException("Unexpected token: null") + when (token.tokenType) { + NeoLangTokenType.ARRAY_START -> { + match(NeoLangTokenType.ARRAY_START, errorThrow = true) + val array = array(attrName) + match(NeoLangTokenType.ARRAY_END, errorThrow = true) + + // Allow empty arrays + return if (array != null) NeoLangBlockNode(array) else NeoLangBlockNode.emptyNode() + } + + else -> throw InvalidTokenException("Unexpected token `${token.tokenValue}' typed `${token.tokenType}' for block") + } + } + + /** + * @param attrName Only available when group is a attribute value + */ + private fun blockNonArrayElement(attrName: NeoLangStringNode?): NeoLangBlockNode? { + val token = currentToken ?: throw InvalidTokenException("Unexpected token: null") + + return when (token.tokenType) { + NeoLangTokenType.NUMBER -> { + match(NeoLangTokenType.NUMBER, errorThrow = true) + return NeoLangBlockNode(NeoLangNumberNode(token)) + } + NeoLangTokenType.ID -> { + match(NeoLangTokenType.ID, errorThrow = true) + return NeoLangBlockNode(NeoLangStringNode(token)) + } + NeoLangTokenType.STRING -> { + match(NeoLangTokenType.STRING, errorThrow = true) + return NeoLangBlockNode(NeoLangStringNode(token)) + } + NeoLangTokenType.BRACKET_START -> { + match(NeoLangTokenType.BRACKET_START, errorThrow = true) + val group = group() + match(NeoLangTokenType.BRACKET_END, errorThrow = true) + + // Allow empty blocks + return if (group != null) NeoLangBlockNode(group) else NeoLangBlockNode.emptyNode() + } + else -> null + } + } +} + +open class InvalidTokenException(message: String) : ParseException(message) +open class ParseException(message: String) : RuntimeException(message) diff --git a/NeoLang/src/main/java/io/neolang/frontend/nodes.kt b/NeoLang/src/main/java/io/neolang/frontend/nodes.kt new file mode 100644 index 0000000..63acf5d --- /dev/null +++ b/NeoLang/src/main/java/io/neolang/frontend/nodes.kt @@ -0,0 +1,114 @@ +package io.neolang.frontend + +import io.neolang.runtime.NeoLangValue + +/** + * @author kiva + */ +open class NeoLangAst { + fun visit(): VisitorFactory { + return VisitorFactory(this) + } +} + +open class NeoLangBaseNode : NeoLangAst() + +/** + * @author kiva + */ +class NeoLangArrayNode(val arrayNameNode: NeoLangStringNode, val elements: Array) : NeoLangBaseNode() { + companion object { + class ArrayElement(val index: Int, val block: NeoLangBlockNode) + } +} + +/** + * @author kiva + */ +open class NeoLangAstBasedNode(val ast: NeoLangBaseNode) : NeoLangBaseNode() { + override fun toString(): String { + return "${javaClass.simpleName} { ast: $ast }" + } +} + +/** + * @author kiva + */ +class NeoLangAttributeNode(val stringNode: NeoLangStringNode, val blockNode: NeoLangBlockNode) : NeoLangBaseNode() { + + override fun toString(): String { + return "NeoLangAttributeNode { stringNode: $stringNode, block: $blockNode }" + } +} + +/** + * @author kiva + */ +class NeoLangBlockNode(blockElement: NeoLangBaseNode) : NeoLangAstBasedNode(blockElement) { + companion object { + fun emptyNode(): NeoLangBlockNode { + return NeoLangBlockNode(NeoLangDummyNode()) + } + } +} + +/** + * @author kiva + */ +class NeoLangDummyNode : NeoLangBaseNode() + +/** + * @author kiva + */ +class NeoLangGroupNode(val attributes: Array) : NeoLangBaseNode() { + + override fun toString(): String { + return "NeoLangGroupNode { attrs: $attributes }" + } + + companion object { + fun emptyNode(): NeoLangGroupNode { + return NeoLangGroupNode(arrayOf()) + } + } +} + +/** + * @author kiva + */ +class NeoLangNumberNode(token: NeoLangToken) : NeoLangTokenBasedNode(token) + +/** + * @author kiva + */ + +class NeoLangProgramNode(val groups: List) : NeoLangBaseNode() { + + override fun toString(): String { + return "NeoLangProgramNode { groups: $groups }" + } + + companion object { + fun emptyNode(): NeoLangProgramNode { + return NeoLangProgramNode(listOf()) + } + } +} + +/** + * @author kiva + */ +class NeoLangStringNode(token: NeoLangToken) : NeoLangTokenBasedNode(token) + +/** + * @author kiva + */ +open class NeoLangTokenBasedNode(val token: NeoLangToken) : NeoLangBaseNode() { + override fun toString(): String { + return "${javaClass.simpleName} { token: $token }" + } + + fun eval(): NeoLangValue { + return token.tokenValue.value + } +} diff --git a/NeoLang/src/main/java/io/neolang/ast/NeoLangTokenValue.kt b/NeoLang/src/main/java/io/neolang/frontend/tokens.kt similarity index 64% rename from NeoLang/src/main/java/io/neolang/ast/NeoLangTokenValue.kt rename to NeoLang/src/main/java/io/neolang/frontend/tokens.kt index 4ccb111..214230e 100644 --- a/NeoLang/src/main/java/io/neolang/ast/NeoLangTokenValue.kt +++ b/NeoLang/src/main/java/io/neolang/frontend/tokens.kt @@ -1,6 +1,41 @@ -package io.neolang.ast +package io.neolang.frontend -import io.neolang.runtime.type.NeoLangValue +import io.neolang.runtime.NeoLangValue + +/** + * @author kiva + */ +class NeoLangEOFToken : NeoLangToken(NeoLangTokenType.EOF, NeoLangTokenValue.EOF) + +/** + * @author kiva + */ + +open class NeoLangToken(val tokenType: NeoLangTokenType, val tokenValue: NeoLangTokenValue) { + var lineNumber = 0 + + override fun toString(): String { + return "Token { tokenType: $tokenType, tokenValue: $tokenValue };" + } +} + +/** + * @author kiva + */ + +enum class NeoLangTokenType { + NUMBER, + ID, + STRING, + BRACKET_START, + BRACKET_END, + ARRAY_START, + ARRAY_END, + COLON, + COMMA, + EOL, + EOF, +} /** * @author kiva diff --git a/NeoLang/src/main/java/io/neolang/visitor/ConfigVisitor.kt b/NeoLang/src/main/java/io/neolang/frontend/visitors.kt similarity index 64% rename from NeoLang/src/main/java/io/neolang/visitor/ConfigVisitor.kt rename to NeoLang/src/main/java/io/neolang/frontend/visitors.kt index 46622f0..e057110 100644 --- a/NeoLang/src/main/java/io/neolang/visitor/ConfigVisitor.kt +++ b/NeoLang/src/main/java/io/neolang/frontend/visitors.kt @@ -1,9 +1,9 @@ -package io.neolang.visitor +package io.neolang.frontend -import io.neolang.ast.visitor.IVisitorCallback -import io.neolang.runtime.context.NeoLangContext -import io.neolang.runtime.type.NeoLangArray -import io.neolang.runtime.type.NeoLangValue +import io.neolang.runtime.NeoLangArray +import io.neolang.runtime.NeoLangContext +import io.neolang.runtime.NeoLangValue +import java.util.* class ConfigVisitor : IVisitorCallback { private var rootContext: NeoLangContext? = null @@ -70,4 +70,41 @@ class ConfigVisitor : IVisitorCallback { override fun getCurrentContext(): NeoLangContext { return currentContext!! } -} \ No newline at end of file +} + +/** + * @author kiva + */ +class DisplayProcessVisitor : IVisitorCallbackAdapter() { + private val contextStack = Stack() + + override fun onStart() { + println(">>> Start") + onEnterContext("global") + } + + override fun onFinish() { + while (contextStack.isNotEmpty()) { + onExitContext() + } + println(">>> Finish") + } + + override fun onEnterContext(contextName: String) { + val context = NeoLangContext(contextName) + contextStack.push(context) + println(">>> Entering Context `$contextName'") + } + + override fun onExitContext() { + val context = contextStack.pop() + println(">>> Exiting & Dumping Context ${context.contextName}") + context.getAttributes().entries.forEach { + println(" > [${it.key}]: ${it.value.asString()}") + } + } + + override fun getCurrentContext(): NeoLangContext { + return contextStack.peek() + } +} diff --git a/NeoLang/src/main/java/io/neolang/main/Main.kt b/NeoLang/src/main/java/io/neolang/main/Main.kt deleted file mode 100644 index ad4ff49..0000000 --- a/NeoLang/src/main/java/io/neolang/main/Main.kt +++ /dev/null @@ -1,40 +0,0 @@ -package io.neolang.main - -import io.neolang.parser.NeoLangParser -import io.neolang.visitor.DisplayProcessVisitor -import java.io.FileInputStream - -/** - * @author kiva - */ -class Main { - companion object { - @JvmStatic - fun main(args: Array) { - if (args.isEmpty()) { - println("Usage: NeoLang ") - return - } - - val parser = NeoLangParser() - args.forEach { - val programCode = readFully(it) - parser.setInputSource(programCode) - val ast = parser.parse() - println("Compile `$it'") - ast.visit() - .getVisitor(DisplayProcessVisitor::class.java) - ?.start() - } - return - } - - private fun readFully(file: String): String { - FileInputStream(file).use { - val bytes = ByteArray(it.available()) - it.read(bytes) - return String(bytes) - } - } - } -} \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/parser/InvalidTokenException.kt b/NeoLang/src/main/java/io/neolang/parser/InvalidTokenException.kt deleted file mode 100644 index 6e2189e..0000000 --- a/NeoLang/src/main/java/io/neolang/parser/InvalidTokenException.kt +++ /dev/null @@ -1,6 +0,0 @@ -package io.neolang.parser - -/** - * @author kiva - */ -open class InvalidTokenException(message: String) : ParseException(message) \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/parser/NeoLangParser.kt b/NeoLang/src/main/java/io/neolang/parser/NeoLangParser.kt deleted file mode 100755 index 7a06b33..0000000 --- a/NeoLang/src/main/java/io/neolang/parser/NeoLangParser.kt +++ /dev/null @@ -1,209 +0,0 @@ -package io.neolang.parser - -import io.neolang.ast.NeoLangToken -import io.neolang.ast.NeoLangTokenType -import io.neolang.ast.NeoLangTokenValue -import io.neolang.ast.base.NeoLangAst -import io.neolang.ast.node.* - -/** - * @author kiva - */ -class NeoLangParser { - private val lexer = NeoLangLexer() - private var tokens = mutableListOf() - private var currentPosition: Int = 0 - private var currentToken: NeoLangToken? = null - - fun setInputSource(programCode: String?) { - lexer.setInputSource(programCode) - } - - fun parse(): NeoLangAst { - return updateParserStatus(lexer.lex()) ?: throw ParseException("AST is null") - } - - private fun updateParserStatus(tokens: List): NeoLangAst? { - if (tokens.isEmpty()) { - // Allow empty program - return NeoLangProgramNode.emptyNode() - } - - this.tokens.clear() - this.tokens.addAll(tokens) - currentPosition = 0 - currentToken = tokens[currentPosition] - return program() - } - - private fun match(tokenType: NeoLangTokenType, errorThrow: Boolean = false): Boolean { - val currentToken = this.currentToken ?: throw InvalidTokenException("Unexpected token: null") - - if (currentToken.tokenType === tokenType) { - currentPosition++ - if (currentPosition >= tokens.size) { - this.currentToken = NeoLangToken(NeoLangTokenType.EOF, NeoLangTokenValue.EOF) - } else { - this.currentToken = tokens[currentPosition] - } - return true - - } else if (errorThrow) { - throw InvalidTokenException( - "Unexpected token `${currentToken.tokenValue}' typed " + - "`${currentToken.tokenType}' near line ${currentToken.lineNumber}, " + - "expected $tokenType" - ) - } - - return false - } - - private fun program(): NeoLangProgramNode { - val token = currentToken - - var group = group() - if (group != null) { - val groups = mutableListOf(group) - while (token?.tokenType !== NeoLangTokenType.EOF) { - group = group() - if (group == null) { - break - } - groups.add(group) - } - return NeoLangProgramNode(groups) - } - - return NeoLangProgramNode.emptyNode() - } - - /** - * @param attrName Only available when group is a attribute value - */ - private fun group(): NeoLangGroupNode? { - val token = currentToken ?: throw InvalidTokenException("Unexpected token: null") - - var attr = attribute() - if (attr != null) { - val attributes = mutableListOf(attr) - - while (token.tokenType !== NeoLangTokenType.EOF - && token.tokenType !== NeoLangTokenType.BRACKET_END - && token.tokenType !== NeoLangTokenType.ARRAY_END - ) { - attr = attribute() - if (attr == null) { - break - } - attributes.add(attr) - } - return NeoLangGroupNode(attributes.toTypedArray()) - } - - return null - } - - private fun attribute(): NeoLangAttributeNode? { - val token = currentToken ?: throw InvalidTokenException("Unexpected token: null") - if (match(NeoLangTokenType.ID)) { - match(NeoLangTokenType.COLON, errorThrow = true) - - val attrName = NeoLangStringNode(token) - - val block = block(attrName) ?: NeoLangBlockNode.emptyNode() - return NeoLangAttributeNode(attrName, block) - } - return null - } - - private fun array(arrayName: NeoLangStringNode): NeoLangArrayNode? { - val token = currentToken ?: throw InvalidTokenException("Unexpected token: null") - - // TODO: Multiple Array - var block = blockNonArrayElement(arrayName) - var index = 0 - - if (block != null) { - val elements = mutableListOf(NeoLangArrayNode.Companion.ArrayElement(index++, block)) - - if (match(NeoLangTokenType.COMMA)) { - // More than one elements - while (token.tokenType !== NeoLangTokenType.EOF - && token.tokenType !== NeoLangTokenType.ARRAY_END - ) { - block = blockNonArrayElement(arrayName) - if (block == null) { - break - } - elements.add(NeoLangArrayNode.Companion.ArrayElement(index++, block)) - - // Meet the last element - if (!match(NeoLangTokenType.COMMA)) { - break - } - } - } - - return NeoLangArrayNode(arrayName, elements.toTypedArray()) - } - - return null - } - - - /** - * @param attrName The block holder's name - */ - private fun block(attrName: NeoLangStringNode): NeoLangBlockNode? { - val block = blockNonArrayElement(attrName) - if (block != null) { - return block - } - - val token = currentToken ?: throw InvalidTokenException("Unexpected token: null") - when (token.tokenType) { - NeoLangTokenType.ARRAY_START -> { - match(NeoLangTokenType.ARRAY_START, errorThrow = true) - val array = array(attrName) - match(NeoLangTokenType.ARRAY_END, errorThrow = true) - - // Allow empty arrays - return if (array != null) NeoLangBlockNode(array) else NeoLangBlockNode.emptyNode() - } - - else -> throw InvalidTokenException("Unexpected token `${token.tokenValue}' typed `${token.tokenType}' for block") - } - } - - /** - * @param attrName Only available when group is a attribute value - */ - private fun blockNonArrayElement(attrName: NeoLangStringNode?): NeoLangBlockNode? { - val token = currentToken ?: throw InvalidTokenException("Unexpected token: null") - - return when (token.tokenType) { - NeoLangTokenType.NUMBER -> { - match(NeoLangTokenType.NUMBER, errorThrow = true) - return NeoLangBlockNode(NeoLangNumberNode(token)) - } - NeoLangTokenType.ID -> { - match(NeoLangTokenType.ID, errorThrow = true) - return NeoLangBlockNode(NeoLangStringNode(token)) - } - NeoLangTokenType.STRING -> { - match(NeoLangTokenType.STRING, errorThrow = true) - return NeoLangBlockNode(NeoLangStringNode(token)) - } - NeoLangTokenType.BRACKET_START -> { - match(NeoLangTokenType.BRACKET_START, errorThrow = true) - val group = group() - match(NeoLangTokenType.BRACKET_END, errorThrow = true) - - // Allow empty blocks - return if (group != null) NeoLangBlockNode(group) else NeoLangBlockNode.emptyNode() - } - else -> null - } - } -} diff --git a/NeoLang/src/main/java/io/neolang/parser/ParseException.kt b/NeoLang/src/main/java/io/neolang/parser/ParseException.kt deleted file mode 100644 index 5d5fa4d..0000000 --- a/NeoLang/src/main/java/io/neolang/parser/ParseException.kt +++ /dev/null @@ -1,6 +0,0 @@ -package io.neolang.parser - -/** - * @author kiva - */ -open class ParseException(message: String) : RuntimeException(message) \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/runtime/context/NeoLangContext.kt b/NeoLang/src/main/java/io/neolang/runtime/context.kt similarity index 92% rename from NeoLang/src/main/java/io/neolang/runtime/context/NeoLangContext.kt rename to NeoLang/src/main/java/io/neolang/runtime/context.kt index f688c46..6abaa96 100644 --- a/NeoLang/src/main/java/io/neolang/runtime/context/NeoLangContext.kt +++ b/NeoLang/src/main/java/io/neolang/runtime/context.kt @@ -1,6 +1,4 @@ -package io.neolang.runtime.context - -import io.neolang.runtime.type.NeoLangValue +package io.neolang.runtime /** * @author kiva diff --git a/NeoLang/src/main/java/io/neolang/runtime/type/NeoLangArrayElement.kt b/NeoLang/src/main/java/io/neolang/runtime/type/NeoLangArrayElement.kt deleted file mode 100644 index 70e9a18..0000000 --- a/NeoLang/src/main/java/io/neolang/runtime/type/NeoLangArrayElement.kt +++ /dev/null @@ -1,18 +0,0 @@ -package io.neolang.runtime.type - -/** - * @author kiva - */ -open class NeoLangArrayElement { - open fun eval(): NeoLangValue { - return NeoLangValue.UNDEFINED - } - - open fun eval(key: String): NeoLangValue { - return NeoLangValue.UNDEFINED - } - - open fun isBlock(): Boolean { - return false - } -} \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/runtime/type/NeoLangValue.kt b/NeoLang/src/main/java/io/neolang/runtime/type/NeoLangValue.kt deleted file mode 100644 index fd406e4..0000000 --- a/NeoLang/src/main/java/io/neolang/runtime/type/NeoLangValue.kt +++ /dev/null @@ -1,30 +0,0 @@ -package io.neolang.runtime.type - -/** - * @author kiva - */ -class NeoLangValue(private val rawValue: Any) { - fun asString(): String { - return rawValue.toString() - } - - fun asNumber(): Double { - if (rawValue is Array<*>) { - return 0.0 - } - - try { - return rawValue.toString().toDouble() - } catch (e: Throwable) { - return 0.0 - } - } - - fun isValid(): Boolean { - return this != UNDEFINED - } - - companion object { - val UNDEFINED = NeoLangValue("") - } -} \ No newline at end of file diff --git a/NeoLang/src/main/java/io/neolang/runtime/type/NeoLangArray.kt b/NeoLang/src/main/java/io/neolang/runtime/types.kt similarity index 67% rename from NeoLang/src/main/java/io/neolang/runtime/type/NeoLangArray.kt rename to NeoLang/src/main/java/io/neolang/runtime/types.kt index eeaecd3..8cc7095 100644 --- a/NeoLang/src/main/java/io/neolang/runtime/type/NeoLangArray.kt +++ b/NeoLang/src/main/java/io/neolang/runtime/types.kt @@ -1,6 +1,4 @@ -package io.neolang.runtime.type - -import io.neolang.runtime.context.NeoLangContext +package io.neolang.runtime /** * @author kiva @@ -59,4 +57,50 @@ class NeoLangArray private constructor( override fun iterator(): Iterator { return elements.iterator() } -} \ No newline at end of file +} + +/** + * @author kiva + */ +open class NeoLangArrayElement { + open fun eval(): NeoLangValue { + return NeoLangValue.UNDEFINED + } + + open fun eval(key: String): NeoLangValue { + return NeoLangValue.UNDEFINED + } + + open fun isBlock(): Boolean { + return false + } +} + +/** + * @author kiva + */ +class NeoLangValue(private val rawValue: Any) { + fun asString(): String { + return rawValue.toString() + } + + fun asNumber(): Double { + if (rawValue is Array<*>) { + return 0.0 + } + + try { + return rawValue.toString().toDouble() + } catch (e: Throwable) { + return 0.0 + } + } + + fun isValid(): Boolean { + return this != UNDEFINED + } + + companion object { + val UNDEFINED = NeoLangValue("") + } +} diff --git a/NeoLang/src/main/java/io/neolang/visitor/DisplayProcessVisitor.kt b/NeoLang/src/main/java/io/neolang/visitor/DisplayProcessVisitor.kt deleted file mode 100644 index c8e854b..0000000 --- a/NeoLang/src/main/java/io/neolang/visitor/DisplayProcessVisitor.kt +++ /dev/null @@ -1,42 +0,0 @@ -package io.neolang.visitor - -import io.neolang.ast.visitor.IVisitorCallbackAdapter -import io.neolang.runtime.context.NeoLangContext -import java.util.* - -/** - * @author kiva - */ -class DisplayProcessVisitor : IVisitorCallbackAdapter() { - private val contextStack = Stack() - - override fun onStart() { - println(">>> Start") - onEnterContext("global") - } - - override fun onFinish() { - while (contextStack.isNotEmpty()) { - onExitContext() - } - println(">>> Finish") - } - - override fun onEnterContext(contextName: String) { - val context = NeoLangContext(contextName) - contextStack.push(context) - println(">>> Entering Context `$contextName'") - } - - override fun onExitContext() { - val context = contextStack.pop() - println(">>> Exiting & Dumping Context ${context.contextName}") - context.getAttributes().entries.forEach { - println(" > [${it.key}]: ${it.value.asString()}") - } - } - - override fun getCurrentContext(): NeoLangContext { - return contextStack.peek() - } -} diff --git a/NeoLang/src/test/java/io/neolang/NeoLangTest.kt b/NeoLang/src/test/java/io/neolang/NeoLangTest.kt deleted file mode 100644 index 6a2151e..0000000 --- a/NeoLang/src/test/java/io/neolang/NeoLangTest.kt +++ /dev/null @@ -1,17 +0,0 @@ -package io.neolang - -import io.neolang.main.Main -import org.junit.Test - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see [Testing documentation](http://d.android.com/tools/testing) - */ -class NeoLangTest { - @Test - fun arrayTest() { - Main.main(arrayOf("NeoLang/example/extra-key.nl")) - } -} - diff --git a/app/src/main/java/io/neoterm/component/NeoInitializer.kt b/app/src/main/java/io/neoterm/component/NeoInitializer.kt deleted file mode 100644 index e30678a..0000000 --- a/app/src/main/java/io/neoterm/component/NeoInitializer.kt +++ /dev/null @@ -1,41 +0,0 @@ -package io.neoterm.component - -import android.content.Context -import io.neoterm.component.codegen.CodeGenComponent -import io.neoterm.component.colorscheme.ColorSchemeComponent -import io.neoterm.component.completion.CompletionComponent -import io.neoterm.component.config.ConfigureComponent -import io.neoterm.component.extrakey.ExtraKeyComponent -import io.neoterm.component.font.FontComponent -import io.neoterm.component.pm.PackageComponent -import io.neoterm.component.profile.ProfileComponent -import io.neoterm.component.session.SessionComponent -import io.neoterm.component.session.ShellProfile -import io.neoterm.component.userscript.UserScriptComponent -import io.neoterm.utils.NLog - -/** - * @author kiva - */ -object NeoInitializer { - fun init(context: Context) { - NLog.init(context) - initComponents() - } - - fun initComponents() { - ComponentManager.registerComponent(ConfigureComponent::class.java) - ComponentManager.registerComponent(CodeGenComponent::class.java) - ComponentManager.registerComponent(ColorSchemeComponent::class.java) - ComponentManager.registerComponent(FontComponent::class.java) - ComponentManager.registerComponent(UserScriptComponent::class.java) - ComponentManager.registerComponent(ExtraKeyComponent::class.java) - ComponentManager.registerComponent(CompletionComponent::class.java) - ComponentManager.registerComponent(PackageComponent::class.java) - ComponentManager.registerComponent(SessionComponent::class.java) - ComponentManager.registerComponent(ProfileComponent::class.java) - - val profileComp = ComponentManager.getComponent() - profileComp.registerProfile(ShellProfile.PROFILE_META_NAME, ShellProfile::class.java) - } -} diff --git a/app/src/main/java/io/neoterm/component/colorscheme/comp.kt b/app/src/main/java/io/neoterm/component/colorscheme/comp.kt index cb6563b..7485a71 100644 --- a/app/src/main/java/io/neoterm/component/colorscheme/comp.kt +++ b/app/src/main/java/io/neoterm/component/colorscheme/comp.kt @@ -1,7 +1,7 @@ package io.neoterm.component.colorscheme import android.content.Context -import io.neolang.visitor.ConfigVisitor +import io.neolang.frontend.ConfigVisitor import io.neoterm.App import io.neoterm.R import io.neoterm.component.ComponentManager diff --git a/app/src/main/java/io/neoterm/component/colorscheme/data.kt b/app/src/main/java/io/neoterm/component/colorscheme/data.kt index 04ec525..16415f9 100644 --- a/app/src/main/java/io/neoterm/component/colorscheme/data.kt +++ b/app/src/main/java/io/neoterm/component/colorscheme/data.kt @@ -1,6 +1,6 @@ package io.neoterm.component.colorscheme -import io.neolang.visitor.ConfigVisitor +import io.neolang.frontend.ConfigVisitor import io.neoterm.backend.TerminalColorScheme import io.neoterm.backend.TerminalColors import io.neoterm.component.ConfigFileBasedObject diff --git a/app/src/main/java/io/neoterm/component/comp.kt b/app/src/main/java/io/neoterm/component/comp.kt index da0e529..be1541a 100644 --- a/app/src/main/java/io/neoterm/component/comp.kt +++ b/app/src/main/java/io/neoterm/component/comp.kt @@ -1,5 +1,18 @@ package io.neoterm.component +import android.content.Context +import io.neoterm.component.codegen.CodeGenComponent +import io.neoterm.component.colorscheme.ColorSchemeComponent +import io.neoterm.component.completion.CompletionComponent +import io.neoterm.component.config.ConfigureComponent +import io.neoterm.component.extrakey.ExtraKeyComponent +import io.neoterm.component.font.FontComponent +import io.neoterm.component.pm.PackageComponent +import io.neoterm.component.profile.ProfileComponent +import io.neoterm.component.session.SessionComponent +import io.neoterm.component.session.ShellProfile +import io.neoterm.component.userscript.UserScriptComponent +import io.neoterm.utils.NLog import java.util.concurrent.ConcurrentHashMap interface NeoComponent { @@ -50,3 +63,28 @@ object ComponentManager { class ComponentDuplicateException(serviceName: String) : RuntimeException("Service $serviceName duplicate") class ComponentNotFoundException(serviceName: String) : RuntimeException("Component `$serviceName' not found") +/** + * @author kiva + */ +object NeoInitializer { + fun init(context: Context) { + NLog.init(context) + initComponents() + } + + fun initComponents() { + ComponentManager.registerComponent(ConfigureComponent::class.java) + ComponentManager.registerComponent(CodeGenComponent::class.java) + ComponentManager.registerComponent(ColorSchemeComponent::class.java) + ComponentManager.registerComponent(FontComponent::class.java) + ComponentManager.registerComponent(UserScriptComponent::class.java) + ComponentManager.registerComponent(ExtraKeyComponent::class.java) + ComponentManager.registerComponent(CompletionComponent::class.java) + ComponentManager.registerComponent(PackageComponent::class.java) + ComponentManager.registerComponent(SessionComponent::class.java) + ComponentManager.registerComponent(ProfileComponent::class.java) + + val profileComp = ComponentManager.getComponent() + profileComp.registerProfile(ShellProfile.PROFILE_META_NAME, ShellProfile::class.java) + } +} diff --git a/app/src/main/java/io/neoterm/component/config/comp.kt b/app/src/main/java/io/neoterm/component/config/comp.kt index d44c728..af7c1d3 100644 --- a/app/src/main/java/io/neoterm/component/config/comp.kt +++ b/app/src/main/java/io/neoterm/component/config/comp.kt @@ -6,8 +6,8 @@ import android.preference.PreferenceManager import android.system.ErrnoException import android.system.Os import android.util.TypedValue -import io.neolang.parser.NeoLangParser -import io.neolang.visitor.ConfigVisitor +import io.neolang.frontend.ConfigVisitor +import io.neolang.frontend.NeoLangParser import io.neoterm.App import io.neoterm.R import io.neoterm.backend.TerminalSession diff --git a/app/src/main/java/io/neoterm/component/config/loaders.kt b/app/src/main/java/io/neoterm/component/config/loaders.kt index 39c610a..f9e93f7 100644 --- a/app/src/main/java/io/neoterm/component/config/loaders.kt +++ b/app/src/main/java/io/neoterm/component/config/loaders.kt @@ -1,7 +1,7 @@ package io.neoterm.component.config -import io.neolang.runtime.type.NeoLangValue -import io.neolang.visitor.ConfigVisitor +import io.neolang.frontend.ConfigVisitor +import io.neolang.runtime.NeoLangValue import io.neoterm.component.colorscheme.NeoColorScheme import io.neoterm.component.extrakey.NeoExtraKey import io.neoterm.frontend.session.view.extrakey.TextButton diff --git a/app/src/main/java/io/neoterm/component/data.kt b/app/src/main/java/io/neoterm/component/data.kt index 3dc2441..76161cc 100644 --- a/app/src/main/java/io/neoterm/component/data.kt +++ b/app/src/main/java/io/neoterm/component/data.kt @@ -1,6 +1,6 @@ package io.neoterm.component -import io.neolang.visitor.ConfigVisitor +import io.neolang.frontend.ConfigVisitor import io.neoterm.component.config.ConfigureComponent import io.neoterm.utils.NLog import java.io.File diff --git a/app/src/main/java/io/neoterm/component/extrakey/comp.kt b/app/src/main/java/io/neoterm/component/extrakey/comp.kt index 93478d7..67dc829 100644 --- a/app/src/main/java/io/neoterm/component/extrakey/comp.kt +++ b/app/src/main/java/io/neoterm/component/extrakey/comp.kt @@ -1,7 +1,7 @@ package io.neoterm.component.extrakey import android.content.Context -import io.neolang.visitor.ConfigVisitor +import io.neolang.frontend.ConfigVisitor import io.neoterm.App import io.neoterm.component.ConfigFileBasedComponent import io.neoterm.component.config.NeoTermPath diff --git a/app/src/main/java/io/neoterm/component/extrakey/data.kt b/app/src/main/java/io/neoterm/component/extrakey/data.kt index df09a6f..a248ed3 100644 --- a/app/src/main/java/io/neoterm/component/extrakey/data.kt +++ b/app/src/main/java/io/neoterm/component/extrakey/data.kt @@ -1,6 +1,6 @@ package io.neoterm.component.extrakey -import io.neolang.visitor.ConfigVisitor +import io.neolang.frontend.ConfigVisitor import io.neoterm.component.ConfigFileBasedObject import io.neoterm.frontend.session.view.extrakey.ExtraKeysView import io.neoterm.frontend.session.view.extrakey.IExtraButton diff --git a/app/src/main/java/io/neoterm/component/profile/comp.kt b/app/src/main/java/io/neoterm/component/profile/comp.kt index a283c22..3ae7f3c 100644 --- a/app/src/main/java/io/neoterm/component/profile/comp.kt +++ b/app/src/main/java/io/neoterm/component/profile/comp.kt @@ -1,6 +1,6 @@ package io.neoterm.component.profile -import io.neolang.visitor.ConfigVisitor +import io.neolang.frontend.ConfigVisitor import io.neoterm.component.ConfigFileBasedComponent import io.neoterm.component.config.NeoTermPath import io.neoterm.utils.NLog diff --git a/app/src/main/java/io/neoterm/component/profile/data.kt b/app/src/main/java/io/neoterm/component/profile/data.kt index 4f53a06..41f74f2 100644 --- a/app/src/main/java/io/neoterm/component/profile/data.kt +++ b/app/src/main/java/io/neoterm/component/profile/data.kt @@ -1,6 +1,6 @@ package io.neoterm.component.profile -import io.neolang.visitor.ConfigVisitor +import io.neolang.frontend.ConfigVisitor import io.neoterm.component.ComponentManager import io.neoterm.component.ConfigFileBasedObject import io.neoterm.component.codegen.CodeGenObject diff --git a/app/src/main/java/io/neoterm/component/session/shell.kt b/app/src/main/java/io/neoterm/component/session/shell.kt index 411b9b2..1ffd10c 100644 --- a/app/src/main/java/io/neoterm/component/session/shell.kt +++ b/app/src/main/java/io/neoterm/component/session/shell.kt @@ -1,7 +1,7 @@ package io.neoterm.component.session import android.content.Context -import io.neolang.visitor.ConfigVisitor +import io.neolang.frontend.ConfigVisitor import io.neoterm.App import io.neoterm.R import io.neoterm.backend.TerminalSession