Created
October 28, 2011 03:38
-
-
Save acfoltzer/1321567 to your computer and use it in GitHub Desktop.
No stacks to overflow here...
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.util.Formatter; | |
public class _interp { | |
//labels | |
static final int _value$_of = 0; | |
static final int _apply$_k = 1; | |
static final int _apply$_env = 2; | |
static final int _apply$_proc = 3; | |
//registers | |
Object __expr, __env, __k, __v, __num, __c, __a; | |
//program counter | |
int __pc; | |
//union types | |
static class _exp_const { | |
Object _n; | |
_exp_const(Object _n) { | |
this._n = _n; | |
} | |
} | |
static class _exp_var { | |
Object _v; | |
_exp_var(Object _v) { | |
this._v = _v; | |
} | |
} | |
static class _exp_if { | |
Object _test, _conseq, _alt; | |
_exp_if(Object _test, Object _conseq, Object _alt) { | |
this._test = _test; | |
this._conseq = _conseq; | |
this._alt = _alt; | |
} | |
} | |
static class _exp_mult { | |
Object _rand1, _rand2; | |
_exp_mult(Object _rand1, Object _rand2) { | |
this._rand1 = _rand1; | |
this._rand2 = _rand2; | |
} | |
} | |
static class _exp_sub1 { | |
Object _rand; | |
_exp_sub1(Object _rand) { | |
this._rand = _rand; | |
} | |
} | |
static class _exp_zero { | |
Object _rand; | |
_exp_zero(Object _rand) { | |
this._rand = _rand; | |
} | |
} | |
static class _exp_letcc { | |
Object _body; | |
_exp_letcc(Object _body) { | |
this._body = _body; | |
} | |
} | |
static class _exp_throw { | |
Object _vexp, _kexp; | |
_exp_throw(Object _vexp, Object _kexp) { | |
this._vexp = _vexp; | |
this._kexp = _kexp; | |
} | |
} | |
static class _exp_let { | |
Object _vexp, _body; | |
_exp_let(Object _vexp, Object _body) { | |
this._vexp = _vexp; | |
this._body = _body; | |
} | |
} | |
static class _exp_lambda { | |
Object _body; | |
_exp_lambda(Object _body) { | |
this._body = _body; | |
} | |
} | |
static class _exp_app { | |
Object _rator, _rand; | |
_exp_app(Object _rator, Object _rand) { | |
this._rator = _rator; | |
this._rand = _rand; | |
} | |
} | |
static class _kt_empty { | |
Object _jumpout; | |
_kt_empty(Object _jumpout) { | |
this._jumpout = _jumpout; | |
} | |
} | |
static class _kt_if { | |
Object _conseq, _alt, _env, _k; | |
_kt_if(Object _conseq, Object _alt, Object _env, Object _k) { | |
this._conseq = _conseq; | |
this._alt = _alt; | |
this._env = _env; | |
this._k = _k; | |
} | |
} | |
static class _kt_mult$_inner { | |
Object _k, _n; | |
_kt_mult$_inner(Object _k, Object _n) { | |
this._k = _k; | |
this._n = _n; | |
} | |
} | |
static class _kt_mult$_outer { | |
Object _rand2, _env, _k; | |
_kt_mult$_outer(Object _rand2, Object _env, Object _k) { | |
this._rand2 = _rand2; | |
this._env = _env; | |
this._k = _k; | |
} | |
} | |
static class _kt_sub1 { | |
Object _k; | |
_kt_sub1(Object _k) { | |
this._k = _k; | |
} | |
} | |
static class _kt_zero { | |
Object _k; | |
_kt_zero(Object _k) { | |
this._k = _k; | |
} | |
} | |
static class _kt_throw { | |
Object _vexp, _env; | |
_kt_throw(Object _vexp, Object _env) { | |
this._vexp = _vexp; | |
this._env = _env; | |
} | |
} | |
static class _kt_let { | |
Object _body, _env, _k; | |
_kt_let(Object _body, Object _env, Object _k) { | |
this._body = _body; | |
this._env = _env; | |
this._k = _k; | |
} | |
} | |
static class _kt_app$_inner { | |
Object _p, _k; | |
_kt_app$_inner(Object _p, Object _k) { | |
this._p = _p; | |
this._k = _k; | |
} | |
} | |
static class _kt_app$_outer { | |
Object _rand, _env, _k; | |
_kt_app$_outer(Object _rand, Object _env, Object _k) { | |
this._rand = _rand; | |
this._env = _env; | |
this._k = _k; | |
} | |
} | |
static class _envr_empty { | |
} | |
static class _envr_extend { | |
Object _arg, _env; | |
_envr_extend(Object _arg, Object _env) { | |
this._arg = _arg; | |
this._env = _env; | |
} | |
} | |
static class _clos_closure { | |
Object _code, _env; | |
_clos_closure(Object _code, Object _env) { | |
this._code = _code; | |
this._env = _env; | |
} | |
} | |
//trampoline | |
void trampoline() throws Exception { | |
tramp: while(true) { | |
switch (__pc) { | |
case _value$_of: | |
if (__expr instanceof _exp_const) { | |
Object _n = ((_exp_const) __expr)._n; | |
__k = __k; | |
__v = _n; | |
__pc = _apply$_k; | |
} else if (__expr instanceof _exp_var) { | |
Object _v = ((_exp_var) __expr)._v; | |
__k = __k; | |
__env = __env; | |
__num = _v; | |
__pc = _apply$_env; | |
} else if (__expr instanceof _exp_if) { | |
Object _test = ((_exp_if) __expr)._test; | |
Object _conseq = ((_exp_if) __expr)._conseq; | |
Object _alt = ((_exp_if) __expr)._alt; | |
__k = new _kt_if(_conseq, _alt, __env, __k); | |
__expr = _test; | |
__env = __env; | |
__pc = _value$_of; | |
} else if (__expr instanceof _exp_mult) { | |
Object _rand1 = ((_exp_mult) __expr)._rand1; | |
Object _rand2 = ((_exp_mult) __expr)._rand2; | |
__k = new _kt_mult$_outer(_rand2, __env, __k); | |
__expr = _rand1; | |
__env = __env; | |
__pc = _value$_of; | |
} else if (__expr instanceof _exp_sub1) { | |
Object _rand = ((_exp_sub1) __expr)._rand; | |
__k = new _kt_sub1(__k); | |
__expr = _rand; | |
__env = __env; | |
__pc = _value$_of; | |
} else if (__expr instanceof _exp_zero) { | |
Object _rand = ((_exp_zero) __expr)._rand; | |
__k = new _kt_zero(__k); | |
__expr = _rand; | |
__env = __env; | |
__pc = _value$_of; | |
} else if (__expr instanceof _exp_letcc) { | |
Object _body = ((_exp_letcc) __expr)._body; | |
__k = __k; | |
__expr = _body; | |
__env = new _envr_extend(__k, __env); | |
__pc = _value$_of; | |
} else if (__expr instanceof _exp_throw) { | |
Object _vexp = ((_exp_throw) __expr)._vexp; | |
Object _kexp = ((_exp_throw) __expr)._kexp; | |
__k = new _kt_throw(_vexp, __env); | |
__expr = _kexp; | |
__env = __env; | |
__pc = _value$_of; | |
} else if (__expr instanceof _exp_let) { | |
Object _vexp = ((_exp_let) __expr)._vexp; | |
Object _body = ((_exp_let) __expr)._body; | |
__k = new _kt_let(_body, __env, __k); | |
__expr = _vexp; | |
__env = __env; | |
__pc = _value$_of; | |
} else if (__expr instanceof _exp_lambda) { | |
Object _body = ((_exp_lambda) __expr)._body; | |
__k = __k; | |
__v = new _clos_closure(_body, __env); | |
__pc = _apply$_k; | |
} else if (__expr instanceof _exp_app) { | |
Object _rator = ((_exp_app) __expr)._rator; | |
Object _rand = ((_exp_app) __expr)._rand; | |
__k = new _kt_app$_outer(_rand, __env, __k); | |
__expr = _rator; | |
__env = __env; | |
__pc = _value$_of; | |
} else { throw new Exception((new Formatter()).format("Error in union-case: could not match %s against type _exp", __expr.getClass().getName()).out().toString()); | |
} | |
break; | |
case _apply$_k: | |
if (__k instanceof _kt_empty) { | |
Object _jumpout = ((_kt_empty) __k)._jumpout; | |
break tramp; | |
} else if (__k instanceof _kt_if) { | |
Object _conseq = ((_kt_if) __k)._conseq; | |
Object _alt = ((_kt_if) __k)._alt; | |
Object _env = ((_kt_if) __k)._env; | |
Object _k = ((_kt_if) __k)._k; | |
if ((Boolean)__v) { | |
__k = _k; | |
__expr = _conseq; | |
__env = _env; | |
__pc = _value$_of; | |
} else { | |
__k = _k; | |
__expr = _alt; | |
__env = _env; | |
__pc = _value$_of; | |
} | |
} else if (__k instanceof _kt_mult$_inner) { | |
Object _k = ((_kt_mult$_inner) __k)._k; | |
Object _n = ((_kt_mult$_inner) __k)._n; | |
__k = _k; | |
__v = ((Integer)_n * (Integer)__v); | |
__pc = _apply$_k; | |
} else if (__k instanceof _kt_mult$_outer) { | |
Object _rand2 = ((_kt_mult$_outer) __k)._rand2; | |
Object _env = ((_kt_mult$_outer) __k)._env; | |
Object _k = ((_kt_mult$_outer) __k)._k; | |
__k = new _kt_mult$_inner(_k, __v); | |
__expr = _rand2; | |
__env = _env; | |
__pc = _value$_of; | |
} else if (__k instanceof _kt_sub1) { | |
Object _k = ((_kt_sub1) __k)._k; | |
__k = _k; | |
__v = ((Integer)__v - (Integer)Integer.valueOf(1)); | |
__pc = _apply$_k; | |
} else if (__k instanceof _kt_zero) { | |
Object _k = ((_kt_zero) __k)._k; | |
__k = _k; | |
__v = ((Integer)__v == 0); | |
__pc = _apply$_k; | |
} else if (__k instanceof _kt_throw) { | |
Object _vexp = ((_kt_throw) __k)._vexp; | |
Object _env = ((_kt_throw) __k)._env; | |
__k = __v; | |
__expr = _vexp; | |
__env = _env; | |
__pc = _value$_of; | |
} else if (__k instanceof _kt_let) { | |
Object _body = ((_kt_let) __k)._body; | |
Object _env = ((_kt_let) __k)._env; | |
Object _k = ((_kt_let) __k)._k; | |
__k = _k; | |
__expr = _body; | |
__env = new _envr_extend(__v, _env); | |
__pc = _value$_of; | |
} else if (__k instanceof _kt_app$_inner) { | |
Object _p = ((_kt_app$_inner) __k)._p; | |
Object _k = ((_kt_app$_inner) __k)._k; | |
__k = _k; | |
__c = _p; | |
__a = __v; | |
__pc = _apply$_proc; | |
} else if (__k instanceof _kt_app$_outer) { | |
Object _rand = ((_kt_app$_outer) __k)._rand; | |
Object _env = ((_kt_app$_outer) __k)._env; | |
Object _k = ((_kt_app$_outer) __k)._k; | |
__k = new _kt_app$_inner(__v, _k); | |
__expr = _rand; | |
__env = _env; | |
__pc = _value$_of; | |
} else { throw new Exception((new Formatter()).format("Error in union-case: could not match %s against type _kt", __k.getClass().getName()).out().toString()); | |
} | |
break; | |
case _apply$_env: | |
if (__env instanceof _envr_empty) { | |
throw new Exception((new Formatter()).format("Error in _env: unbound variable").out().toString()); | |
} else if (__env instanceof _envr_extend) { | |
Object _arg = ((_envr_extend) __env)._arg; | |
Object _env = ((_envr_extend) __env)._env; | |
if ((Boolean)((Integer)__num == 0)) { | |
__k = __k; | |
__v = _arg; | |
__pc = _apply$_k; | |
} else { | |
__k = __k; | |
__env = _env; | |
__num = ((Integer)__num - 1); | |
__pc = _apply$_env; | |
} | |
} else { throw new Exception((new Formatter()).format("Error in union-case: could not match %s against type _envr", __env.getClass().getName()).out().toString()); | |
} | |
break; | |
case _apply$_proc: | |
if (__c instanceof _clos_closure) { | |
Object _code = ((_clos_closure) __c)._code; | |
Object _env = ((_clos_closure) __c)._env; | |
__k = __k; | |
__expr = _code; | |
__env = new _envr_extend(__a, _env); | |
__pc = _value$_of; | |
} else { throw new Exception((new Formatter()).format("Error in union-case: could not match %s against type _clos", __c.getClass().getName()).out().toString()); | |
} | |
break; | |
default: throw new Exception("Invalid label " + __pc); | |
} | |
} | |
} | |
//run corresponds to main label from ParentheC | |
public void run() throws Exception { | |
__expr = new _exp_app(new _exp_lambda(new _exp_app(new _exp_app(new _exp_var(Integer.valueOf(0)), new _exp_var(Integer.valueOf(0))), new _exp_const(Integer.valueOf(5)))), new _exp_lambda(new _exp_lambda(new _exp_if(new _exp_zero(new _exp_var(Integer.valueOf(0))), new _exp_const(Integer.valueOf(1)), new _exp_mult(new _exp_var(Integer.valueOf(0)), new _exp_app(new _exp_app(new _exp_var(Integer.valueOf(1)), new _exp_var(Integer.valueOf(1))), new _exp_sub1(new _exp_var(Integer.valueOf(0))))))))); | |
__env = new _envr_empty(); | |
__pc = _value$_of; | |
__k = new _kt_empty(null); | |
trampoline(); | |
System.out.printf("1: %s\n", __v); | |
__expr = new _exp_mult(new _exp_const(Integer.valueOf(2)), new _exp_letcc(new _exp_mult(new _exp_const(Integer.valueOf(5)), new _exp_throw(new _exp_mult(new _exp_const(Integer.valueOf(2)), new _exp_const(Integer.valueOf(6))), new _exp_var(Integer.valueOf(0)))))); | |
__env = new _envr_empty(); | |
__pc = _value$_of; | |
__k = new _kt_empty(null); | |
trampoline(); | |
System.out.printf("2: %s\n", __v); | |
__expr = new _exp_let(new _exp_lambda(new _exp_lambda(new _exp_if(new _exp_zero(new _exp_var(Integer.valueOf(0))), new _exp_const(Integer.valueOf(1)), new _exp_mult(new _exp_var(Integer.valueOf(0)), new _exp_app(new _exp_app(new _exp_var(Integer.valueOf(1)), new _exp_var(Integer.valueOf(1))), new _exp_sub1(new _exp_var(Integer.valueOf(0)))))))), new _exp_app(new _exp_app(new _exp_var(Integer.valueOf(0)), new _exp_var(Integer.valueOf(0))), new _exp_const(Integer.valueOf(5)))); | |
__env = new _envr_empty(); | |
__pc = _value$_of; | |
__k = new _kt_empty(null); | |
trampoline(); | |
System.out.printf("3: %s\n", __v); | |
} | |
public static void main(String[] args) throws Exception { | |
new _interp().run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment