Skip to content

Instantly share code, notes, and snippets.

@astynax
Last active November 15, 2021 21:51
Show Gist options
  • Save astynax/76b09a79f2cf31f4ee55d384f3bc8f7a to your computer and use it in GitHub Desktop.
Save astynax/76b09a79f2cf31f4ee55d384f3bc8f7a to your computer and use it in GitHub Desktop.
Made just for fun the BASIC-like DSL in Kotlin
class Basic {
data class Var(val name: String)
class Let {
infix fun BE(value: Any): Binding = TODO()
}
class Binding {
infix fun MOD(other: Any): Binding = TODO()
}
class For {
infix fun IN(range: IntRange) {}
}
class If {
infix fun EQUALS(value: Any): Then = TODO()
}
class Then {
infix fun THEN(line: Int): Else = TODO()
}
class Else {
infix fun ELSE(line: Int) { TODO() }
}
class Context {
val A = Var("A")
val B = Var("B")
val C = Var("C")
val D = Var("D")
val E = Var("E")
infix fun Int.PRINT(message: String) { TODO() }
infix fun Int.PRINT(name: Var) { TODO() }
infix fun Int.GOTO(line: Int) { TODO() }
infix fun Int.LET(name: Var): Let = TODO()
infix fun Int.FOR(name: Var): For = TODO()
infix fun Int.IF(name: Var): If = TODO()
infix fun Int.NEXT(name: Var) { TODO() }
}
fun build(body: Context.() -> Unit) {
Context().apply(body)
}
}
fun basic(script: Basic.Context.() -> Unit) {
Basic().build(script)
}
basic {
10 PRINT "-= FizzBuzz =-"
20 FOR A IN 1..20
30 LET B BE A MOD 3
40 LET C BE A MOD 5
50 IF B EQUALS 0 THEN 90
60 IF C EQUALS 0 THEN 120
70 PRINT A
80 GOTO 150
90 IF C EQUALS 0 THEN 140
100 PRINT "fizz"
110 GOTO 150
120 PRINT "buzz"
130 GOTO 150
140 PRINT "fizzbuzz"
150 NEXT A
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment