Initial commit

This commit is contained in:
2018-11-25 14:07:08 +01:00
commit 0b05242184
7 changed files with 206 additions and 0 deletions

10
.idea/kotlinc.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
<component name="KotlinCommonCompilerArguments">
<option name="apiVersion" value="1.2" />
<option name="languageVersion" value="1.2" />
</component>
</project>

19
.idea/libraries/KotlinJavaRuntime.xml generated Normal file
View File

@@ -0,0 +1,19 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime">
<CLASSES>
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-reflect.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-test.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk7.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk8.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-sources.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-reflect-sources.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-test-sources.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk7-sources.jar!/" />
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk8-sources.jar!/" />
</SOURCES>
</library>
</component>

6
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Bsl.iml" filepath="$PROJECT_DIR$/Bsl.iml" />
</modules>
</component>
</project>

12
Bsl.iml Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>

146
src/Interpreter.kt Normal file
View File

@@ -0,0 +1,146 @@
class Interpreter {
class BslFunction(val name: String, val pos: Int)
var error = false
val functions = ArrayList<BslFunction>()
val key_fun = '#'
val key_comment = '?'
val key_opening_bracket = '{'
val key_closing_bracket = '}'
val key_section_marker = '"'
val fun_typ_pattern = "pattern"
val fun_typ_main = "main"
val fun_stat_place_cube = "Cube"
val fun_opening_bracket = '('
val fun_closing_bracket = ')'
val args_seperator = ','
var code = "#pattern test (runs, arg,) {Cube (1.0, 3, 5,)} #main () {test (1, \"Hello there\",) Cube (1.2, 3, 6,)}"
var pos = 0
fun placeCube(timestamp: Double, type: Int, value: Int) {
System.out.println("Cube placed: " + timestamp + " " + type + " " + value)
}
fun placeBomb(timestamp: Double, value: Int) {
}
/*
Returns the current word at the cursor.
*/
fun popNextWord(escapeChar: Char): String {
val builder = StringBuilder()
while (code[pos] != escapeChar) {
if (code[pos] == key_section_marker) {
while (code[pos] != key_section_marker) { //We use that for Strings with spaces.
builder.append(code[pos])
pos++
}
pos++
}else {
builder.append(code[pos])
pos++
}
}
pos++
return builder.toString()
}
/*
Goes to the next specified char.
*/
fun gotoNext(key: Char) {
while (code[pos] != key) pos++
pos++
}
/*
Jumps to the next non-Space char
*/
fun jumpSpaces() {
while (code[pos] == ' ') pos++
}
fun popArgs(): Array<String> {
val argsList = ArrayList<String>()
while (code[pos] != fun_closing_bracket) {
jumpSpaces()
argsList.add(popNextWord(args_seperator))
}
pos++
return argsList.toTypedArray()
}
fun executeFun(my_pos: Int) {
val old_pos = pos
pos = my_pos
gotoNext(fun_opening_bracket)
//This time we just need the names for interpretation purposes
val agrsNames = popArgs()
gotoNext(key_opening_bracket)
while (code[pos] != key_closing_bracket) {
jumpSpaces()
if (code[pos] == key_comment) {
gotoNext(key_comment)
} else {
val fun_call = popNextWord(' ')
gotoNext(fun_opening_bracket)
callFunction(fun_call, popArgs())
}
}
pos = old_pos
}
fun callFunction(name: String, args: Array<String>) {
if (name == fun_stat_place_cube) {
placeCube(args[0].toDouble(), args[1].toInt(), args[2].toInt())
} else {
//Now we need to check for custom functions
for (this_fun in functions) {
if (this_fun.name == name) {
executeFun(this_fun.pos)
break
}
}
}
}
fun interpret() {
while (pos < code.length) {
if (code[pos] == key_fun) {
pos++
val fun_typ = popNextWord(' ')
jumpSpaces()
if (fun_typ.equals(fun_typ_pattern)) {
val functionName = popNextWord(' ')
functions.add(BslFunction(functionName, pos))
gotoNext(key_closing_bracket)
} else if (fun_typ == fun_typ_main) {
executeFun(pos)
return
}
} else if (code[pos] == key_comment) {
//We don't need comments
gotoNext(key_comment)
} else {
pos++
}
}
}
}

5
src/Main.kt Normal file
View File

@@ -0,0 +1,5 @@
fun main(args: Array<String>) {
val interpreter = Interpreter()
interpreter.interpret()
}