diff --git a/examples/main.chsp b/examples/main.chsp index 214de86..f6c7f11 100644 --- a/examples/main.chsp +++ b/examples/main.chsp @@ -10,6 +10,10 @@ init { } val dingus = "dongusify" var dongus = "dingusify" + + dongus = dongus + "dabingus" + dongus = "heheheha" + } onRecieving egg * * { diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 54208bb..f8bde43 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -30,6 +30,24 @@ fun splitList(input: String): List { return result } +object log { + var isLogging = 0 + fun log(input: String) { + if (isLogging == 1) { + println(input) + } + } + fun error(input: String) { + println("\u001b[31m$input\u001b[0m") + } + fun note(input: String) { + println("\u001b[32m$input\u001b[0m") + } + fun info(input: String) { + println("\u001b[33m$input\u001b[0m") + } +} + object files { val outputIndexJS = File("out/index.js") val outputMessageJS = File("out/message.js}") @@ -37,8 +55,15 @@ object files { val generatedIndexJS = mutableListOf() val generatedMessageJS = mutableListOf() val generatedHTML = mutableListOf() - + + var errorStatus = 0 + fun write() { + if (errorStatus != 0) { + log.error("\u001b[1mTranspile aborted due to code errors. If you believe this is a mistake please make a bug report. Run again with verbose mode (-v) and upload the log.") + return + } + log.note("Finished! Writing...") outputIndexJS.writeText("") outputMessageJS.writeText("") outputHTML.writeText("") @@ -52,30 +77,51 @@ object files { for (item in generatedIndexJS) { outputIndexJS.appendText("$item\n") } + log.note("All done! Your files are in ./out/") } } fun testValidity(input: List, num: Int): Int { - println("Testing validity of ${input.toString()}...") - if (input[1] != "=") { - println("""Error at line $num: Define your variable with "=", not "${input[1]}"""") - return 1 + val validOperations = listOf("=", "++") + val declarations = listOf("val", "var") + val initialising = declarations.contains(input[0]) + + log.log("Testing validity of ${input.toString()}...") + log.log("Testing a newly initialised variable is $initialising") + + if (initialising) { + if (input[2] != "=") { + log.error("""Error at line $num: Define your variable with "=" not "${input[2]}"""") + files.errorStatus = 1 + return 1 + } + } else { + if (!validOperations.contains(input[1])) { + log.error("""Error at line $num: Invalid operation "${input[1]}". Valid operations are: ${validOperations.joinToString(", ")}""") + files.errorStatus = 1 + return 1 + } } + return 0 } fun main(args: Array) { - println("Chookspeak Language Processor") - println("Let's Go!") + log.note("Starting transpiler...") + try { + if (args[1] == "-v") { + log.isLogging = 1 + } + } catch (e: Exception) {} var fileLocation: File try { fileLocation = File(args[0]) } catch (e: Exception) { - println("Please specify a file") + log.error("Error: Please specify a file") return } if (!fileLocation.exists()) { - println("Your Chookspeak file does not exist.") + log.error("Error: Your Chookspeak file does not exist.") return } val file = fileLocation.readLines() @@ -84,13 +130,15 @@ fun main(args: Array) { var eggName = "" val status = mutableListOf() val variables = mutableListOf() + val keywords = listOf("init", "sendMessage", "html", "log", "js", "val", "var", "}") + log.note("File loaded!") for (line in file) { val lineArgsUnfiltered = splitList(line) val lineArgs = lineArgsUnfiltered.filterNot { it == "" } if (lineArgs.size == 0) { continue } - println("line ${lineArgs.toString()}") + log.log("line ${lineArgs.toString()}") var slc = 0 for (item in lineArgs) { if (item == "//") { @@ -105,7 +153,7 @@ fun main(args: Array) { if (commenting == 0) { if (lineCounter == 1) { if (lineArgs[0] != "egg") { - println("Please specify your egg on your first line of code.") + log.error("Error: Please specify your egg on your first line of code.") return } else { eggName = lineArgs[1] @@ -117,12 +165,12 @@ fun main(args: Array) { try { status.removeLast() } catch (e: Exception) {} - println("status ${status.toString()}") + log.log("status ${status.toString()}") } if (lineArgs.size > 1) { if (lineArgs[1] == "{") { status.add(lineArgs[0]) - println("status ${status.toString()}") + log.log("status ${status.toString()}") } } if (status == listOf("init")) { @@ -137,7 +185,8 @@ fun main(args: Array) { if (lineArgs[2] == "=") { files.generatedIndexJS.add("const ${lineArgs[1]} = ${lineArgs[3]};") } else { - println("Error on line $lineCounter: Define your value with '=', not ${lineArgs[2]}") + log.error("Error on line $lineCounter: Define your value with '=', not ${lineArgs[2]}") + files.errorStatus = 1 } } if (lineArgs[0] == "var") { @@ -146,13 +195,17 @@ fun main(args: Array) { files.generatedIndexJS.add("let ${lineArgs[1]} = ${lineArgs[3]};") variables.add(lineArgs[1]) } else { - println("Error on line $lineCounter: Define your variable with '=', not ${lineArgs[2]}") + log.error("Error on line $lineCounter: Define your variable with '=', not ${lineArgs[2]}") + files.errorStatus = 1 } } if (variables.contains(lineArgs[0])) { - if (lineArgs[1] == "=") { - //if () + if (testValidity(lineArgs, lineCounter) == 0) { + files.generatedIndexJS.add(lineArgs.joinToString(" ")) } + } else if (!keywords.contains(lineArgs[0])){ + log.info("Info: On line $lineCounter, I don't know how to ${lineArgs[0]} yet. Maybe you forgot to define a variable? Or if the feature isn't implemented yet, check back later.") + log.info("I'll skip that line. Your project will still be outputted, however expect bugs.") } } if (status == listOf("init", "html") && lineArgs[0] != "html") {