Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Czechtina v0.1.5.1 (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ten-James authored Oct 23, 2023
1 parent 1893c06 commit f58d3cd
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ java -jar czechtina.jar build/ukol.cz --no-compile --fpeterek --friendly --set-d

## TODO

- invalidate "1 je 3"
- std lib
- string anotation
- better cli
- file structure
- rest of loop features.
- structures
- range definition
- check validation of return type

## Credits

Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/AST/ASTFunctionCallNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/AST/ASTFunctionNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/AST/ASTOperandNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()}"
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/AST/ASTUnaryNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()}"
Expand Down
8 changes: 3 additions & 5 deletions src/main/kotlin/compiler/Compiler.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -179,7 +178,6 @@ object Compiler {
var cCode = tree.toC()



while (isAnyUsedFunctionUndefined()) {
for (function in definedFunctions){
val fce = function.value
Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/compiler/DefinedFunction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefinedType>): Int {
for (variant in variants) {
if (variant.params.size != params.size){
Expand Down Expand Up @@ -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<DefinedType>, var defined:Boolean = true, val enableArgs: Boolean = false, val virtual: Boolean = false) {
class DefinedFunctionVariant(val translatedName: String, val params: List<DefinedType>, 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"
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/compiler/DefinedType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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"
}
6 changes: 3 additions & 3 deletions src/main/kotlin/czechtina/lesana/function.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fun LesanaBuilder<ASTNode>.inlineFunction(
)
{ (funName, varDef, _, line) ->
ASTFunctionNode(
ASTUnaryNode(ASTUnaryTypes.TYPE, line.getType()),
ASTUnaryNode(ASTUnaryTypes.TYPE,data="", expressionType = line.getType()),
funName,
listOf(varDef),
ASTUnaryNode(
Expand All @@ -41,7 +41,7 @@ fun LesanaBuilder<ASTNode>.inlineFunction(
)
{ (funName, varDefs, _, line) ->
ASTFunctionNode(
ASTUnaryNode(ASTUnaryTypes.TYPE, line.getType()),
ASTUnaryNode(ASTUnaryTypes.TYPE,data="", expressionType = line.getType()),
funName,
varDefs.nodes,
ASTUnaryNode(
Expand All @@ -59,7 +59,7 @@ fun LesanaBuilder<ASTNode>.inlineFunction(
)
{ (funName, _, line) ->
ASTFunctionNode(
ASTUnaryNode(ASTUnaryTypes.TYPE, line.getType()),
ASTUnaryNode(ASTUnaryTypes.TYPE,data="", expressionType = line.getType()),
funName,
listOf(),
ASTUnaryNode(
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/czechtina/regex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fun cTypeFromCzechtina (czechType: String): String {
if (czechType == cValue)
return cValue
}
return ""
throw Exception("Unknown type $czechType")
}


Expand All @@ -199,7 +199,7 @@ fun czTypeFromCzechtina (czechType: String): String {
if (czechType == cValue)
return cValue
}
return ""
throw Exception("Unknown type $czechType")
}

fun cAndCzechtinaRegex (list: List<GrammarToken>): String = list.joinToString("|") { czechtina[it]!! + "|" + C[it]!! }
Expand Down

0 comments on commit f58d3cd

Please sign in to comment.