Add variables, improve error handling
This commit is contained in:
parent
00d679715d
commit
bb31f570c2
|
@ -10,6 +10,10 @@ init {
|
||||||
}
|
}
|
||||||
val dingus = "dongusify"
|
val dingus = "dongusify"
|
||||||
var dongus = "dingusify"
|
var dongus = "dingusify"
|
||||||
|
|
||||||
|
dongus = dongus + "dabingus"
|
||||||
|
dongus = "heheheha"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onRecieving egg * * {
|
onRecieving egg * * {
|
||||||
|
|
|
@ -30,6 +30,24 @@ fun splitList(input: String): List<String> {
|
||||||
return result
|
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 {
|
object files {
|
||||||
val outputIndexJS = File("out/index.js")
|
val outputIndexJS = File("out/index.js")
|
||||||
val outputMessageJS = File("out/message.js}")
|
val outputMessageJS = File("out/message.js}")
|
||||||
|
@ -38,7 +56,14 @@ object files {
|
||||||
val generatedMessageJS = mutableListOf<String>()
|
val generatedMessageJS = mutableListOf<String>()
|
||||||
val generatedHTML = mutableListOf<String>()
|
val generatedHTML = mutableListOf<String>()
|
||||||
|
|
||||||
|
var errorStatus = 0
|
||||||
|
|
||||||
fun write() {
|
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("")
|
outputIndexJS.writeText("")
|
||||||
outputMessageJS.writeText("")
|
outputMessageJS.writeText("")
|
||||||
outputHTML.writeText("")
|
outputHTML.writeText("")
|
||||||
|
@ -52,30 +77,51 @@ object files {
|
||||||
for (item in generatedIndexJS) {
|
for (item in generatedIndexJS) {
|
||||||
outputIndexJS.appendText("$item\n")
|
outputIndexJS.appendText("$item\n")
|
||||||
}
|
}
|
||||||
|
log.note("All done! Your files are in ./out/")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testValidity(input: List<String>, num: Int): Int {
|
fun testValidity(input: List<String>, num: Int): Int {
|
||||||
println("Testing validity of ${input.toString()}...")
|
val validOperations = listOf("=", "++")
|
||||||
if (input[1] != "=") {
|
val declarations = listOf("val", "var")
|
||||||
println("""Error at line $num: Define your variable with "=", not "${input[1]}"""")
|
val initialising = declarations.contains(input[0])
|
||||||
return 1
|
|
||||||
|
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
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
println("Chookspeak Language Processor")
|
log.note("Starting transpiler...")
|
||||||
println("Let's Go!")
|
try {
|
||||||
|
if (args[1] == "-v") {
|
||||||
|
log.isLogging = 1
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {}
|
||||||
var fileLocation: File
|
var fileLocation: File
|
||||||
try {
|
try {
|
||||||
fileLocation = File(args[0])
|
fileLocation = File(args[0])
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
println("Please specify a file")
|
log.error("Error: Please specify a file")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (!fileLocation.exists()) {
|
if (!fileLocation.exists()) {
|
||||||
println("Your Chookspeak file does not exist.")
|
log.error("Error: Your Chookspeak file does not exist.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val file = fileLocation.readLines()
|
val file = fileLocation.readLines()
|
||||||
|
@ -84,13 +130,15 @@ fun main(args: Array<String>) {
|
||||||
var eggName = ""
|
var eggName = ""
|
||||||
val status = mutableListOf<String>()
|
val status = mutableListOf<String>()
|
||||||
val variables = mutableListOf<String>()
|
val variables = mutableListOf<String>()
|
||||||
|
val keywords = listOf("init", "sendMessage", "html", "log", "js", "val", "var", "}")
|
||||||
|
log.note("File loaded!")
|
||||||
for (line in file) {
|
for (line in file) {
|
||||||
val lineArgsUnfiltered = splitList(line)
|
val lineArgsUnfiltered = splitList(line)
|
||||||
val lineArgs = lineArgsUnfiltered.filterNot { it == "" }
|
val lineArgs = lineArgsUnfiltered.filterNot { it == "" }
|
||||||
if (lineArgs.size == 0) {
|
if (lineArgs.size == 0) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
println("line ${lineArgs.toString()}")
|
log.log("line ${lineArgs.toString()}")
|
||||||
var slc = 0
|
var slc = 0
|
||||||
for (item in lineArgs) {
|
for (item in lineArgs) {
|
||||||
if (item == "//") {
|
if (item == "//") {
|
||||||
|
@ -105,7 +153,7 @@ fun main(args: Array<String>) {
|
||||||
if (commenting == 0) {
|
if (commenting == 0) {
|
||||||
if (lineCounter == 1) {
|
if (lineCounter == 1) {
|
||||||
if (lineArgs[0] != "egg") {
|
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
|
return
|
||||||
} else {
|
} else {
|
||||||
eggName = lineArgs[1]
|
eggName = lineArgs[1]
|
||||||
|
@ -117,12 +165,12 @@ fun main(args: Array<String>) {
|
||||||
try {
|
try {
|
||||||
status.removeLast()
|
status.removeLast()
|
||||||
} catch (e: Exception) {}
|
} catch (e: Exception) {}
|
||||||
println("status ${status.toString()}")
|
log.log("status ${status.toString()}")
|
||||||
}
|
}
|
||||||
if (lineArgs.size > 1) {
|
if (lineArgs.size > 1) {
|
||||||
if (lineArgs[1] == "{") {
|
if (lineArgs[1] == "{") {
|
||||||
status.add(lineArgs[0])
|
status.add(lineArgs[0])
|
||||||
println("status ${status.toString()}")
|
log.log("status ${status.toString()}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (status == listOf("init")) {
|
if (status == listOf("init")) {
|
||||||
|
@ -137,7 +185,8 @@ fun main(args: Array<String>) {
|
||||||
if (lineArgs[2] == "=") {
|
if (lineArgs[2] == "=") {
|
||||||
files.generatedIndexJS.add("const ${lineArgs[1]} = ${lineArgs[3]};")
|
files.generatedIndexJS.add("const ${lineArgs[1]} = ${lineArgs[3]};")
|
||||||
} else {
|
} 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") {
|
if (lineArgs[0] == "var") {
|
||||||
|
@ -146,13 +195,17 @@ fun main(args: Array<String>) {
|
||||||
files.generatedIndexJS.add("let ${lineArgs[1]} = ${lineArgs[3]};")
|
files.generatedIndexJS.add("let ${lineArgs[1]} = ${lineArgs[3]};")
|
||||||
variables.add(lineArgs[1])
|
variables.add(lineArgs[1])
|
||||||
} else {
|
} 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 (variables.contains(lineArgs[0])) {
|
||||||
if (lineArgs[1] == "=") {
|
if (testValidity(lineArgs, lineCounter) == 0) {
|
||||||
//if ()
|
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") {
|
if (status == listOf("init", "html") && lineArgs[0] != "html") {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user