Created
December 5, 2022 17:46
-
-
Save davidedc/2aa61ca59b089e3aa34d113e05d449c1 to your computer and use it in GitHub Desktop.
SmallWorld subset translated to js by jsweet
This file contains 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
// follow-up to https://gist.github.com/davidedc/138e752949f0acd73595f37f46c7cdb9 | |
var __extends = (this && this.__extends) || (function () { | |
var extendStatics = function (d, b) { | |
extendStatics = Object.setPrototypeOf || | |
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | |
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | |
return extendStatics(d, b); | |
}; | |
return function (d, b) { | |
extendStatics(d, b); | |
function __() { this.constructor = d; } | |
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | |
}; | |
})(); | |
/* Generated from Java with JSweet 3.0.0 - http://www.jsweet.org */ | |
var Runner = /** @class */ (function () { | |
function Runner(imageName) { | |
if (this.interpreter === undefined) { | |
this.interpreter = null; | |
} | |
this.interpreter = new SmallInterpreter(); | |
} | |
Runner.prototype.doIt = function (task) { | |
Runner.out("Running task: " + task); | |
var TrueClass = this.interpreter.trueObject.objClass; | |
var name = TrueClass.data[0]; | |
var StringClass = name.objClass; | |
var methods = StringClass.data[2]; | |
var doItMethod = null; | |
for (var i = 0; i < methods.data.length; i++) { | |
{ | |
var aMethod = methods.data[i]; | |
if ("doIt" === aMethod.data[0].toString()) { | |
doItMethod = aMethod; | |
} | |
} | |
; | |
} | |
if (doItMethod == null) { | |
Runner.out("can\'t find do it!!"); | |
} | |
else { | |
var rec = new SmallByteArray(StringClass, task); | |
var args = new SmallObject(this.interpreter.ArrayClass, 1); | |
args.data[0] = rec; | |
var ctx = this.interpreter.buildContext(this.interpreter.nilObject, args, doItMethod); | |
try { | |
return this.interpreter.execute(ctx, 1, 1); | |
} | |
catch (ex) { | |
console.error(ex.message, ex); | |
} | |
finally { | |
Runner.out("Task complete"); | |
} | |
} | |
return null; | |
}; | |
/*private*/ Runner.out = function (o) { | |
console.info(o); | |
}; | |
/*private*/ Runner.prompt = function () { | |
console.info("SmallWorld> "); | |
java.lang.System.out.flush(); | |
}; | |
Runner.main = function (args) { | |
var runner = new Runner(args.length > 0 ? args[0] : null); | |
Runner.prompt(); | |
}; | |
return Runner; | |
}()); | |
Runner["__class"] = "Runner"; | |
var Sema = /** @class */ (function () { | |
function Sema() { | |
this.hasBeenSet = false; | |
if (this.value === undefined) { | |
this.value = null; | |
} | |
} | |
Sema.prototype.get = function () { | |
return this.value; | |
}; | |
Sema.prototype.set = function (v) { | |
this.value = v; | |
this.hasBeenSet = true; | |
}; | |
return Sema; | |
}()); | |
Sema["__class"] = "Sema"; | |
var ImageWriter = /** @class */ (function () { | |
function ImageWriter() { | |
if (this.allObjects === undefined) { | |
this.allObjects = null; | |
} | |
if (this.smallIntCount === undefined) { | |
this.smallIntCount = 0; | |
} | |
if (this.objectIndex === undefined) { | |
this.objectIndex = 0; | |
} | |
if (this.roots === undefined) { | |
this.roots = null; | |
} | |
} | |
ImageWriter.prototype.finish = function () { | |
}; | |
ImageWriter.prototype.writeObject$SmallInt_A = function (ints) { | |
if (this.smallIntCount > 0) { | |
throw Object.defineProperty(new Error("Can only write ints one time"), '__classes', { configurable: true, value: ['java.lang.Throwable', 'java.lang.Object', 'java.lang.RuntimeException', 'java.lang.Exception'] }); | |
} | |
this.smallIntCount = ints.length; | |
for (var index11935 = 0; index11935 < ints.length; index11935++) { | |
var child = ints[index11935]; | |
{ | |
this.writeObject$SmallObject(child); | |
} | |
} | |
}; | |
ImageWriter.prototype.writeObject = function (ints) { | |
if (((ints != null && ints instanceof Array && (ints.length == 0 || ints[0] == null || (ints[0] != null && ints[0] instanceof SmallInt))) || ints === null)) { | |
return this.writeObject$SmallInt_A(ints); | |
} | |
else if (((ints != null && ints instanceof SmallObject) || ints === null)) { | |
return this.writeObject$SmallObject(ints); | |
} | |
else | |
throw new Error('invalid overload'); | |
}; | |
ImageWriter.prototype.writeObject$SmallObject = function (obj) { | |
}; | |
/*private*/ ImageWriter.prototype.writeObjectImpl = function (obj) { | |
}; | |
return ImageWriter; | |
}()); | |
ImageWriter["__class"] = "ImageWriter"; | |
var SmallException = /** @class */ (function (_super) { | |
__extends(SmallException, _super); | |
function SmallException(gripe, c) { | |
var _this = _super.call(this, gripe) || this; | |
_this.message = gripe; | |
Object.setPrototypeOf(_this, SmallException.prototype); | |
if (_this.context === undefined) { | |
_this.context = null; | |
} | |
_this.context = c; | |
return _this; | |
} | |
return SmallException; | |
}(Error)); | |
SmallException["__class"] = "SmallException"; | |
SmallException["__interfaces"] = ["java.io.Serializable"]; | |
var SmallObject = /** @class */ (function () { | |
function SmallObject(cl, size) { | |
if (((cl != null && cl instanceof SmallObject) || cl === null) && ((typeof size === 'number') || size === null)) { | |
var __args = arguments; | |
if (this.data === undefined) { | |
this.data = null; | |
} | |
if (this.objClass === undefined) { | |
this.objClass = null; | |
} | |
this.objClass = cl; | |
this.data = (function (s) { var a = []; while (s-- > 0) | |
a.push(null); return a; })(size); | |
} | |
else if (cl === undefined && size === undefined) { | |
var __args = arguments; | |
if (this.data === undefined) { | |
this.data = null; | |
} | |
if (this.objClass === undefined) { | |
this.objClass = null; | |
} | |
this.objClass = null; | |
this.data = null; | |
} | |
else | |
throw new Error('invalid overload'); | |
} | |
SmallObject.prototype.copy = function (cl) { | |
return this; | |
}; | |
return SmallObject; | |
}()); | |
SmallObject["__class"] = "SmallObject"; | |
/** | |
* Little Smalltalk Interpreter written in Java. | |
* | |
* <p>Written by Tim Budd, [email protected] | |
* | |
* <p>Version 0.8 (November 2002) | |
* @class | |
*/ | |
var SmallInterpreter = /** @class */ (function () { | |
function SmallInterpreter() { | |
if (this.ArrayClass === undefined) { | |
this.ArrayClass = null; | |
} | |
if (this.BlockClass === undefined) { | |
this.BlockClass = null; | |
} | |
if (this.ContextClass === undefined) { | |
this.ContextClass = null; | |
} | |
if (this.falseObject === undefined) { | |
this.falseObject = null; | |
} | |
if (this.IntegerClass === undefined) { | |
this.IntegerClass = null; | |
} | |
if (this.nilObject === undefined) { | |
this.nilObject = null; | |
} | |
if (this.smallInts === undefined) { | |
this.smallInts = null; | |
} | |
if (this.trueObject === undefined) { | |
this.trueObject = null; | |
} | |
} | |
SmallInterpreter.prototype.buildContext = function (oldContext, __arguments, method) { | |
var context = new SmallObject(this.ContextClass, 7); | |
context.data[0] = method; | |
context.data[1] = __arguments; | |
var max = (method.data[4]).value; | |
if (max > 0) { | |
context.data[2] = new SmallObject(this.ArrayClass, max); | |
while ((max > 0)) { | |
context.data[2].data[--max] = this.nilObject; | |
} | |
; | |
} | |
max = (method.data[3]).value; | |
context.data[3] = new SmallObject(this.ArrayClass, max); | |
context.data[4] = this.smallInts[0]; | |
context.data[5] = this.smallInts[0]; | |
context.data[6] = oldContext; | |
return context; | |
}; | |
SmallInterpreter.prototype.execute = function (context, myThread, parentThread) { | |
var selectorCache = (function (s) { var a = []; while (s-- > 0) | |
a.push(null); return a; })(197); | |
var classCache = (function (s) { var a = []; while (s-- > 0) | |
a.push(null); return a; })(197); | |
var methodCache = (function (s) { var a = []; while (s-- > 0) | |
a.push(null); return a; })(197); | |
var lookup = 0; | |
var cached = 0; | |
var contextData = context.data; | |
outerLoop: while ((true)) { | |
{ | |
var debug = false; | |
var method = contextData[0]; | |
var code = method.data[1].values; | |
var bytePointer = contextData[4].value; | |
var stack = contextData[3].data; | |
var stackTop = contextData[5].value; | |
var returnedValue = null; | |
var tempa = void 0; | |
var temporaries = null; | |
var instanceVariables = null; | |
var __arguments = null; | |
var literals = null; | |
innerLoop: while ((true)) { | |
{ | |
var high = code[bytePointer++]; | |
var low = high & 15; | |
high = (high >>= 4) & 15; | |
if (high === 0) { | |
high = low; | |
low = code[bytePointer++] & 255; | |
} | |
switch ((high)) { | |
case 1: | |
if (__arguments == null) { | |
__arguments = contextData[1]; | |
} | |
if (instanceVariables == null) { | |
instanceVariables = __arguments.data[0].data; | |
} | |
stack[stackTop++] = instanceVariables[low]; | |
break; | |
case 2: | |
if (__arguments == null) { | |
__arguments = contextData[1]; | |
} | |
stack[stackTop++] = __arguments.data[low]; | |
break; | |
case 3: | |
if (temporaries == null) { | |
temporaries = contextData[2].data; | |
} | |
stack[stackTop++] = temporaries[low]; | |
break; | |
case 4: | |
if (literals == null) { | |
literals = method.data[2].data; | |
} | |
stack[stackTop++] = literals[low]; | |
break; | |
case 5: | |
switch ((low)) { | |
case 0: | |
case 1: | |
case 2: | |
case 3: | |
case 4: | |
case 5: | |
case 6: | |
case 7: | |
case 8: | |
case 9: | |
stack[stackTop++] = this.smallInts[low]; | |
break; | |
case 10: | |
stack[stackTop++] = this.nilObject; | |
break; | |
case 11: | |
stack[stackTop++] = this.trueObject; | |
break; | |
case 12: | |
stack[stackTop++] = this.falseObject; | |
break; | |
default: | |
throw new SmallException("Unknown constant " + low, context); | |
} | |
break; | |
case 12: | |
high = code[bytePointer++] & 255; | |
returnedValue = new SmallObject(this.BlockClass, 10); | |
tempa = returnedValue.data; | |
tempa[0] = contextData[0]; | |
tempa[1] = contextData[1]; | |
tempa[2] = contextData[2]; | |
tempa[3] = contextData[3]; | |
tempa[4] = this.newInteger(bytePointer); | |
tempa[5] = this.smallInts[0]; | |
tempa[6] = contextData[6]; | |
tempa[7] = this.newInteger(low); | |
tempa[8] = context; | |
tempa[9] = this.newInteger(bytePointer); | |
stack[stackTop++] = returnedValue; | |
bytePointer = high; | |
break; | |
case 14: | |
if (__arguments == null) { | |
__arguments = contextData[1]; | |
} | |
if (instanceVariables == null) { | |
instanceVariables = __arguments.data[0].data; | |
} | |
stack[stackTop++] = __arguments.data[0].objClass.data[low + 5]; | |
break; | |
case 6: | |
if (__arguments == null) { | |
__arguments = contextData[1]; | |
} | |
if (instanceVariables == null) { | |
instanceVariables = __arguments.data[0].data; | |
} | |
instanceVariables[low] = stack[stackTop - 1]; | |
break; | |
case 7: | |
if (temporaries == null) { | |
temporaries = contextData[2].data; | |
} | |
temporaries[low] = stack[stackTop - 1]; | |
break; | |
case 8: | |
var newArguments = new SmallObject(this.ArrayClass, low); | |
tempa = newArguments.data; | |
while ((low > 0)) { | |
tempa[--low] = stack[--stackTop]; | |
} | |
; | |
stack[stackTop++] = newArguments; | |
break; | |
case 9: | |
__arguments = stack[--stackTop]; | |
contextData[5] = (stackTop < 10) ? this.smallInts[stackTop] : new SmallInt(this.IntegerClass, stackTop); | |
contextData[4] = (bytePointer < 10) ? this.smallInts[bytePointer] : new SmallInt(this.IntegerClass, bytePointer); | |
if (literals == null) { | |
literals = method.data[2].data; | |
} | |
returnedValue = literals[low]; | |
high = Math.abs(/* hashCode */ (function (o) { if (o.hashCode) { | |
return o.hashCode(); | |
} | |
else { | |
return o.toString().split('').reduce(function (prevHash, currVal) { return (((prevHash << 5) - prevHash) + currVal.charCodeAt(0)) | 0; }, 0); | |
} })(__arguments.data[0].objClass) + /* hashCode */ (function (o) { if (o.hashCode) { | |
return o.hashCode(); | |
} | |
else { | |
return o.toString().split('').reduce(function (prevHash, currVal) { return (((prevHash << 5) - prevHash) + currVal.charCodeAt(0)) | 0; }, 0); | |
} })(returnedValue)) % 197; | |
if ((selectorCache[high] != null) && (selectorCache[high] === returnedValue) && (classCache[high] === __arguments.data[0].objClass)) { | |
method = methodCache[high]; | |
cached++; | |
} | |
else { | |
method = this.methodLookup(__arguments.data[0].objClass, literals[low], context, __arguments); | |
lookup++; | |
selectorCache[high] = returnedValue; | |
classCache[high] = __arguments.data[0].objClass; | |
methodCache[high] = method; | |
} | |
context = this.buildContext(context, __arguments, method); | |
contextData = context.data; | |
continue outerLoop; | |
case 10: | |
if (low === 0) { | |
var arg = stack[--stackTop]; | |
stack[stackTop++] = (arg === this.nilObject) ? this.trueObject : this.falseObject; | |
} | |
else if (low === 1) { | |
var arg = stack[--stackTop]; | |
stack[stackTop++] = (arg !== this.nilObject) ? this.trueObject : this.falseObject; | |
} | |
else { | |
throw new SmallException("Illegal SendUnary " + low, context); | |
} | |
break; | |
case 11: | |
{ | |
if ((stack[stackTop - 1] != null && stack[stackTop - 1] instanceof SmallInt) && (stack[stackTop - 2] != null && stack[stackTop - 2] instanceof SmallInt)) { | |
var j = stack[--stackTop].value; | |
var i = stack[--stackTop].value; | |
var done = true; | |
switch ((low)) { | |
case 0: | |
returnedValue = (i < j) ? this.trueObject : this.falseObject; | |
break; | |
case 1: | |
returnedValue = (i <= j) ? this.trueObject : this.falseObject; | |
break; | |
case 2: | |
var li = i + (function (n) { return n < 0 ? Math.ceil(n) : Math.floor(n); })(j); | |
if (li !== (i + j)) { | |
done = false; | |
} | |
returnedValue = this.newInteger(i + j); | |
break; | |
} | |
if (done) { | |
stack[stackTop++] = returnedValue; | |
break; | |
} | |
else { | |
stackTop += 2; | |
} | |
} | |
__arguments = new SmallObject(this.ArrayClass, 2); | |
__arguments.data[1] = stack[--stackTop]; | |
__arguments.data[0] = stack[--stackTop]; | |
contextData[5] = this.newInteger(stackTop); | |
contextData[4] = this.newInteger(bytePointer); | |
var msg = null; | |
switch ((low)) { | |
case 0: | |
msg = new SmallByteArray(null, "<"); | |
break; | |
case 1: | |
msg = new SmallByteArray(null, "<="); | |
break; | |
case 2: | |
msg = new SmallByteArray(null, "+"); | |
break; | |
} | |
method = this.methodLookup(__arguments.data[0].objClass, msg, context, __arguments); | |
context = this.buildContext(context, __arguments, method); | |
contextData = context.data; | |
continue outerLoop; | |
} | |
; | |
case 13: | |
high = code[bytePointer++] & 255; | |
switch ((high)) { | |
case 1: | |
returnedValue = stack[--stackTop]; | |
if (returnedValue === stack[--stackTop]) { | |
returnedValue = this.trueObject; | |
} | |
else { | |
returnedValue = this.falseObject; | |
} | |
break; | |
case 2: | |
returnedValue = stack[--stackTop].objClass; | |
break; | |
case 4: | |
returnedValue = stack[--stackTop]; | |
if (returnedValue != null && returnedValue instanceof SmallByteArray) { | |
low = returnedValue.values.length; | |
} | |
else { | |
low = returnedValue.data.length; | |
} | |
returnedValue = this.newInteger(low); | |
break; | |
case 5: | |
low = stack[--stackTop].value; | |
returnedValue = stack[--stackTop]; | |
returnedValue.data[low - 1] = stack[--stackTop]; | |
break; | |
case 6: | |
break; | |
case 7: | |
low = stack[--stackTop].value; | |
returnedValue = new SmallObject(stack[--stackTop], low); | |
while ((low > 0)) { | |
returnedValue.data[--low] = this.nilObject; | |
} | |
; | |
break; | |
case 8: | |
{ | |
returnedValue = stack[--stackTop]; | |
high = returnedValue.data[7].value; | |
low -= 2; | |
if (low >= 0) { | |
temporaries = returnedValue.data[2].data; | |
while ((low >= 0)) { | |
{ | |
temporaries[high + low--] = stack[--stackTop]; | |
} | |
} | |
; | |
} | |
contextData[5] = this.newInteger(stackTop); | |
contextData[4] = this.newInteger(bytePointer); | |
var newContext = new SmallObject(this.ContextClass, 10); | |
for (var i = 0; i < 10; i++) { | |
newContext.data[i] = returnedValue.data[i]; | |
} | |
newContext.data[6] = contextData[6]; | |
newContext.data[5] = this.smallInts[0]; | |
newContext.data[4] = returnedValue.data[9]; | |
low = newContext.data[3].data.length; | |
newContext.data[3] = new SmallObject(this.ArrayClass, low); | |
context = newContext; | |
contextData = context.data; | |
continue outerLoop; | |
} | |
; | |
case 9: | |
break; | |
case 10: | |
{ | |
low = stack[--stackTop].value; | |
high = stack[--stackTop].value; | |
var lhigh = ((function (n) { return n < 0 ? Math.ceil(n) : Math.floor(n); })(high)) + (function (n) { return n < 0 ? Math.ceil(n) : Math.floor(n); })(low); | |
high += low; | |
if (lhigh === high) { | |
returnedValue = this.newInteger(high); | |
} | |
else { | |
returnedValue = this.nilObject; | |
} | |
} | |
; | |
break; | |
case 11: | |
low = stack[--stackTop].value; | |
high = stack[--stackTop].value; | |
high = (function (n) { return n < 0 ? Math.ceil(n) : Math.floor(n); })(high / low); | |
returnedValue = this.newInteger(high); | |
break; | |
case 12: | |
low = stack[--stackTop].value; | |
high = stack[--stackTop].value; | |
high %= low; | |
returnedValue = this.newInteger(high); | |
break; | |
case 14: | |
low = stack[--stackTop].value; | |
high = stack[--stackTop].value; | |
returnedValue = (low === high) ? this.trueObject : this.falseObject; | |
break; | |
case 15: | |
{ | |
low = stack[--stackTop].value; | |
high = stack[--stackTop].value; | |
var lhigh = ((function (n) { return n < 0 ? Math.ceil(n) : Math.floor(n); })(high)) * (function (n) { return n < 0 ? Math.ceil(n) : Math.floor(n); })(low); | |
high *= low; | |
if (lhigh === high) { | |
returnedValue = this.newInteger(high); | |
} | |
else { | |
returnedValue = this.nilObject; | |
} | |
} | |
; | |
break; | |
case 16: | |
{ | |
low = stack[--stackTop].value; | |
high = stack[--stackTop].value; | |
var lhigh = ((function (n) { return n < 0 ? Math.ceil(n) : Math.floor(n); })(high)) - (function (n) { return n < 0 ? Math.ceil(n) : Math.floor(n); })(low); | |
high -= low; | |
if (lhigh === high) { | |
returnedValue = this.newInteger(high); | |
} | |
else { | |
returnedValue = this.nilObject; | |
} | |
} | |
; | |
break; | |
case 17: | |
low = stack[--stackTop].value; | |
returnedValue = new SmallByteArray(stack[--stackTop], /* valueOf */ String(low).toString()); | |
break; | |
case 18: | |
returnedValue = stack[--stackTop]; | |
console.info("Debug " + returnedValue + " class " + returnedValue.objClass.data[0]); | |
break; | |
case 19: | |
break; | |
case 20: | |
low = stack[--stackTop].value; | |
returnedValue = new SmallByteArray(stack[--stackTop], low); | |
break; | |
case 21: | |
low = stack[--stackTop].value; | |
returnedValue = stack[--stackTop]; | |
var baa = returnedValue; | |
low = baa.values[low - 1] & 255; | |
returnedValue = this.newInteger(low); | |
break; | |
case 22: | |
low = stack[--stackTop].value; | |
var ba = stack[--stackTop]; | |
high = stack[--stackTop].value; | |
ba.values[low - 1] = (high | 0); | |
returnedValue = ba; | |
break; | |
case 23: | |
returnedValue = stack[--stackTop]; | |
returnedValue = stack[--stackTop].copy(returnedValue); | |
break; | |
case 24: | |
{ | |
var a = stack[--stackTop]; | |
var b = stack[--stackTop]; | |
low = a.values.length + b.values.length; | |
var n = new SmallByteArray(a.objClass, low); | |
high = 0; | |
for (var i = 0; i < a.values.length; i++) { | |
n.values[high++] = a.values[i]; | |
} | |
for (var i = 0; i < b.values.length; i++) { | |
n.values[high++] = b.values[i]; | |
} | |
returnedValue = n; | |
} | |
; | |
break; | |
case 26: | |
{ | |
var a = stack[--stackTop]; | |
var b = stack[--stackTop]; | |
low = a.values.length; | |
high = b.values.length; | |
var s = (low < high) ? low : high; | |
var r = 0; | |
for (var i = 0; i < s; i++) { | |
if (a.values[i] < b.values[i]) { | |
r = 1; | |
break; | |
} | |
else if (b.values[i] < a.values[i]) { | |
r = -1; | |
break; | |
} | |
; | |
} | |
if (r === 0) { | |
if (low < high) { | |
r = 1; | |
} | |
else if (low > high) { | |
r = -1; | |
} | |
} | |
returnedValue = this.newInteger(r); | |
} | |
; | |
break; | |
case 29: | |
{ | |
} | |
; | |
break; | |
case 30: | |
low = stack[--stackTop].value; | |
returnedValue = stack[--stackTop]; | |
returnedValue = returnedValue.data[low - 1]; | |
break; | |
case 31: | |
{ | |
var oldar = stack[--stackTop]; | |
low = oldar.data.length; | |
returnedValue = new SmallObject(oldar.objClass, low + 1); | |
for (var i = 0; i < low; i++) { | |
returnedValue.data[i] = oldar.data[i]; | |
} | |
returnedValue.data[low] = stack[--stackTop]; | |
} | |
; | |
break; | |
case 32: | |
{ | |
returnedValue = stack[--stackTop]; | |
low = returnedValue.data.length; | |
var na = (function (s) { var a = []; while (s-- > 0) | |
a.push(null); return a; })(low + 1); | |
for (var i = 0; i < low; i++) { | |
na[i] = returnedValue.data[i]; | |
} | |
na[low] = stack[--stackTop]; | |
returnedValue.data = na; | |
} | |
; | |
break; | |
case 33: | |
{ | |
} | |
; | |
break; | |
case 34: | |
{ | |
} | |
; | |
case 35: | |
returnedValue = context; | |
break; | |
case 36: | |
returnedValue = new SmallObject(this.ArrayClass, low); | |
for (var i = low - 1; i >= 0; i--) { | |
returnedValue.data[i] = stack[--stackTop]; | |
} | |
break; | |
case 41: | |
{ | |
} | |
; | |
break; | |
case 42: | |
{ | |
} | |
; | |
break; | |
case 43: | |
{ | |
} | |
; | |
break; | |
case 44: | |
{ | |
} | |
; | |
break; | |
case 50: | |
low = stack[--stackTop].value; | |
returnedValue = new SmallJavaObject(stack[--stackTop], low); | |
break; | |
case 51: | |
{ | |
var a = stack[--stackTop].value; | |
var b = stack[--stackTop].value; | |
returnedValue = new SmallJavaObject(stack[--stackTop], a + b); | |
} | |
; | |
break; | |
case 52: | |
{ | |
var a = stack[--stackTop].value; | |
var b = stack[--stackTop].value; | |
returnedValue = new SmallJavaObject(stack[--stackTop], a - b); | |
} | |
; | |
break; | |
case 53: | |
{ | |
var a = stack[--stackTop].value; | |
var b = stack[--stackTop].value; | |
returnedValue = new SmallJavaObject(stack[--stackTop], a * b); | |
} | |
; | |
break; | |
case 54: | |
{ | |
var a = stack[--stackTop].value; | |
var b = stack[--stackTop].value; | |
returnedValue = new SmallJavaObject(stack[--stackTop], a / b); | |
} | |
; | |
break; | |
case 55: | |
{ | |
var a = stack[--stackTop].value; | |
var b = stack[--stackTop].value; | |
returnedValue = (a < b) ? this.trueObject : this.falseObject; | |
} | |
; | |
break; | |
case 56: | |
{ | |
var a = stack[--stackTop].value; | |
var b = stack[--stackTop].value; | |
returnedValue = (a === b) ? this.trueObject : this.falseObject; | |
} | |
; | |
break; | |
case 57: | |
{ | |
var a = stack[--stackTop].value; | |
returnedValue = this.newInteger((a | 0)); | |
} | |
; | |
break; | |
case 58: | |
returnedValue = new SmallJavaObject(stack[--stackTop], Math.random()); | |
break; | |
case 59: | |
returnedValue = stack[--stackTop]; | |
returnedValue = new SmallByteArray(stack[--stackTop], /* valueOf */ String(/* doubleValue */ returnedValue.value).toString()); | |
break; | |
case 60: | |
{ | |
} | |
; | |
break; | |
case 61: | |
{ | |
} | |
; | |
break; | |
case 62: | |
{ | |
} | |
; | |
break; | |
case 63: | |
break; | |
case 64: | |
{ | |
} | |
; | |
break; | |
case 65: | |
{ | |
} | |
; | |
break; | |
case 66: | |
{ | |
} | |
; | |
break; | |
case 70: | |
{ | |
} | |
; | |
break; | |
case 71: | |
{ | |
} | |
; | |
break; | |
case 72: | |
case 73: | |
case 74: | |
{ | |
} | |
; | |
break; | |
case 75: | |
{ | |
} | |
; | |
break; | |
case 76: | |
{ | |
} | |
; | |
break; | |
case 77: | |
{ | |
} | |
; | |
break; | |
case 80: | |
{ | |
} | |
; | |
break; | |
case 81: | |
{ | |
} | |
; | |
break; | |
case 82: | |
{ | |
} | |
; | |
break; | |
case 83: | |
{ | |
} | |
; | |
break; | |
case 84: | |
{ | |
} | |
; | |
break; | |
case 85: | |
{ | |
} | |
; | |
break; | |
case 86: | |
{ | |
} | |
; | |
break; | |
case 87: | |
{ | |
} | |
; | |
break; | |
case 88: | |
{ | |
} | |
; | |
break; | |
case 90: | |
{ | |
} | |
; | |
break; | |
case 91: | |
{ | |
} | |
; | |
break; | |
case 100: | |
returnedValue = new SmallJavaObject(stack[--stackTop], new Sema()); | |
break; | |
case 101: | |
{ | |
var jo = stack[--stackTop]; | |
try { | |
returnedValue = jo.value.get(); | |
} | |
catch (e) { | |
returnedValue = this.nilObject; | |
} | |
} | |
; | |
break; | |
case 102: | |
{ | |
returnedValue = stack[--stackTop]; | |
var jo = stack[--stackTop]; | |
jo.value.set(returnedValue); | |
} | |
; | |
break; | |
case 110: | |
{ | |
} | |
; | |
break; | |
case 111: | |
{ | |
} | |
; | |
break; | |
case 113: | |
{ | |
} | |
; | |
break; | |
case 114: | |
{ | |
} | |
; | |
break; | |
case 115: | |
{ | |
} | |
; | |
break; | |
default: | |
throw new SmallException("Unknown Primitive " + high, context); | |
} | |
stack[stackTop++] = returnedValue; | |
break; | |
case 15: | |
switch ((low)) { | |
case 1: | |
if (__arguments == null) { | |
__arguments = contextData[1]; | |
} | |
returnedValue = __arguments.data[0]; | |
context = contextData[6]; | |
break innerLoop; | |
case 2: | |
returnedValue = stack[--stackTop]; | |
context = contextData[6]; | |
break innerLoop; | |
case 3: | |
returnedValue = stack[--stackTop]; | |
context = contextData[8]; | |
context = context.data[6]; | |
break innerLoop; | |
case 4: | |
returnedValue = stack[stackTop - 1]; | |
stack[stackTop++] = returnedValue; | |
break; | |
case 5: | |
stackTop--; | |
break; | |
case 6: | |
low = code[bytePointer++] & 255; | |
bytePointer = low; | |
break; | |
case 7: | |
low = code[bytePointer++] & 255; | |
returnedValue = stack[--stackTop]; | |
if (returnedValue === this.trueObject) { | |
bytePointer = low; | |
} | |
break; | |
case 8: | |
low = code[bytePointer++] & 255; | |
returnedValue = stack[--stackTop]; | |
if (returnedValue === this.falseObject) { | |
bytePointer = low; | |
} | |
break; | |
case 11: | |
low = code[bytePointer++] & 255; | |
__arguments = stack[--stackTop]; | |
contextData[5] = this.newInteger(stackTop); | |
contextData[4] = this.newInteger(bytePointer); | |
if (literals == null) { | |
literals = method.data[2].data; | |
} | |
if (method == null) { | |
method = context.data[0]; | |
} | |
method = method.data[5]; | |
method = method.data[1]; | |
method = this.methodLookup(method, literals[low], context, __arguments); | |
context = this.buildContext(context, __arguments, method); | |
contextData = context.data; | |
continue outerLoop; | |
default: | |
throw new SmallException("Unrecogized DoSpecial " + low, context); | |
} | |
break; | |
default: | |
throw new SmallException("Unrecogized opCode " + low, context); | |
} | |
} | |
} | |
; | |
if ((context == null) || (context === this.nilObject)) { | |
if (debug) { | |
console.info("lookups " + lookup + " cached " + cached); | |
} | |
return returnedValue; | |
} | |
contextData = context.data; | |
stack = contextData[3].data; | |
stackTop = contextData[5].value; | |
stack[stackTop++] = returnedValue; | |
contextData[5] = this.newInteger(stackTop); | |
} | |
} | |
; | |
}; | |
/*private*/ SmallInterpreter.prototype.methodLookup = function (receiver, messageSelector, context, __arguments) { | |
var name = messageSelector.toString(); | |
var cls; | |
for (cls = receiver; cls !== this.nilObject; cls = cls.data[1]) { | |
{ | |
var dict = cls.data[2]; | |
for (var i = 0; i < dict.data.length; i++) { | |
{ | |
var aMethod = dict.data[i]; | |
if (name === aMethod.data[0].toString()) { | |
return aMethod; | |
} | |
} | |
; | |
} | |
} | |
; | |
} | |
if (name === ("error:")) { | |
throw new SmallException("Unrecognized message selector: " + messageSelector, context); | |
} | |
var newArgs = [null, null]; | |
newArgs[0] = __arguments.data[0]; | |
newArgs[1] = new SmallByteArray(messageSelector.objClass, "Unrecognized message selector: " + name); | |
__arguments.data = newArgs; | |
return this.methodLookup(receiver, new SmallByteArray(messageSelector.objClass, "error:"), context, __arguments); | |
}; | |
SmallInterpreter.prototype.newInteger = function (val) { | |
if ((val >= 0) && (val < 10)) { | |
return this.smallInts[val]; | |
} | |
else { | |
return new SmallInt(this.IntegerClass, val); | |
} | |
}; | |
return SmallInterpreter; | |
}()); | |
SmallInterpreter["__class"] = "SmallInterpreter"; | |
var SmallJavaObject = /** @class */ (function (_super) { | |
__extends(SmallJavaObject, _super); | |
function SmallJavaObject(cls, v) { | |
var _this = _super.call(this, cls, 0) || this; | |
if (_this.value === undefined) { | |
_this.value = null; | |
} | |
_this.value = v; | |
return _this; | |
} | |
return SmallJavaObject; | |
}(SmallObject)); | |
SmallJavaObject["__class"] = "SmallJavaObject"; | |
var SmallByteArray = /** @class */ (function (_super) { | |
__extends(SmallByteArray, _super); | |
function SmallByteArray(cl, text) { | |
var _this = this; | |
if (((cl != null && cl instanceof SmallObject) || cl === null) && ((typeof text === 'string') || text === null)) { | |
var __args = arguments; | |
_this = _super.call(this, cl, 0) || this; | |
if (_this.values === undefined) { | |
_this.values = null; | |
} | |
var size = text.length; | |
_this.values = (function (s) { var a = []; while (s-- > 0) | |
a.push(0); return a; })(size); | |
for (var i = 0; i < size; i++) { | |
_this.values[i] = (text.charAt(i)).charCodeAt(0); | |
} | |
} | |
else if (((cl != null && cl instanceof SmallObject) || cl === null) && ((typeof text === 'number') || text === null)) { | |
var __args = arguments; | |
var size = __args[1]; | |
_this = _super.call(this, cl, 0) || this; | |
if (_this.values === undefined) { | |
_this.values = null; | |
} | |
_this.values = (function (s) { var a = []; while (s-- > 0) | |
a.push(0); return a; })(size); | |
} | |
else | |
throw new Error('invalid overload'); | |
return _this; | |
} | |
/** | |
* | |
* @param {SmallObject} cl | |
* @return {SmallObject} | |
*/ | |
SmallByteArray.prototype.copy = function (cl) { | |
var newObj = new SmallByteArray(cl, this.values.length); | |
for (var i = 0; i < this.values.length; i++) { | |
{ | |
newObj.values[i] = this.values[i]; | |
} | |
; | |
} | |
return newObj; | |
}; | |
/** | |
* | |
* @return {string} | |
*/ | |
SmallByteArray.prototype.toString = function () { | |
return String.fromCharCode.apply(null, this.values); | |
}; | |
return SmallByteArray; | |
}(SmallObject)); | |
SmallByteArray["__class"] = "SmallByteArray"; | |
var SmallInt = /** @class */ (function (_super) { | |
__extends(SmallInt, _super); | |
function SmallInt(IntegerClass, v) { | |
var _this = _super.call(this, IntegerClass, 0) || this; | |
if (_this.value === undefined) { | |
_this.value = 0; | |
} | |
_this.value = v; | |
return _this; | |
} | |
/** | |
* | |
* @return {string} | |
*/ | |
SmallInt.prototype.toString = function () { | |
return "SmallInt: " + this.value; | |
}; | |
return SmallInt; | |
}(SmallObject)); | |
SmallInt["__class"] = "SmallInt"; | |
Runner.main(null); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment