Expressions #7

Merged
andre merged 21 commits from vm2 into master 2026-05-02 15:07:12 -05:00
2 changed files with 35 additions and 18 deletions
Showing only changes of commit 5299ae9d63 - Show all commits

View File

@@ -17,7 +17,6 @@ static int init_ = []() {
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)
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()
{
Operation op = code_.operation(loc_.top());
Value a, b;
switch (op.instruction) {
case Instruction::PushInt8:
case Instruction::PushInt16:
@@ -60,55 +61,72 @@ void VM::step()
push_integer(op.operator_);
break;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
case Instruction::Return: {
Value v = stack_.pop();