Created
December 6, 2008 13:13
-
-
Save hsak/32874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream | |
import java.io.IOException | |
class String_readByteList(filename:String) { | |
def readByteList():List[Byte] = { | |
try { | |
def loop(in:FileInputStream):List[Byte] = { | |
val c:Int = in.read() | |
if (c == -1) { | |
in.close() | |
Nil | |
} else { | |
c.asInstanceOf[Byte]::loop (in) | |
} | |
} | |
loop(new FileInputStream(filename)) | |
} catch { | |
case _ => return List[Byte]() | |
} | |
} | |
} | |
object hoge { | |
implicit def toReadByteList(filename:String) = new String_readByteList(filename) | |
def main(argv:Array[String]) { | |
val data = "um.um".readByteList() | |
println(data) | |
eval(data) | |
} | |
def eval(c:List[byte]) { | |
def eval1(c:List[byte],reg:Array[Int]) { | |
val (code:Int,rest:List[byte]) = c match { | |
case (a::b::c::d::e) => (((a & 255)<<24)|((b & 255)<<16)|((c & 255)<<8)| (d & 255), e) | |
case _ => (-1,Nil) | |
} | |
// println(code) | |
if (code != -1) { | |
val operator = (code >> 28)&0xf | |
val a = ((code>>6) & 7) | |
val b = ((code>>3) & 7) | |
val c = (code & 7) | |
val o = (code>>9) & 0x7ffff | |
val A = ((code >> 25) & 7) | |
val value = (code & 0x1ffffff) | |
val opName = operator match { | |
case 0 => "Conditional Move.("+a+","+b+","+c+")"+o | |
case 1 => "Array Index.("+a+","+b+","+c+")"+o | |
case 2 => "Array Amendment.("+a+","+b+","+c+")"+o | |
case 3 => "Addition.("+a+","+b+","+c+")"+o | |
case 4 => "Multiplication.("+a+","+b+","+c+")"+o | |
case 5 => "Division.("+a+","+b+","+c+")"+o | |
case 6 => "Not-And.("+a+","+b+","+c+")"+o | |
case 7 => "Halt.("+a+","+b+","+c+")"+o | |
case 8 => "Allocation.("+a+","+b+","+c+")"+o | |
case 9 => "Abandonment.("+a+","+b+","+c+")"+o | |
case 10 => "Output.("+a+","+b+","+c+")"+o | |
case 11 => "Input.("+a+","+b+","+c+")"+o | |
case 12 => "Load Program.("+a+","+b+","+c+")"+o | |
case 13 => "Orthography.("+A+","+value+")" | |
case _ => "unknown.("+operator+")" | |
} | |
println(opName) | |
val opName = operator match { | |
case 0 => "Conditional Move.("+a+","+b+","+c+")"+o | |
case 1 => "Array Index.("+a+","+b+","+c+")"+o | |
case 2 => "Array Amendment.("+a+","+b+","+c+")"+o | |
case 3 => "Addition.("+a+","+b+","+c+")"+o | |
case 4 => "Multiplication.("+a+","+b+","+c+")"+o | |
case 5 => "Division.("+a+","+b+","+c+")"+o | |
case 6 => "Not-And.("+a+","+b+","+c+")"+o | |
case 7 => "Halt.("+a+","+b+","+c+")"+o | |
case 8 => "Allocation.("+a+","+b+","+c+")"+o | |
case 9 => "Abandonment.("+a+","+b+","+c+")"+o | |
case 10 => "Output.("+a+","+b+","+c+")"+o | |
case 11 => "Input.("+a+","+b+","+c+")"+o | |
case 12 => "Load Program.("+a+","+b+","+c+")"+o | |
case 13 => reg[A] = value; eval1(rest, reg) | |
case _ => "unknown.("+operator+")" | |
} | |
} | |
} | |
eval1(c,new Array[Int](8)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment