From de92740e8703a45753156e3ffe48d130e40879de Mon Sep 17 00:00:00 2001 From: meisl Date: Sat, 17 Jul 2021 21:29:01 +0200 Subject: [PATCH] * simple refactoring: move IStringEncoding, IMemSizer and IBuiltinFunctions to files of their own, also ext method Number.toHex to file compilerAst/src/prog8/ast/Extensions.kt. Moved the other interfaces that are indeed closely related to Node to the top of AstToplevel.kt. *This is really ONLY moving text around*, so it's easier to find things. Nothing else. --- compilerAst/src/prog8/ast/AstToplevel.kt | 116 +++++++----------- compilerAst/src/prog8/ast/Extensions.kt | 19 +++ .../src/prog8/ast/IBuiltinFunctions.kt | 13 ++ compilerAst/src/prog8/ast/IMemSizer.kt | 7 ++ compilerAst/src/prog8/ast/IStringEncoding.kt | 6 + 5 files changed, 87 insertions(+), 74 deletions(-) create mode 100644 compilerAst/src/prog8/ast/Extensions.kt create mode 100644 compilerAst/src/prog8/ast/IBuiltinFunctions.kt create mode 100644 compilerAst/src/prog8/ast/IMemSizer.kt create mode 100644 compilerAst/src/prog8/ast/IStringEncoding.kt diff --git a/compilerAst/src/prog8/ast/AstToplevel.kt b/compilerAst/src/prog8/ast/AstToplevel.kt index dfbf16310..4c67f054c 100644 --- a/compilerAst/src/prog8/ast/AstToplevel.kt +++ b/compilerAst/src/prog8/ast/AstToplevel.kt @@ -6,54 +6,12 @@ import prog8.ast.statements.* import prog8.ast.walk.AstWalker import prog8.ast.walk.IAstVisitor import prog8.parser.SourceCode -import kotlin.math.abs const val internedStringsModuleName = "prog8_interned_strings" -interface IStringEncoding { - fun encodeString(str: String, altEncoding: Boolean): List - fun decodeString(bytes: List, altEncoding: Boolean): String -} -interface Node { - val position: Position - var parent: Node // will be linked correctly later (late init) - fun linkParents(parent: Node) - - fun definingModule(): Module { - if(this is Module) - return this - return findParentNode(this)!! - } - - fun definingSubroutine(): Subroutine? = findParentNode(this) - - fun definingScope(): INameScope { - val scope = findParentNode(this) - if(scope!=null) { - return scope - } - if(this is Label && this.name.startsWith("builtin::")) { - return BuiltinFunctionScopePlaceholder - } - if(this is GlobalNamespace) - return this - throw FatalAstException("scope missing from $this") - } - - fun definingBlock(): Block { - if(this is Block) - return this - return findParentNode(this)!! - } - - fun containingStatement(): Statement { - if(this is Statement) - return this - return findParentNode(this)!! - } - - fun replaceChildNode(node: Node, replacement: Node) +interface IAssignable { + // just a tag for now } interface IFunctionCall { @@ -61,7 +19,6 @@ interface IFunctionCall { var args: MutableList } - interface INameScope { val name: String val position: Position @@ -227,25 +184,51 @@ interface INameScope { } } -interface IAssignable { - // just a tag for now + +interface Node { + val position: Position + var parent: Node // will be linked correctly later (late init) + fun linkParents(parent: Node) + + fun definingModule(): Module { + if(this is Module) + return this + return findParentNode(this)!! + } + + fun definingSubroutine(): Subroutine? = findParentNode(this) + + fun definingScope(): INameScope { + val scope = findParentNode(this) + if(scope!=null) { + return scope + } + if(this is Label && this.name.startsWith("builtin::")) { + return BuiltinFunctionScopePlaceholder + } + if(this is GlobalNamespace) + return this + throw FatalAstException("scope missing from $this") + } + + fun definingBlock(): Block { + if(this is Block) + return this + return findParentNode(this)!! + } + + fun containingStatement(): Statement { + if(this is Statement) + return this + return findParentNode(this)!! + } + + fun replaceChildNode(node: Node, replacement: Node) } -interface IMemSizer { - fun memorySize(dt: DataType): Int -} - -interface IBuiltinFunctions { - val names: Set - val purefunctionNames: Set - fun constValue(name: String, args: List, position: Position, memsizer: IMemSizer): NumericLiteralValue? - fun returnType(name: String, args: MutableList): InferredTypes.InferredType -} /*********** Everything starts from here, the Program; zero or more modules *************/ - - class Program(val name: String, val modules: MutableList, val builtinFunctions: IBuiltinFunctions, @@ -419,18 +402,3 @@ object BuiltinFunctionScopePlaceholder : INameScope { } -fun Number.toHex(): String { - // 0..15 -> "0".."15" - // 16..255 -> "$10".."$ff" - // 256..65536 -> "$0100".."$ffff" - // negative values are prefixed with '-'. - val integer = this.toInt() - if(integer<0) - return '-' + abs(integer).toHex() - return when (integer) { - in 0 until 16 -> integer.toString() - in 0 until 0x100 -> "$"+integer.toString(16).padStart(2,'0') - in 0 until 0x10000 -> "$"+integer.toString(16).padStart(4,'0') - else -> throw IllegalArgumentException("number too large for 16 bits $this") - } -} diff --git a/compilerAst/src/prog8/ast/Extensions.kt b/compilerAst/src/prog8/ast/Extensions.kt new file mode 100644 index 000000000..5d6bf84eb --- /dev/null +++ b/compilerAst/src/prog8/ast/Extensions.kt @@ -0,0 +1,19 @@ +package prog8.ast + +import kotlin.math.abs + +fun Number.toHex(): String { + // 0..15 -> "0".."15" + // 16..255 -> "$10".."$ff" + // 256..65536 -> "$0100".."$ffff" + // negative values are prefixed with '-'. + val integer = this.toInt() + if(integer<0) + return '-' + abs(integer).toHex() + return when (integer) { + in 0 until 16 -> integer.toString() + in 0 until 0x100 -> "$"+integer.toString(16).padStart(2,'0') + in 0 until 0x10000 -> "$"+integer.toString(16).padStart(4,'0') + else -> throw IllegalArgumentException("number too large for 16 bits $this") + } +} \ No newline at end of file diff --git a/compilerAst/src/prog8/ast/IBuiltinFunctions.kt b/compilerAst/src/prog8/ast/IBuiltinFunctions.kt new file mode 100644 index 000000000..b9d19cf58 --- /dev/null +++ b/compilerAst/src/prog8/ast/IBuiltinFunctions.kt @@ -0,0 +1,13 @@ +package prog8.ast + +import prog8.ast.base.Position +import prog8.ast.expressions.Expression +import prog8.ast.expressions.InferredTypes +import prog8.ast.expressions.NumericLiteralValue + +interface IBuiltinFunctions { + val names: Set + val purefunctionNames: Set + fun constValue(name: String, args: List, position: Position, memsizer: IMemSizer): NumericLiteralValue? + fun returnType(name: String, args: MutableList): InferredTypes.InferredType +} \ No newline at end of file diff --git a/compilerAst/src/prog8/ast/IMemSizer.kt b/compilerAst/src/prog8/ast/IMemSizer.kt new file mode 100644 index 000000000..9ba6767e3 --- /dev/null +++ b/compilerAst/src/prog8/ast/IMemSizer.kt @@ -0,0 +1,7 @@ +package prog8.ast + +import prog8.ast.base.DataType + +interface IMemSizer { + fun memorySize(dt: DataType): Int +} \ No newline at end of file diff --git a/compilerAst/src/prog8/ast/IStringEncoding.kt b/compilerAst/src/prog8/ast/IStringEncoding.kt new file mode 100644 index 000000000..8296d2988 --- /dev/null +++ b/compilerAst/src/prog8/ast/IStringEncoding.kt @@ -0,0 +1,6 @@ +package prog8.ast + +interface IStringEncoding { + fun encodeString(str: String, altEncoding: Boolean): List + fun decodeString(bytes: List, altEncoding: Boolean): String +} \ No newline at end of file