integer math where possible

This commit is contained in:
Miguel M 2023-02-22 17:45:13 +00:00
parent 179152a278
commit a5ea324114
3 changed files with 139 additions and 68 deletions

View File

@ -2,6 +2,8 @@ pub mod parsing;
use parsing::ast;
use crate::vm::parsing::ast::ComparisonOperator;
type Bytecode = (OpCode, usize);
#[derive(Debug)]
@ -17,22 +19,23 @@ pub struct Vm<'code> {
#[derive(Debug)]
pub struct Registers {
x: u128,
y: u128,
x: i64,
y: i64,
n: u32,
p: u32,
k: u32,
xpop: u32,
ypop: u32,
npop: u32,
ppop: u32,
kpop: u32,
}
#[derive(Debug)]
pub enum VmOutput {
Boolean(bool),
Arithmetic(f64),
Arithmetic(ArithmeticValue),
}
#[derive(Debug)]
pub enum ArithmeticValue {
Integer(i64),
Floating(f64),
}
#[derive(Debug)]
@ -43,7 +46,7 @@ enum OpCode {
UnaryArithmeticOperator(ast::UnaryArithmeticOperator),
BinaryBooleanOperator(ast::BinaryBooleanOperator),
Variable(ast::Variable),
Literal(usize),
Literal(i64),
}
enum Expression {
@ -52,19 +55,8 @@ enum Expression {
}
impl Registers {
pub fn load(x: u128, y: u128, n: u32, p: u32, k: u32) -> Self {
Self {
x,
y,
n,
p,
k,
xpop: x.count_ones(),
ypop: y.count_ones(),
npop: n.count_ones(),
ppop: p.count_ones(),
kpop: k.count_ones(),
}
pub fn load(x: i64, y: i64, n: u32, p: u32, k: u32) -> Self {
Self { x, y, n, p, k }
}
}
@ -121,25 +113,48 @@ impl<'code> Vm<'code> {
),
};
match op {
ast::ComparisonOperator::GreaterOrEqual => {
VmOutput::Boolean(left_operand >= right_operand)
}
ast::ComparisonOperator::LessOrEqual => {
VmOutput::Boolean(left_operand <= right_operand)
}
ast::ComparisonOperator::GreaterThan => {
VmOutput::Boolean(left_operand > right_operand)
}
ast::ComparisonOperator::LessThan => {
VmOutput::Boolean(left_operand < right_operand)
}
ast::ComparisonOperator::NotEqual => {
VmOutput::Boolean(left_operand != right_operand)
}
ast::ComparisonOperator::Equal => {
VmOutput::Boolean(left_operand == right_operand)
}
fn comparison<T: std::cmp::PartialOrd>(
op: &ComparisonOperator,
left_operand: T,
right_operand: T,
) -> VmOutput {
VmOutput::Boolean(match op {
ast::ComparisonOperator::GreaterOrEqual => {
left_operand.ge(&right_operand)
}
ast::ComparisonOperator::LessOrEqual => {
left_operand.le(&right_operand)
}
ast::ComparisonOperator::GreaterThan => {
left_operand.gt(&right_operand)
}
ast::ComparisonOperator::LessThan => {
left_operand.lt(&right_operand)
}
ast::ComparisonOperator::NotEqual => {
left_operand.ne(&right_operand)
}
ast::ComparisonOperator::Equal => left_operand.eq(&right_operand),
})
};
match left_operand {
ArithmeticValue::Integer(left_operand) => match right_operand {
ArithmeticValue::Integer(right_operand) => {
comparison(op, left_operand, right_operand)
}
ArithmeticValue::Floating(right_operand) => {
comparison(op, left_operand as f64, right_operand)
}
},
ArithmeticValue::Floating(left_operand) => match right_operand {
ArithmeticValue::Integer(right_operand) => {
comparison(op, left_operand, right_operand as f64)
}
ArithmeticValue::Floating(right_operand) => {
comparison(op, left_operand, right_operand)
}
},
}
}
OpCode::BinaryArithmeticOperator(op) => {
@ -156,35 +171,90 @@ impl<'code> Vm<'code> {
),
};
match op {
let operations_as_integer = |left_operand: i64, right_operand: i64| match op
{
ast::BinaryArithmeticOperator::Times => {
VmOutput::Arithmetic(left_operand * right_operand)
ArithmeticValue::Integer(left_operand * right_operand)
}
ast::BinaryArithmeticOperator::Divide => {
VmOutput::Arithmetic(left_operand / right_operand)
if left_operand % right_operand == 0 {
ArithmeticValue::Integer(left_operand / right_operand)
} else {
ArithmeticValue::Floating(
left_operand as f64 / right_operand as f64,
)
}
}
ast::BinaryArithmeticOperator::Plus => {
VmOutput::Arithmetic(left_operand + right_operand)
ArithmeticValue::Integer(left_operand + right_operand)
}
ast::BinaryArithmeticOperator::Minus => {
VmOutput::Arithmetic(left_operand - right_operand)
ArithmeticValue::Integer(left_operand - right_operand)
}
ast::BinaryArithmeticOperator::Xor => VmOutput::Arithmetic(
((left_operand as u128) ^ (right_operand as u128)) as f64,
),
}
ast::BinaryArithmeticOperator::Xor => {
ArithmeticValue::Integer(left_operand ^ right_operand)
}
};
let operations_as_floating =
|left_operand: f64, right_operand: f64| match op {
ast::BinaryArithmeticOperator::Times => {
ArithmeticValue::Floating(left_operand * right_operand)
}
ast::BinaryArithmeticOperator::Divide => ArithmeticValue::Floating(
left_operand as f64 / right_operand as f64,
),
ast::BinaryArithmeticOperator::Plus => {
ArithmeticValue::Floating(left_operand + right_operand)
}
ast::BinaryArithmeticOperator::Minus => {
ArithmeticValue::Floating(left_operand - right_operand)
}
ast::BinaryArithmeticOperator::Xor => ArithmeticValue::Integer(
left_operand as i64 ^ right_operand as i64,
),
};
VmOutput::Arithmetic(match left_operand {
ArithmeticValue::Integer(left_operand) => match right_operand {
ArithmeticValue::Integer(right_operand) => {
operations_as_integer(left_operand, right_operand)
}
ArithmeticValue::Floating(right_operand) => {
operations_as_floating(left_operand as f64, right_operand)
}
},
ArithmeticValue::Floating(left_operand) => match right_operand {
ArithmeticValue::Integer(right_operand) => {
operations_as_floating(left_operand, right_operand as f64)
}
ArithmeticValue::Floating(right_operand) => {
operations_as_floating(left_operand, right_operand)
}
},
})
}
OpCode::UnaryArithmeticOperator(op) => {
let value = match (resolver.f)(resolver, op_index + 1) {
VmOutput::Arithmetic(value) => value,
VmOutput::Boolean(_) => panic!("Bad bytecode! Arithmetic unary operator followed by boolean resolution."),
};
match op {
ast::UnaryArithmeticOperator::Negative => VmOutput::Arithmetic(-value),
VmOutput::Arithmetic(match op {
ast::UnaryArithmeticOperator::Negative => match value {
ArithmeticValue::Integer(value) => ArithmeticValue::Integer(-value),
ArithmeticValue::Floating(value) => {
ArithmeticValue::Floating(-value)
}
},
ast::UnaryArithmeticOperator::Ham => {
VmOutput::Arithmetic((value as u128).count_ones() as f64)
ArithmeticValue::Integer(match value {
ArithmeticValue::Integer(value) => value.count_ones() as i64,
ArithmeticValue::Floating(value) => {
(value as u64).count_ones() as i64
}
})
}
}
})
}
OpCode::BinaryBooleanOperator(op) => {
let left_operand = match (resolver.f)(resolver, op_index + 1) {
@ -206,14 +276,16 @@ impl<'code> Vm<'code> {
ast::BinaryBooleanOperator::Xor => left_operand ^ right_operand,
})
}
OpCode::Variable(var) => VmOutput::Arithmetic(match var {
ast::Variable::X => registers.x,
ast::Variable::Y => registers.y,
ast::Variable::N => registers.n as u128,
ast::Variable::P => registers.p as u128,
ast::Variable::K => registers.k as u128,
} as f64),
OpCode::Literal(lit) => VmOutput::Arithmetic(*lit as f64),
OpCode::Variable(var) => {
VmOutput::Arithmetic(ArithmeticValue::Integer(match var {
ast::Variable::X => registers.x,
ast::Variable::Y => registers.y,
ast::Variable::N => registers.n as i64,
ast::Variable::P => registers.p as i64,
ast::Variable::K => registers.k as i64,
}))
}
OpCode::Literal(lit) => VmOutput::Arithmetic(ArithmeticValue::Integer(*lit)),
}
},
};
@ -318,19 +390,18 @@ fn compile_expression(expression: Expression, code: &mut Vec<Bytecode>) -> usize
#[test]
fn boolean_compilation_test() {
let expression =
parsing::parse_relation("and and < x 1 != 1 y < ham x k").expect("Valid AST");
let expression = parsing::parse_relation("and (and (< x 1) (!= 1 y)) (< ham x k)").expect("Valid AST");
println!("{:?}", Vm::compile_boolean(expression));
}
#[test]
fn run_test() {
let expression = parsing::parse_relation("(= (ham (^ x y)) 1)");
let expression = parsing::parse_relation("(= (ham (^ x y)) (ham 1))");
if let Err(e) = expression {
println!("{}", e);
panic!();
}
let code = Vm::compile_boolean(expression.unwrap());
let vm = Vm::load(&code, Registers::load(0b_1001, 0b_1111, 10, 2, 3));
let vm = Vm::load(&code, Registers::load(0b_1011, 0b_1111, 10, 2, 3));
println!("{:?}", vm.run());
}

View File

@ -82,7 +82,7 @@ pub struct BinaryArithmeticConjunction {
#[derive(Debug)]
pub enum ArithmeticOperand {
Literal(usize),
Literal(i64),
Expression(Box<ArithmeticExpression>),
}

View File

@ -185,7 +185,7 @@ fn parse_arithmetic_operand(rule: pest::iterators::Pair<Rule>) -> ArithmeticOper
let inner = rule.into_inner().next().unwrap();
match inner.as_rule() {
Rule::number_literal => {
let inner = inner.as_str().trim().parse::<usize>().unwrap();
let inner = inner.as_str().trim().parse::<i64>().unwrap();
ArithmeticOperand::Literal(inner)
}
Rule::arithmetic_expression => {