Skip to content

Commit

Permalink
remove evaluator readint32 method
Browse files Browse the repository at this point in the history
  • Loading branch information
SenkJu committed Sep 5, 2020
1 parent 90991e8 commit 3cc7a02
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/evaluator/Evaluator.hx
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ class Evaluator {
final right = stack.pop();
final left = stack.pop();

if (left.type != ObjectType.Float || right.type != ObjectType.Float) {
var cRight;
var cLeft;

try {
cRight = cast(right, FloatObj).value;
cLeft = cast(left, FloatObj).value;
} catch(e) {
error.error('cannot perform operation $opCode on left (${left.type}) and right (${right.type}) value');
return;
}

final cRight = cast(right, FloatObj).value;
final cLeft = cast(left, FloatObj).value;

final result:Float = switch (opCode) {
case OpCode.Add: cLeft + cRight;
case OpCode.Multiply: cLeft * cRight;
Expand All @@ -97,11 +101,11 @@ class Evaluator {

stack.add(new FloatObj(result));
case OpCode.Constant:
final constantIndex = readInt32();
final constantIndex = byteCode.readInt32();

stack.add(constants[constantIndex]);
case OpCode.SetLocal:
final localIndex = readInt32();
final localIndex = byteCode.readInt32();

final value = stack.pop();

Expand All @@ -111,7 +115,7 @@ class Evaluator {

env.setVariable(localIndex, value);
case OpCode.GetLocal:
final localIndex = readInt32();
final localIndex = byteCode.readInt32();

final value = env.getVariable(localIndex);

Expand All @@ -121,18 +125,18 @@ class Evaluator {

stack.add(value);
case OpCode.GetBuiltIn:
final builtInIndex = readInt32();
final builtInIndex = byteCode.readInt32();

stack.add(new FunctionObj(builtInIndex, ObjectOrigin.BuiltIn));
case OpCode.JumpNot:
final jumpIndex = readInt32();
final jumpIndex = byteCode.readInt32();

final conditionValue = cast(stack.pop(), FloatObj);
if (conditionValue.value == 0) {
byteCode.position = jumpIndex;
}
case OpCode.Jump:
final jumpIndex = readInt32();
final jumpIndex = byteCode.readInt32();

byteCode.position = jumpIndex;
case OpCode.Call:
Expand All @@ -159,9 +163,4 @@ class Evaluator {

}
}

function readInt32():Int {
final value = byteCode.readInt32();
return value;
}
}

0 comments on commit 3cc7a02

Please sign in to comment.