kentaro (owner)

Revisions

gist: 140914 Download_button fork
public
Public Clone URL: git://gist.github.com/140914.git
Embed All Files: show embed
Java #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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("`|");