This commit is contained in:
Andre Wagner
2026-05-01 16:39:49 -05:00
parent bebaed1998
commit 0bc40cc562
9 changed files with 147 additions and 18 deletions

View File

@@ -5,13 +5,14 @@
namespace tyche::vm {
void VM::load_bytecode(ByteArray const& ba)
VM& VM::load_bytecode(ByteArray const& ba)
{
FunctionId f_id = code_.import_bytecode(ba);
stack_.push(Value::CreateFunctionId(f_id));
return *this;
}
void VM::call(size_t n_params)
VM& VM::call(size_t n_params)
{
// TODO - parameters
@@ -24,6 +25,8 @@ void VM::call(size_t n_params)
run_until_return();
// stack_.pop_fp();
loc_.pop();
return *this;
}
int32_t VM::to_integer(int index) const
@@ -33,9 +36,10 @@ int32_t VM::to_integer(int index) const
return i.as_integer();
}
void VM::push_integer(int32_t value)
VM& VM::push_integer(int32_t value)
{
stack_.push(Value::CreateInteger(value));
return *this;
}
void VM::run_until_return()
@@ -58,6 +62,45 @@ void VM::step()
case Instruction::Sum:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Sum));
break;
case Instruction::Subtract:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Subtraction));
break;
case Instruction::Multiply:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Multiplication));
break;
case Instruction::Divide:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Division));
break;
case Instruction::DivideInt:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::IntegerDivision));
break;
case Instruction::Equals:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Equality));
break;
case Instruction::NotEquals:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Inequality));
break;
case Instruction::LessThan:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::LessThan));
break;
case Instruction::LessThanEq:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::LessThanOrEquals));
break;
case Instruction::GreaterThan:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::GreaterThan));
break;
case Instruction::GreaterThanEq:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::GreaterThanOrEquals));
break;
case Instruction::And:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::BitwiseAnd));
break;
case Instruction::Or:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::BitwiseOr));
break;
case Instruction::Xor:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::BitwiseXor));
break;
case Instruction::Return: {
Value v = stack_.pop();
stack_.pop_fp();