Initial commit

This commit is contained in:
Maxwell Jeffress 2024-12-13 11:01:20 +11:00
commit 8c25a8be3e
5 changed files with 182 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.gradle
.build
build
gradle
.kotlin
out

34
build.gradle.kts Normal file
View File

@ -0,0 +1,34 @@
plugins {
kotlin("jvm") version "2.0.0"
application
distribution
}
application {
mainClass.set("xyz.maxwellj.chookspeak.MainKt")
layout.buildDirectory.dir("distributions/")
}
group = "xyz.maxwellj.chookspeak"
version = "0.0.1"
repositories {
mavenCentral()
}
tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = "xyz.maxwellj.chookspeak.MainKt"
}
}
dependencies {
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(17)
}

16
examples/main.chsp Normal file
View File

@ -0,0 +1,16 @@
// comment lmao
/* a confusing comment */
egg notepad
init {
sendMessage "Hello there!"
html {
<textarea id="dingus">
}
log "egg initialised"
}
onRecieving egg, *, * {
messagehtml {
<div style="color: blue">we have successfully dingused</div>
}
}

5
settings.gradle.kts Normal file
View File

@ -0,0 +1,5 @@
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}
rootProject.name = "chookspeak"

120
src/main/kotlin/Main.kt Normal file
View File

@ -0,0 +1,120 @@
package xyz.maxwellj.chookspeak
import java.io.File
fun splitList(input: String): List<String> {
val result = mutableListOf<String>()
var currentToken = StringBuilder()
var insideQuotes = false
for (char in input) {
when {
char == '"' -> {
currentToken.append(char)
insideQuotes = !insideQuotes
}
char.isWhitespace() && !insideQuotes -> {
if (currentToken.isNotEmpty()) {
result.add(currentToken.toString())
currentToken = StringBuilder()
}
}
else -> currentToken.append(char)
}
}
if (currentToken.isNotEmpty()) {
result.add(currentToken.toString())
}
return result
}
fun main(args: Array<String>) {
println("Chookspeak Language Processor")
println("Let's Go!")
var fileLocation: File
try {
fileLocation = File(args[0])
} catch (e: Exception) {
println("Please specify a file")
return
}
if (!fileLocation.exists()) {
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<String>()
for (line in file) {
val lineArgsUnfiltered = splitList(line)
val lineArgs = lineArgsUnfiltered.filterNot { it == "" }
if (lineArgs.size == 0) {
continue
}
println("line ${lineArgs.toString()}")
var slc = 0
for (item in lineArgs) {
if (item == "//") {
commenting = 1
} else if (item == "/*") {
commenting = 2
slc = 1
} else if (item == "*/" && commenting == 2 && slc == 0) {
commenting = 0
}
}
if (commenting == 0) {
if (lineCounter == 1) {
if (lineArgs[0] != "egg") {
println("Please specify your egg on your first line of code.")
return
} else {
eggName = lineArgs[1]
outputIndexJS.appendText("function egg$eggName() {\n")
outputHTML.appendText("""<div id="$eggName" style="display: hidden">${"\n"}""")
}
}
if (lineArgs[0] == "}") {
try {
status.removeLast()
} catch (e: Exception) {}
println("status ${status.toString()}")
}
if (lineArgs.size > 1) {
if (lineArgs[1] == "{") {
status.add(lineArgs[0])
println("status ${status.toString()}")
}
}
if (status == listOf("init")) {
if (lineArgs[0] == "log") {
outputIndexJS.appendText("console.log(${lineArgs[1]});\n")
}
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")
}
}
if (status == listOf("init", "html") && lineArgs[0] != "html") {
outputHTML.appendText("${lineArgs.joinToString(" ")}\n")
}
lineCounter ++
} else if (commenting == 1) {
commenting = 0
} else if (commenting == 2) {
commenting = 0
}
}
outputIndexJS.appendText("}\n")
outputHTML.appendText("</div>\n")
}