diff --git a/examples/main.chsp b/examples/main.chsp index 97fdfe6..214de86 100644 --- a/examples/main.chsp +++ b/examples/main.chsp @@ -1,4 +1,3 @@ -// comment lmao egg notepad init { sendMessage "Hello there!" @@ -9,6 +8,8 @@ init { js { alert("this is an alert! i repeat - this is an alert!"); } + val dingus = "dongusify" + var dongus = "dingusify" } onRecieving egg * * { diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 5f68c0a..54208bb 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -30,6 +30,40 @@ fun splitList(input: String): List { return result } +object files { + val outputIndexJS = File("out/index.js") + val outputMessageJS = File("out/message.js}") + val outputHTML = File("out/index.html") + val generatedIndexJS = mutableListOf() + val generatedMessageJS = mutableListOf() + val generatedHTML = mutableListOf() + + fun write() { + outputIndexJS.writeText("") + outputMessageJS.writeText("") + outputHTML.writeText("") + + for (item in generatedHTML) { + outputHTML.appendText("$item\n") + } + for (item in generatedMessageJS) { + outputMessageJS.appendText("$item\n") + } + for (item in generatedIndexJS) { + outputIndexJS.appendText("$item\n") + } + } +} + +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 + } + return 0 +} + fun main(args: Array) { println("Chookspeak Language Processor") println("Let's Go!") @@ -44,17 +78,12 @@ fun main(args: Array) { println("Your Chookspeak file does not exist.") return } - val outputIndexJS = File("out/index.js") - val outputMessageJS = File("out/message.js}") - val outputHTML = File("out/index.html") - outputIndexJS.writeText("") - outputMessageJS.writeText("") - outputHTML.writeText("") val file = fileLocation.readLines() var lineCounter = 1 var commenting = 0 var eggName = "" - var status = mutableListOf() + val status = mutableListOf() + val variables = mutableListOf() for (line in file) { val lineArgsUnfiltered = splitList(line) val lineArgs = lineArgsUnfiltered.filterNot { it == "" } @@ -80,8 +109,8 @@ fun main(args: Array) { return } else { eggName = lineArgs[1] - outputIndexJS.appendText("function egg$eggName() {\n") - outputHTML.appendText("""
${"\n"}""") + files.generatedIndexJS.add("function egg$eggName() {") + files.generatedHTML.add("""
""") } } if (lineArgs[0] == "}") { @@ -98,18 +127,39 @@ fun main(args: Array) { } if (status == listOf("init")) { if (lineArgs[0] == "log") { - outputIndexJS.appendText("console.log(${lineArgs[1]});\n") + files.generatedIndexJS.add("console.log(${lineArgs[1]});") } if (lineArgs[0] == "sendMessage") { - outputIndexJS.appendText("""const egg${eggName}Message = { "type" = "egg-$eggName", "username" = username, "token" = md5(password), "content": ${lineArgs[1]}};${"\n"}""") - outputIndexJS.appendText("ws.send(JSON.stringify(egg${eggName}Message))\n") + files.generatedIndexJS.add("""const egg${eggName}Message = { "type" = "egg-$eggName", "username" = username, "token" = md5(password), "content": ${lineArgs[1]}};${""}""") + files.generatedIndexJS.add("ws.send(JSON.stringify(egg${eggName}Message))") + } + if (lineArgs[0] == "val") { + if (lineArgs[2] == "=") { + files.generatedIndexJS.add("const ${lineArgs[1]} = ${lineArgs[3]};") + } else { + println("Error on line $lineCounter: Define your value with '=', not ${lineArgs[2]}") + } + } + if (lineArgs[0] == "var") { + if (lineArgs[2] == "=") { + testValidity(lineArgs, lineCounter) + 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]}") + } + } + if (variables.contains(lineArgs[0])) { + if (lineArgs[1] == "=") { + //if () + } } } if (status == listOf("init", "html") && lineArgs[0] != "html") { - outputHTML.appendText("${lineArgs.joinToString(" ")}\n") + files.generatedHTML.add("${lineArgs.joinToString(" ")}") } if (status == listOf("init", "js") && lineArgs[0] != "js") { - outputIndexJS.appendText("${lineArgs.joinToString(" ")}\n") + files.generatedIndexJS.add("${lineArgs.joinToString(" ")}") } lineCounter ++ } else if (commenting == 1) { @@ -118,6 +168,7 @@ fun main(args: Array) { commenting = 0 } } - outputIndexJS.appendText("}\n") - outputHTML.appendText("
\n") + files.generatedIndexJS.add("}") + files.generatedHTML.add("
") + files.write() }