diff --git a/README.md b/README.md index 5bc74c5..d744b12 100644 --- a/README.md +++ b/README.md @@ -254,7 +254,6 @@ java -jar czechtina.jar build/ukol.cz --no-compile --fpeterek --friendly --set-d ## TODO -- invalidate "1 je 3" - std lib - string anotation - better cli @@ -262,6 +261,7 @@ java -jar czechtina.jar build/ukol.cz --no-compile --fpeterek --friendly --set-d - rest of loop features. - structures - range definition +- check validation of return type ## Credits diff --git a/src/main/kotlin/AST/ASTFunctionCallNode.kt b/src/main/kotlin/AST/ASTFunctionCallNode.kt index fee14f1..a360122 100644 --- a/src/main/kotlin/AST/ASTFunctionCallNode.kt +++ b/src/main/kotlin/AST/ASTFunctionCallNode.kt @@ -29,8 +29,9 @@ class ASTFunctionCallNode : ASTVariableNode { return (params!! as ASTVariableNode).getType().toDynamic() if (function.toC() == "const") return (params!! as ASTVariableNode).getType().toConst() - if (Compiler.definedFunctions[function.toC()]!!.validateParams(paramsTypes) != -1) - return Compiler.definedFunctions[function.toC()]!!.returnType + val variantIndex = Compiler.definedFunctions[function.toC()]!!.validateParams(paramsTypes) + if (variantIndex != -1) + return Compiler.definedFunctions[function.toC()]!!.getReturnType(variantIndex) } return DefinedType("none") } diff --git a/src/main/kotlin/AST/ASTFunctionNode.kt b/src/main/kotlin/AST/ASTFunctionNode.kt index cb188da..7dfe5f0 100644 --- a/src/main/kotlin/AST/ASTFunctionNode.kt +++ b/src/main/kotlin/AST/ASTFunctionNode.kt @@ -61,11 +61,11 @@ class ASTFunctionNode : ASTNode { if (Compiler.definedFunctions.containsKey(name!!)) { val newName = "${name}_v${Compiler.definedFunctions[name!!]!!.variants.size}" - Compiler.definedFunctions[name!!]!!.variants.add(compiler.DefinedFunctionVariant(newName, paramsTypes)) + Compiler.definedFunctions[name!!]!!.variants.add(compiler.DefinedFunctionVariant(newName, paramsTypes, returnType = type.getType())) name = newName; } else - Compiler.definedFunctions += mapOf(name!! to compiler.DefinedFunction(name!!, compiler.DefinedType(type.toC()), listOf(compiler.DefinedFunctionVariant(name!!, paramsTypes)), virtual = false)) + Compiler.definedFunctions += mapOf(name!! to compiler.DefinedFunction(name!!, type.getType(), listOf(compiler.DefinedFunctionVariant(name!!, paramsTypes, returnType = type.getType())), virtual = false)) if (paramsTypes.any{it.isTemplate()}) return "//${name}_Declaration_CZECHTINA ANCHOR\n" diff --git a/src/main/kotlin/AST/ASTOperandNode.kt b/src/main/kotlin/AST/ASTOperandNode.kt index 95dcdb7..83c79a3 100644 --- a/src/main/kotlin/AST/ASTOperandNode.kt +++ b/src/main/kotlin/AST/ASTOperandNode.kt @@ -29,7 +29,8 @@ class ASTOperandNode : ASTTypedNode { return ASTOperandNode(operand, left.copy(), right.copy()) } - override fun toC(): String = when (operand) { - else -> "${left?.toC()} ${Compiler.typeFromCzechtina(operand)} ${right?.toC()}" + override fun toC(): String { + expType = Compiler.calcBinaryType(left, right, operand) + return "${left?.toC()} ${Compiler.typeFromCzechtina(operand)} ${right?.toC()}" } } \ No newline at end of file diff --git a/src/main/kotlin/AST/ASTUnaryNode.kt b/src/main/kotlin/AST/ASTUnaryNode.kt index d8dcb58..5463fde 100644 --- a/src/main/kotlin/AST/ASTUnaryNode.kt +++ b/src/main/kotlin/AST/ASTUnaryNode.kt @@ -58,7 +58,7 @@ class ASTUnaryNode : ASTTypedNode { override fun toC(): String = when (type) { ASTUnaryTypes.LITERAL -> data.toString() ASTUnaryTypes.VARIABLE -> data.toString() - ASTUnaryTypes.TYPE -> if (getType().isTemplate()) getType().typeString else Compiler.typeFromCzechtina(data.toString()) + ASTUnaryTypes.TYPE -> if (getType().isTemplate()) getType().typeString else getType().toC() ASTUnaryTypes.TYPE_POINTER -> "${(data as ASTNode).toC()}*" ASTUnaryTypes.RETURN -> "${Compiler.grammar[GrammarToken.KEYWORD_RETURN]} ${(data as ASTNode).toC()}" ASTUnaryTypes.IMPORT -> "//xd ${data.toString()}" diff --git a/src/main/kotlin/compiler/Compiler.kt b/src/main/kotlin/compiler/Compiler.kt index e1da430..6ff2105 100644 --- a/src/main/kotlin/compiler/Compiler.kt +++ b/src/main/kotlin/compiler/Compiler.kt @@ -1,9 +1,6 @@ package compiler -import AST.ASTListNode -import AST.ASTNode -import AST.ASTProgramNode -import AST.ASTTypedNode +import AST.* import cz.j_jzk.klang.input.InputFactory import czechtina.* import czechtina.header.createCzechtinaDefineFile @@ -120,6 +117,8 @@ object Compiler { if (operand == "=") { if (left.getType().isConst && isParsed) throw Exception("Cannot change const type of variable ${left}") + if (left is ASTVariableNode) + left.addType(right.getType()) return DefinedType(right.getType()) } @@ -179,7 +178,6 @@ object Compiler { var cCode = tree.toC() - while (isAnyUsedFunctionUndefined()) { for (function in definedFunctions){ val fce = function.value diff --git a/src/main/kotlin/compiler/DefinedFunction.kt b/src/main/kotlin/compiler/DefinedFunction.kt index f4c49cf..92b9e27 100644 --- a/src/main/kotlin/compiler/DefinedFunction.kt +++ b/src/main/kotlin/compiler/DefinedFunction.kt @@ -7,6 +7,10 @@ class DefinedFunction(val name: String,val returnType: DefinedType, val virtual: this.variants.addAll(variants) } + fun getReturnType(variantIndex : Int): DefinedType { + return variants[variantIndex].returnType ?: returnType + } + fun validateParams(params: List): Int { for (variant in variants) { if (variant.params.size != params.size){ @@ -59,7 +63,7 @@ class DefinedFunction(val name: String,val returnType: DefinedType, val virtual: override fun toString(): String = "\n$name: $returnType $virtual (\n\t${variants.joinToString("\n\t")}\n)" } -class DefinedFunctionVariant(val translatedName: String, val params: List, var defined:Boolean = true, val enableArgs: Boolean = false, val virtual: Boolean = false) { +class DefinedFunctionVariant(val translatedName: String, val params: List, val returnType: DefinedType? = null, var defined:Boolean = true, val enableArgs: Boolean = false, val virtual: Boolean = false) { var timeUsed: Int = 0 override fun toString(): String = "$translatedName(${params.joinToString(",")}): $timeUsed $defined" diff --git a/src/main/kotlin/compiler/DefinedType.kt b/src/main/kotlin/compiler/DefinedType.kt index c88800d..7658516 100644 --- a/src/main/kotlin/compiler/DefinedType.kt +++ b/src/main/kotlin/compiler/DefinedType.kt @@ -18,6 +18,12 @@ class DefinedType { this.isConst = DT.isConst } + fun toC(): String { + if (isPointer() || isDynamic()) + return "${getPrimitive()} *" + return getPrimitive() + } + fun toHeap(): DefinedType { return DefinedType(typeString, true) } @@ -55,6 +61,14 @@ class DefinedType { return typeString } + fun getPrimitive(): String { + if (isTemplate()) + return getTemplate() + if (typeString.contains("-")) + return typeString.split("-")[1] + return typeString + } + override fun toString(): String = "$typeString - $isHeap - $isConst" } \ No newline at end of file diff --git a/src/main/kotlin/czechtina/lesana/function.kt b/src/main/kotlin/czechtina/lesana/function.kt index 1572761..0d64395 100644 --- a/src/main/kotlin/czechtina/lesana/function.kt +++ b/src/main/kotlin/czechtina/lesana/function.kt @@ -23,7 +23,7 @@ fun LesanaBuilder.inlineFunction( ) { (funName, varDef, _, line) -> ASTFunctionNode( - ASTUnaryNode(ASTUnaryTypes.TYPE, line.getType()), + ASTUnaryNode(ASTUnaryTypes.TYPE,data="", expressionType = line.getType()), funName, listOf(varDef), ASTUnaryNode( @@ -41,7 +41,7 @@ fun LesanaBuilder.inlineFunction( ) { (funName, varDefs, _, line) -> ASTFunctionNode( - ASTUnaryNode(ASTUnaryTypes.TYPE, line.getType()), + ASTUnaryNode(ASTUnaryTypes.TYPE,data="", expressionType = line.getType()), funName, varDefs.nodes, ASTUnaryNode( @@ -59,7 +59,7 @@ fun LesanaBuilder.inlineFunction( ) { (funName, _, line) -> ASTFunctionNode( - ASTUnaryNode(ASTUnaryTypes.TYPE, line.getType()), + ASTUnaryNode(ASTUnaryTypes.TYPE,data="", expressionType = line.getType()), funName, listOf(), ASTUnaryNode( diff --git a/src/main/kotlin/czechtina/regex.kt b/src/main/kotlin/czechtina/regex.kt index 3a1c909..f977dcc 100644 --- a/src/main/kotlin/czechtina/regex.kt +++ b/src/main/kotlin/czechtina/regex.kt @@ -183,7 +183,7 @@ fun cTypeFromCzechtina (czechType: String): String { if (czechType == cValue) return cValue } - return "" + throw Exception("Unknown type $czechType") } @@ -199,7 +199,7 @@ fun czTypeFromCzechtina (czechType: String): String { if (czechType == cValue) return cValue } - return "" + throw Exception("Unknown type $czechType") } fun cAndCzechtinaRegex (list: List): String = list.joinToString("|") { czechtina[it]!! + "|" + C[it]!! }