mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
DEV: Modulus operator works.
This commit is contained in:
parent
a36322461e
commit
40fc6fe15f
@ -103,6 +103,7 @@ pub enum Op {
|
|||||||
Sub,
|
Sub,
|
||||||
Div,
|
Div,
|
||||||
Mul,
|
Mul,
|
||||||
|
Mod,
|
||||||
// Comparison Ops
|
// Comparison Ops
|
||||||
Equal,
|
Equal,
|
||||||
Gt,
|
Gt,
|
||||||
|
@ -588,6 +588,8 @@ fn simple_binary_expr() {
|
|||||||
"2-1;" => P(Int(1)),
|
"2-1;" => P(Int(1)),
|
||||||
"2*2;" => P(Int(4)),
|
"2*2;" => P(Int(4)),
|
||||||
"6/2;" => P(Int(3)),
|
"6/2;" => P(Int(3)),
|
||||||
|
"4 %% 2;" => P(Int(0)),
|
||||||
|
"5 %% 2;" => P(Int(1)),
|
||||||
"1.0+1.0;" => P(Float(2.0)),
|
"1.0+1.0;" => P(Float(2.0)),
|
||||||
"\"foo\"+\"bar\";" => P(Str("foobar".to_owned())),
|
"\"foo\"+\"bar\";" => P(Str("foobar".to_owned())),
|
||||||
"1==1;" => P(Bool(true)),
|
"1==1;" => P(Bool(true)),
|
||||||
|
@ -131,7 +131,12 @@ impl AST {
|
|||||||
let jptr = (ops.len() - 1 - idx) as i32;
|
let jptr = (ops.len() - 1 - idx) as i32;
|
||||||
ops[idx] = Op::Or(jptr);
|
ops[idx] = Op::Or(jptr);
|
||||||
}
|
}
|
||||||
BinaryExprType::IN | BinaryExprType::Mod | BinaryExprType::DOT => {
|
BinaryExprType::Mod => {
|
||||||
|
Self::translate_expr(*def.right, &mut ops);
|
||||||
|
Self::translate_expr(*def.left, &mut ops);
|
||||||
|
ops.push(Op::Mod);
|
||||||
|
}
|
||||||
|
BinaryExprType::IN | BinaryExprType::DOT => {
|
||||||
unimplemented!("Binary expressions are not implmented yet")
|
unimplemented!("Binary expressions are not implmented yet")
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
@ -83,6 +83,7 @@ impl<'a> VM {
|
|||||||
Op::Sym(s) => self.push(Rc::new(S(s.clone())))?,
|
Op::Sym(s) => self.push(Rc::new(S(s.clone())))?,
|
||||||
Op::DeRef(s) => self.op_deref(s.clone())?,
|
Op::DeRef(s) => self.op_deref(s.clone())?,
|
||||||
Op::Add => self.op_add()?,
|
Op::Add => self.op_add()?,
|
||||||
|
Op::Mod => self.op_mod()?,
|
||||||
Op::Sub => self.op_sub()?,
|
Op::Sub => self.op_sub()?,
|
||||||
Op::Mul => self.op_mul()?,
|
Op::Mul => self.op_mul()?,
|
||||||
Op::Div => self.op_div()?,
|
Op::Div => self.op_div()?,
|
||||||
@ -398,6 +399,15 @@ impl<'a> VM {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn op_mod(&mut self) -> Result<(), Error> {
|
||||||
|
// Adds the previous two items in the stack.
|
||||||
|
let left = self.pop()?;
|
||||||
|
let right = self.pop()?;
|
||||||
|
// Then pushes the result onto the stack.
|
||||||
|
self.push(Rc::new(P(self.modulus(&left, &right)?)))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn op_add(&mut self) -> Result<(), Error> {
|
fn op_add(&mut self) -> Result<(), Error> {
|
||||||
// Adds the previous two items in the stack.
|
// Adds the previous two items in the stack.
|
||||||
let left = self.pop()?;
|
let left = self.pop()?;
|
||||||
@ -674,6 +684,14 @@ impl<'a> VM {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn modulus(&self, left: &Value, right: &Value) -> Result<Primitive, Error> {
|
||||||
|
Ok(match (left, right) {
|
||||||
|
(P(Int(i)), Value::P(Int(ii))) => Int(i % ii),
|
||||||
|
(P(Float(f)), Value::P(Float(ff))) => Float(f % ff),
|
||||||
|
_ => return Err(dbg!(Error {})),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn add(&self, left: &Value, right: &Value) -> Result<Primitive, Error> {
|
fn add(&self, left: &Value, right: &Value) -> Result<Primitive, Error> {
|
||||||
Ok(match (left, right) {
|
Ok(match (left, right) {
|
||||||
(P(Int(i)), Value::P(Int(ii))) => Int(i + ii),
|
(P(Int(i)), Value::P(Int(ii))) => Int(i + ii),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user