Skip to content

Instantly share code, notes, and snippets.

@kentaro
Created July 5, 2009 09:42
Show Gist options
  • Save kentaro/140914 to your computer and use it in GitHub Desktop.
Save kentaro/140914 to your computer and use it in GitHub Desktop.
import scala.collection.mutable.ArrayStack
object Kemuri extends Application {
private var stack: ArrayStack[Int] = new ArrayStack[Int];
private def eval (c: Char) = {
if (c == '|') {
stack.foreach((i:Int) => print(i.toChar))
stack.clear
}
else if (c == '^') {
val a = stack.pop
val b = stack.pop
stack.push(a ^ b)
}
else if (c == '~') {
stack.push(~stack.pop)
}
else if (c == '"') {
val a = stack.pop
stack.push(a)
stack.push(a)
}
else if (c == '\'') {
val a = stack.pop
val b = stack.pop
val c = stack.pop
stack.push(a)
stack.push(c)
stack.push(b)
}
else if (c == '`') {
"Hello, world!".reverse.foreach((c:Char) => stack.push(c.toByte))
}
else { error("illegal char") }
}
def run (args: Array[String]): Unit = {
args.foreach((s:String) => s.foreach(eval))
}
def run (args: String): Unit = {
args foreach eval
}
}
Kemuri.run(args)
// Kemuri.run("`|");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment