Add variables, improve error handling

This commit is contained in:
Maxwell Jeffress 2024-12-16 13:32:51 +11:00
parent 00d679715d
commit bb31f570c2
2 changed files with 74 additions and 17 deletions

View File

@ -10,6 +10,10 @@ init {
}
val dingus = "dongusify"
var dongus = "dingusify"
dongus = dongus + "dabingus"
dongus = "heheheha"
}
onRecieving egg * * {

View File

@ -30,6 +30,24 @@ fun splitList(input: String): List<String> {
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}")
@ -38,7 +56,14 @@ object files {
val generatedMessageJS = mutableListOf<String>()
val generatedHTML = mutableListOf<String>()
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<String>, num: Int): Int {
println("Testing validity of ${input.toString()}...")
if (input[1] != "=") {
println("""Error at line $num: Define your variable with "=", not "${input[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<String>) {
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<String>) {
var eggName = ""
val status = mutableListOf<String>()
val variables = mutableListOf<String>()
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<String>) {
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<String>) {
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<String>) {
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<String>) {
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") {