Skip to content

Instantly share code, notes, and snippets.

@dested
Created December 22, 2017 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dested/22b4fc55fb1b4b92166ddd6d50a36533 to your computer and use it in GitHub Desktop.
Save dested/22b4fc55fb1b4b92166ddd6d50a36533 to your computer and use it in GitHub Desktop.
function Evaluate(method, parameters) {
var instructionsOld = method.Instructions;
var stack = new Array(method.MaxStack);
var locals = new Array(method.LocalCount);
// var True = this.True;
// var False = this.False;
var lastStack;
var tempQ;
var tempIndex1;
var tempParameters;
var tempIndex2;
var stackIndex = 0;
var i = -1;
var j;
var instruction;
while (true) {
i++;
// coutner++;
instruction = instructionsOld[i];
switch (instruction.Type) {
case "AddInt":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = stack[tempIndex1] + stack[tempIndex2];
stackIndex = tempIndex2;
break;
case "AddString":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = stack[tempIndex1] + stack[tempIndex2];
stackIndex = tempIndex2;
break;
case "AddIntString":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = stack[tempIndex1] + stack[tempIndex2];
stackIndex = tempIndex2;
break;
case "AddStringInt":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = stack[tempIndex1] + stack[tempIndex2];
stackIndex = tempIndex2;
break;
case "Goto":
i = instruction.GotoIndex;
break;
case "Subtract":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = stack[tempIndex1] - stack[tempIndex2];
stackIndex = tempIndex2;
break;
case "Multiply":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = stack[tempIndex1] * stack[tempIndex2];
stackIndex = tempIndex2;
break;
case "Divide":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = stack[tempIndex1] / stack[tempIndex2];
stackIndex = tempIndex2;
break;
case "And":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = (stack[tempIndex1] && stack[tempIndex2]);
stackIndex = tempIndex2;
break;
case "Or":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = (stack[tempIndex1] || stack[tempIndex2]);
stackIndex = tempIndex2;
break;
case "Not":
tempIndex1 = stackIndex - 1;
stack[tempIndex1] = (!stack[tempIndex1]);
break;
case "LessThanEqual":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = (stack[tempIndex1] <= stack[tempIndex2]);
stackIndex = tempIndex2;
break;
case "BoolEqual":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = (stack[tempIndex1].Equals(stack[tempIndex2]));
stackIndex = tempIndex2;
break;
case "IntEqual":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = (stack[tempIndex1].Equals(stack[tempIndex2]));
stackIndex = tempIndex2;
break;
case "StringEqual":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = stack[tempIndex1] == stack[tempIndex2];
stackIndex = tempIndex2;
break;
case "LessThan":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = (stack[tempIndex1] < stack[tempIndex2]);
stackIndex = tempIndex2;
break;
case "GreaterThanEqual":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = (stack[tempIndex1] >= stack[tempIndex2]);
stackIndex = tempIndex2;
break;
case "GreaterThan":
tempIndex1 = stackIndex - 2;
tempIndex2 = stackIndex - 1;
stack[tempIndex1] = (stack[tempIndex1] > stack[tempIndex2]);
stackIndex = tempIndex2;
break;
case "GetParam":
stack[stackIndex++] = parameters[instruction.ParamIndex];
break;
case "GetParam0":
stack[stackIndex++] = parameters[0];
break;
case "GetParam1":
stack[stackIndex++] = parameters[1];
break;
case "GetParam2":
stack[stackIndex++] = parameters[2];
break;
case "GetParam3":
stack[stackIndex++] = parameters[3];
break;
case "GetIntConstant":
stack[stackIndex++] = instruction.IntConstant;
break;
case "GetIntConstant0":
stack[stackIndex++] = 0;
break;
case "GetIntConstant1":
stack[stackIndex++] = 1;
break;
case "GetIntConstant2":
stack[stackIndex++] = 2;
break;
case "GetIntConstant3":
stack[stackIndex++] = 3;
break;
case "GetIntConstant4":
stack[stackIndex++] = 4;
break;
case "GetBoolConstant":
stack[stackIndex++] = instruction.BoolConstant;
break;
case "GetStringConstant":
stack[stackIndex++] = instruction.StringConstant;
break;
case "StoreField":
lastStack = stack[--stackIndex];
lastStack.Fields[instruction.VariableID] = stack[--stackIndex];
break;
case "StoreToReference":
lastStack = stack[stackIndex - 2];
//same as store field without discarding the stack object
lastStack.Fields[instruction.VariableID] = stack[--stackIndex];
break;
case "StoreLocal":
locals[instruction.VariableID] = stack[--stackIndex];
break;
case "GetField":
tempIndex1 = stackIndex - 1;
stack[tempIndex1] = stack[tempIndex1].Fields[instruction.VariableID];
break;
case "PlusPlusLocal":
locals[instruction.VariableID]++;
break;
case "PlusPlusField":
lastStack = stack[--stackIndex];
lastStack.Fields[instruction.VariableID]++;
break;
case "GetLocal":
stack[stackIndex++] = locals[instruction.VariableID];
break;
case "CreateReference":
lastStack = createInstance(instruction.VariableID);
if (instruction.VariableID > -1) {
Evaluate(Methods[instruction.MethodID], [lastStack]);
}
stack[stackIndex++] = lastStack;
break;
case "CreateArray":
lastStack = createArray();
stack[stackIndex++] = lastStack;
break;
case "CallMethod":
tempParameters = new Array(instruction.ParamCount + 1);
for (j = instruction.ParamCount; j >= 0; j--) {
tempParameters[j] = stack[--stackIndex];
}
stack[stackIndex++] = Evaluate(Methods[instruction.MethodID], tempParameters);
break;
case "CallInternal":
tempParameters = new Array(instruction.ParamCount);
for (j = instruction.ParamCount - 1; j >= 0; j--) {
tempParameters[j] = stack[--stackIndex];
}
stack[stackIndex++] = InternalMethods[instruction.MethodID](tempParameters);
break;
case "BreakpointInstruction":
break;
case "AddToArray":
lastStack = stack[--stackIndex];
var dmb = stack[stackIndex - 1];
dmb.Items[dmb.CurrentArrayLength++] = lastStack;
break;
case "RemoveToArray":
// throw new Exception("g");
lastStack = stack[--stackIndex];
tempQ = stack[stackIndex - 1];
tempParameters = tempQ.Items;
for (j = tempQ.CurrentArraySize - 1; j >= 0; j--) {
if (tempParameters[j].Equals(lastStack)) {
tempQ.RemoveAtArray(j);
}
}
break;
case "InsertToArray":
tempParameters = new Array(2);
for (j = 2 - 1; j >= 0; j--) {
tempParameters[j] = stack[--stackIndex];
}
for (j = stack[stackIndex - 1].CurrentArraySize - 1; j >= tempParameters[0]; j--) {
ArrayItems[j + 1] = ArrayItems[j];
}
stack[stackIndex - 1].Items[tempParameters[0]] = tempParameters[1];
stack[stackIndex - 1].CurrentArraySize++;
break;
case "LengthOfArray":
lastStack = stack[--stackIndex];
stack[stackIndex++] = lastStack.CurrentArrayLength;
break;
case "ArrayElem":
lastStack = stack[--stackIndex];
stack[stackIndex - 1] = stack[stackIndex - 1].Items[lastStack];
break;
case "Return":
if (instruction.EmptyReturn) return null;
return stack[--stackIndex];
break;
case "PopStack":
stackIndex--;
break;
case "IsTrue":
if (stack[--stackIndex]) {
continue;
}
else {
if (instruction.ElseLine == -1) i = instruction.IfLine;
else i = instruction.ElseLine;
}
break;
default:
throw "Fuck";
}
}
return null;
}
function createInstance(count) {
return {
Fields: new Array(count)
};
}
function createArray() {
return {
Items: new Array(18),
CurrentArrayLength: 0
};
}
counter = 0;
InternalMethods = [function (f) {
counter++;
document.getElementById('someID').innerHTML += f[0];
}, function (f) {
counter++;
document.getElementById('someID').innerHTML += f[0]+"<br />";
}, function (f) {
counter++;
document.getElementById('someID').innerHTML += f[0];
}, function (f) {
return 0;
} //document.getElementById('someID').innerHTML = "";
, function (f) {
return input();
}, function (f) {
return 0;
}];
function doSome() {
var dccc = Evaluate(Methods[0], [createInstance(0)]);
alert(counter);
}
Methods = [{
MaxStack: 5,
LocalCount: 4,
Instructions: [{
Type: "CreateArray"
},
{
Type: "GetParam0"
},
{
Type: "StoreField",
VariableID: 0
},
{
Type: "CreateArray"
},
{
Type: "GetParam0"
},
{
Type: "StoreField",
VariableID: 1
},
{
Type: "CreateArray"
},
{
Type: "GetParam0"
},
{
Type: "StoreField",
VariableID: 2
},
{
Type: "GetIntConstant",
IntConstant: 20
},
{
Type: "StoreLocal",
VariableID: 0
},
{
Type: "GetStringConstant",
StringConstant: 'START'
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 2
},
{
Type: "PopStack"
},
{
Type: "GetParam0"
},
{
Type: "GetIntConstant1"
},
{
Type: "GetLocal",
VariableID: 0
},
{
Type: "GetIntConstant1"
},
{
Type: "CallMethod",
ParamCount: 3,
MethodID: 2
},
{
Type: "StoreLocal",
VariableID: 2
},
{
Type: "GetIntConstant0"
},
{
Type: "StoreLocal",
VariableID: 3
},
{
Type: "GetLocal",
VariableID: 3
},
{
Type: "GetLocal",
VariableID: 2
},
{
Type: "LengthOfArray"
},
{
Type: "LessThan"
},
{
Type: "IsTrue",
IfLine: -1,
ElseLine: 37
},
{
Type: "GetLocal",
VariableID: 2
},
{
Type: "GetLocal",
VariableID: 3
},
{
Type: "ArrayElem"
},
{
Type: "StoreLocal",
VariableID: 1
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 0
},
{
Type: "GetLocal",
VariableID: 1
},
{
Type: "AddToArray"
},
{
Type: "PopStack"
},
{
Type: "PlusPlusLocal",
VariableID: 3
},
{
Type: "Goto",
GotoIndex: 21
},
{
Type: "GetParam0"
},
{
Type: "CallMethod",
ParamCount: 0,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetParam0"
},
{
Type: "GetLocal",
VariableID: 0
},
{
Type: "GetIntConstant1"
},
{
Type: "Subtract"
},
{
Type: "GetStringConstant",
StringConstant: '1'
},
{
Type: "GetStringConstant",
StringConstant: '3'
},
{
Type: "GetStringConstant",
StringConstant: '2'
},
{
Type: "CallMethod",
ParamCount: 4,
MethodID: 3
},
{
Type: "PopStack"
},
{
Type: "GetStringConstant",
StringConstant: 'DONE'
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 2
},
{
Type: "PopStack"
},
{
Type: "Return",
EmptyReturn: true
}]
},
{
MaxStack: 2,
LocalCount: 13,
Instructions: [{
Type: "GetIntConstant1"
},
{
Type: "StoreLocal",
VariableID: 0
},
{
Type: "GetStringConstant",
StringConstant: ' '
},
{
Type: "StoreLocal",
VariableID: 1
},
{
Type: "GetStringConstant",
StringConstant: '0'
},
{
Type: "StoreLocal",
VariableID: 2
},
{
Type: "GetIntConstant0"
},
{
Type: "StoreLocal",
VariableID: 3
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 0
},
{
Type: "StoreLocal",
VariableID: 5
},
{
Type: "GetIntConstant0"
},
{
Type: "StoreLocal",
VariableID: 6
},
{
Type: "GetLocal",
VariableID: 6
},
{
Type: "GetLocal",
VariableID: 5
},
{
Type: "LengthOfArray"
},
{
Type: "LessThan"
},
{
Type: "IsTrue",
IfLine: -1,
ElseLine: 44
},
{
Type: "GetLocal",
VariableID: 5
},
{
Type: "GetLocal",
VariableID: 6
},
{
Type: "ArrayElem"
},
{
Type: "StoreLocal",
VariableID: 4
},
{
Type: "GetLocal",
VariableID: 3
},
{
Type: "GetLocal",
VariableID: 0
},
{
Type: "CallInternal",
ParamCount: 2,
MethodID: 5
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 4
},
{
Type: "GetIntConstant",
IntConstant: 10
},
{
Type: "LessThan"
},
{
Type: "IsTrue",
IfLine: 32,
ElseLine: -1
},
{
Type: "GetLocal",
VariableID: 2
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 4
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 1
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 0
},
{
Type: "GetIntConstant1"
},
{
Type: "AddInt"
},
{
Type: "StoreLocal",
VariableID: 0
},
{
Type: "PlusPlusLocal",
VariableID: 6
},
{
Type: "Goto",
GotoIndex: 12
},
{
Type: "GetIntConstant1"
},
{
Type: "StoreLocal",
VariableID: 0
},
{
Type: "GetLocal",
VariableID: 3
},
{
Type: "GetIntConstant3"
},
{
Type: "AddInt"
},
{
Type: "StoreLocal",
VariableID: 3
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 1
},
{
Type: "StoreLocal",
VariableID: 8
},
{
Type: "GetIntConstant0"
},
{
Type: "StoreLocal",
VariableID: 9
},
{
Type: "GetLocal",
VariableID: 9
},
{
Type: "GetLocal",
VariableID: 8
},
{
Type: "LengthOfArray"
},
{
Type: "LessThan"
},
{
Type: "IsTrue",
IfLine: -1,
ElseLine: 87
},
{
Type: "GetLocal",
VariableID: 8
},
{
Type: "GetLocal",
VariableID: 9
},
{
Type: "ArrayElem"
},
{
Type: "StoreLocal",
VariableID: 7
},
{
Type: "GetLocal",
VariableID: 3
},
{
Type: "GetLocal",
VariableID: 0
},
{
Type: "CallInternal",
ParamCount: 2,
MethodID: 5
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 7
},
{
Type: "GetIntConstant",
IntConstant: 10
},
{
Type: "LessThan"
},
{
Type: "IsTrue",
IfLine: 75,
ElseLine: -1
},
{
Type: "GetLocal",
VariableID: 2
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 7
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 1
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 0
},
{
Type: "GetIntConstant1"
},
{
Type: "AddInt"
},
{
Type: "StoreLocal",
VariableID: 0
},
{
Type: "PlusPlusLocal",
VariableID: 9
},
{
Type: "Goto",
GotoIndex: 55
},
{
Type: "GetIntConstant1"
},
{
Type: "StoreLocal",
VariableID: 0
},
{
Type: "GetLocal",
VariableID: 3
},
{
Type: "GetIntConstant3"
},
{
Type: "AddInt"
},
{
Type: "StoreLocal",
VariableID: 3
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 2
},
{
Type: "StoreLocal",
VariableID: 11
},
{
Type: "GetIntConstant0"
},
{
Type: "StoreLocal",
VariableID: 12
},
{
Type: "GetLocal",
VariableID: 12
},
{
Type: "GetLocal",
VariableID: 11
},
{
Type: "LengthOfArray"
},
{
Type: "LessThan"
},
{
Type: "IsTrue",
IfLine: -1,
ElseLine: 130
},
{
Type: "GetLocal",
VariableID: 11
},
{
Type: "GetLocal",
VariableID: 12
},
{
Type: "ArrayElem"
},
{
Type: "StoreLocal",
VariableID: 10
},
{
Type: "GetLocal",
VariableID: 3
},
{
Type: "GetLocal",
VariableID: 0
},
{
Type: "CallInternal",
ParamCount: 2,
MethodID: 5
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 10
},
{
Type: "GetIntConstant",
IntConstant: 10
},
{
Type: "LessThan"
},
{
Type: "IsTrue",
IfLine: 118,
ElseLine: -1
},
{
Type: "GetLocal",
VariableID: 2
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 10
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 1
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 0
},
{
Type: "GetIntConstant1"
},
{
Type: "AddInt"
},
{
Type: "StoreLocal",
VariableID: 0
},
{
Type: "PlusPlusLocal",
VariableID: 12
},
{
Type: "Goto",
GotoIndex: 98
},
{
Type: "Return",
EmptyReturn: true
}]
},
{
MaxStack: 2,
LocalCount: 2,
Instructions: [{
Type: "GetParam1"
},
{
Type: "StoreLocal",
VariableID: 0
},
{
Type: "CreateArray"
},
{
Type: "StoreLocal",
VariableID: 1
},
{
Type: "GetLocal",
VariableID: 0
},
{
Type: "GetParam2"
},
{
Type: "LessThan"
},
{
Type: "IsTrue",
IfLine: 16,
ElseLine: -1
},
{
Type: "GetLocal",
VariableID: 1
},
{
Type: "GetLocal",
VariableID: 0
},
{
Type: "AddToArray"
},
{
Type: "PopStack"
},
{
Type: "GetLocal",
VariableID: 0
},
{
Type: "GetParam3"
},
{
Type: "AddInt"
},
{
Type: "StoreLocal",
VariableID: 0
},
{
Type: "Goto",
GotoIndex: 3
},
{
Type: "GetLocal",
VariableID: 1
},
{
Type: "Return",
EmptyReturn: false
},
{
Type: "Return",
EmptyReturn: true
}]
},
{
MaxStack: 5,
LocalCount: 0,
Instructions: [{
Type: "GetParam1"
},
{
Type: "GetIntConstant0"
},
{
Type: "GreaterThan"
},
{
Type: "IsTrue",
IfLine: 27,
ElseLine: -1
},
{
Type: "GetParam0"
},
{
Type: "GetParam1"
},
{
Type: "GetIntConstant1"
},
{
Type: "Subtract"
},
{
Type: "GetParam2"
},
{
Type: "GetParam",
ParamIndex: 4
},
{
Type: "GetParam3"
},
{
Type: "CallMethod",
ParamCount: 4,
MethodID: 3
},
{
Type: "PopStack"
},
{
Type: "GetParam0"
},
{
Type: "GetParam1"
},
{
Type: "GetParam2"
},
{
Type: "GetParam3"
},
{
Type: "CallMethod",
ParamCount: 3,
MethodID: 4
},
{
Type: "PopStack"
},
{
Type: "GetParam0"
},
{
Type: "GetParam1"
},
{
Type: "GetIntConstant1"
},
{
Type: "Subtract"
},
{
Type: "GetParam",
ParamIndex: 4
},
{
Type: "GetParam3"
},
{
Type: "GetParam2"
},
{
Type: "CallMethod",
ParamCount: 4,
MethodID: 3
},
{
Type: "PopStack"
},
{
Type: "Return",
EmptyReturn: true
}]
},
{
MaxStack: 3,
LocalCount: 0,
Instructions: [{
Type: "GetIntConstant0"
},
{
Type: "GetIntConstant0"
},
{
Type: "CallInternal",
ParamCount: 2,
MethodID: 5
},
{
Type: "PopStack"
},
{
Type: "CallInternal",
ParamCount: 0,
MethodID: 3
},
{
Type: "PopStack"
},
{
Type: "GetStringConstant",
StringConstant: 'Disk '
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetParam1"
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetStringConstant",
StringConstant: ' From '
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetParam2"
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetStringConstant",
StringConstant: ' To '
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 1
},
{
Type: "PopStack"
},
{
Type: "GetParam3"
},
{
Type: "CallInternal",
ParamCount: 1,
MethodID: 2
},
{
Type: "PopStack"
},
{
Type: "GetParam2"
},
{
Type: "GetStringConstant",
StringConstant: '1'
},
{
Type: "StringEqual"
},
{
Type: "GetParam3"
},
{
Type: "GetStringConstant",
StringConstant: '2'
},
{
Type: "StringEqual"
},
{
Type: "And"
},
{
Type: "IsTrue",
IfLine: 43,
ElseLine: -1
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 0
},
{
Type: "GetParam1"
},
{
Type: "RemoveToArray"
},
{
Type: "PopStack"
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 1
},
{
Type: "GetIntConstant0"
},
{
Type: "GetParam1"
},
{
Type: "InsertToArray"
},
{
Type: "PopStack"
},
{
Type: "Return",
EmptyReturn: true
},
{
Type: "GetParam2"
},
{
Type: "GetStringConstant",
StringConstant: '1'
},
{
Type: "StringEqual"
},
{
Type: "GetParam3"
},
{
Type: "GetStringConstant",
StringConstant: '3'
},
{
Type: "StringEqual"
},
{
Type: "And"
},
{
Type: "IsTrue",
IfLine: 63,
ElseLine: -1
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 0
},
{
Type: "GetParam1"
},
{
Type: "RemoveToArray"
},
{
Type: "PopStack"
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 2
},
{
Type: "GetIntConstant0"
},
{
Type: "GetParam1"
},
{
Type: "InsertToArray"
},
{
Type: "PopStack"
},
{
Type: "Return",
EmptyReturn: true
},
{
Type: "GetParam2"
},
{
Type: "GetStringConstant",
StringConstant: '2'
},
{
Type: "StringEqual"
},
{
Type: "GetParam3"
},
{
Type: "GetStringConstant",
StringConstant: '1'
},
{
Type: "StringEqual"
},
{
Type: "And"
},
{
Type: "IsTrue",
IfLine: 83,
ElseLine: -1
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 1
},
{
Type: "GetParam1"
},
{
Type: "RemoveToArray"
},
{
Type: "PopStack"
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 0
},
{
Type: "GetIntConstant0"
},
{
Type: "GetParam1"
},
{
Type: "InsertToArray"
},
{
Type: "PopStack"
},
{
Type: "Return",
EmptyReturn: true
},
{
Type: "GetParam2"
},
{
Type: "GetStringConstant",
StringConstant: '2'
},
{
Type: "StringEqual"
},
{
Type: "GetParam3"
},
{
Type: "GetStringConstant",
StringConstant: '3'
},
{
Type: "StringEqual"
},
{
Type: "And"
},
{
Type: "IsTrue",
IfLine: 103,
ElseLine: -1
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 1
},
{
Type: "GetParam1"
},
{
Type: "RemoveToArray"
},
{
Type: "PopStack"
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 2
},
{
Type: "GetIntConstant0"
},
{
Type: "GetParam1"
},
{
Type: "InsertToArray"
},
{
Type: "PopStack"
},
{
Type: "Return",
EmptyReturn: true
},
{
Type: "GetParam2"
},
{
Type: "GetStringConstant",
StringConstant: '3'
},
{
Type: "StringEqual"
},
{
Type: "GetParam3"
},
{
Type: "GetStringConstant",
StringConstant: '2'
},
{
Type: "StringEqual"
},
{
Type: "And"
},
{
Type: "IsTrue",
IfLine: 123,
ElseLine: -1
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 2
},
{
Type: "GetParam1"
},
{
Type: "RemoveToArray"
},
{
Type: "PopStack"
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 1
},
{
Type: "GetIntConstant0"
},
{
Type: "GetParam1"
},
{
Type: "InsertToArray"
},
{
Type: "PopStack"
},
{
Type: "Return",
EmptyReturn: true
},
{
Type: "GetParam2"
},
{
Type: "GetStringConstant",
StringConstant: '3'
},
{
Type: "StringEqual"
},
{
Type: "GetParam3"
},
{
Type: "GetStringConstant",
StringConstant: '1'
},
{
Type: "StringEqual"
},
{
Type: "And"
},
{
Type: "IsTrue",
IfLine: 143,
ElseLine: -1
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 2
},
{
Type: "GetParam1"
},
{
Type: "RemoveToArray"
},
{
Type: "PopStack"
},
{
Type: "GetParam0"
},
{
Type: "GetField",
VariableID: 0
},
{
Type: "GetIntConstant0"
},
{
Type: "GetParam1"
},
{
Type: "InsertToArray"
},
{
Type: "PopStack"
},
{
Type: "Return",
EmptyReturn: true
},
{
Type: "Return",
EmptyReturn: true
}]
}];
/* ACG
Class Main
Peg1=Create Int|
Peg2=Create Int|
Peg3=Create Int|
Method Creator()
counts=20
WriteLine("START")
Loop This.MuRange(1,counts,1) Into R
This.Peg1.Add(R)
EndLoop
This.Draw()
This. doHanoi(counts-1, "1", "3", "2")
WriteLine("DONE")
EndMethod
Method Draw()
i=1
s=" "
s2="0"
G=0
Loop This.Peg1 Into P1
SetConsolePosition(G, i)
if P1<10
Write(s2)
Endif
Write(P1)
Write(s)
i=i+1
EndLoop
i=1
G=G+3
Loop This.Peg2 Into P2
SetConsolePosition(G, i)
if P2<10
Write(s2)
Endif
Write(P2)
Write(s)
i=i+1
EndLoop
i=1
G=G+3
Loop This.Peg3 Into P3
SetConsolePosition(G, i)
if P3<10
Write(s2)
Endif
Write(P3)
Write(s)
i=i+1
EndLoop
EndMethod
Method MuRange(start,finish,countr)
i=start
items=Create Int|
Loop While i<finish
items.Add(i)
i=i+countr
EndLoop
Return items
EndMethod
Method doHanoi(n, f, t, u)
if n >0
This. doHanoi(n - 1, f, u, t)
This. Move(n,f,t)
! This.Draw()
This. doHanoi(n - 1, u, t, f)
EndIf
EndMethod
Method Move(n,f,t)
SetConsolePosition(0,0)
ClearScreen()
Write("Disk ")
Write(n)
Write(" From ")
Write(f)
Write(" To ")
WriteLine(t)
//ReadLine()
if f="1" And (t="2")
This.Peg1.Remove(n)
This.Peg2.Insert(0,n)
Return
EndIf
if f="1" And (t="3")
This.Peg1.Remove(n)
This.Peg3.Insert(0,n)
Return
EndIf
if f="2" And (t="1")
This.Peg2.Remove(n)
This.Peg1.Insert(0,n)
Return
EndIf
if f="2" And (t="3")
This.Peg2.Remove(n)
This.Peg3.Insert(0,n)
Return
EndIf
if f="3" And (t="2")
This.Peg3.Remove(n)
This.Peg2.Insert(0,n)
Return
EndIf
if f="3" And (t="1")
This.Peg3.Remove(n)
This.Peg1.Insert(0,n)
Return
EndIf
EndMethod
EndClass
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment