This commit is contained in:
2026-05-01 22:04:21 -05:00
parent 9e6d9c6212
commit 5299ae9d63
2 changed files with 35 additions and 18 deletions

View File

@@ -17,7 +17,6 @@ static int init_ = []() {
throw VMInvalidOperation((BinaryOperationType) i, a.type(), b.type()); throw VMInvalidOperation((BinaryOperationType) i, a.type(), b.type());
}; };
// TODO - create macro
#define BIN_OP(op, t1, t2) binary_ops[(size_t) BinaryOperationType::op][(size_t) Type::t1][(size_t) Type::t2] = [](Value const& b, Value const& a) #define BIN_OP(op, t1, t2) binary_ops[(size_t) BinaryOperationType::op][(size_t) Type::t1][(size_t) Type::t2] = [](Value const& b, Value const& a)
BIN_OP(Sum, Integer, Integer) { return Value::CreateInteger(a.as_integer() + b.as_integer()); }; BIN_OP(Sum, Integer, Integer) { return Value::CreateInteger(a.as_integer() + b.as_integer()); };

View File

@@ -53,6 +53,7 @@ void VM::run_until_return()
void VM::step() void VM::step()
{ {
Operation op = code_.operation(loc_.top()); Operation op = code_.operation(loc_.top());
Value a, b;
switch (op.instruction) { switch (op.instruction) {
case Instruction::PushInt8: case Instruction::PushInt8:
case Instruction::PushInt16: case Instruction::PushInt16:
@@ -60,55 +61,72 @@ void VM::step()
push_integer(op.operator_); push_integer(op.operator_);
break; break;
case Instruction::Sum: case Instruction::Sum:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Sum)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Sum));
break; break;
case Instruction::Subtract: case Instruction::Subtract:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Subtraction)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Subtraction));
break; break;
case Instruction::Multiply: case Instruction::Multiply:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Multiplication)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Multiplication));
break; break;
case Instruction::Divide: case Instruction::Divide:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Division)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Division));
break; break;
case Instruction::DivideInt: case Instruction::DivideInt:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::IntegerDivision)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::IntegerDivision));
break; break;
case Instruction::Equals: case Instruction::Equals:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Equality)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Equality));
break; break;
case Instruction::NotEquals: case Instruction::NotEquals:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Inequality)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Inequality));
break; break;
case Instruction::LessThan: case Instruction::LessThan:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::LessThan)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::LessThan));
break; break;
case Instruction::LessThanEq: case Instruction::LessThanEq:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::LessThanOrEquals)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::LessThanOrEquals));
break; break;
case Instruction::GreaterThan: case Instruction::GreaterThan:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::GreaterThan)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::GreaterThan));
break; break;
case Instruction::GreaterThanEq: case Instruction::GreaterThanEq:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::GreaterThanOrEquals)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::GreaterThanOrEquals));
break; break;
case Instruction::And: case Instruction::And:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::BitwiseAnd)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::BitwiseAnd));
break; break;
case Instruction::Or: case Instruction::Or:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::BitwiseOr)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::BitwiseOr));
break; break;
case Instruction::Xor: case Instruction::Xor:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::BitwiseXor)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::BitwiseXor));
break; break;
case Instruction::Power: case Instruction::Power:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Power)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::Power));
break; break;
case Instruction::ShiftLeft: case Instruction::ShiftLeft:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::ShiftLeft)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::ShiftLeft));
break; break;
case Instruction::ShiftRight: case Instruction::ShiftRight:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::ShiftRight)); a = stack_.pop(); b = stack_.pop();
stack_.push(binary_operation(a, b, BinaryOperationType::ShiftRight));
break; break;
case Instruction::Return: { case Instruction::Return: {
Value v = stack_.pop(); Value v = stack_.pop();