Skip to content

Commit

Permalink
Merge pull request #32 from nivpgir/master
Browse files Browse the repository at this point in the history
Implement a remainder operator through the ffi
  • Loading branch information
slightknack authored Mar 30, 2021
2 parents 9d31080 + b0a0baa commit 6de5bb0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/compiler/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl Lexer {
Box::new(Lexer::mul),
Box::new(Lexer::div),
Box::new(Lexer::equal),
Box::new(Lexer::remainder),
Box::new(Lexer::magic),
Box::new(Lexer::print), // remove print statements after FFI

Expand Down Expand Up @@ -269,6 +270,9 @@ impl Lexer {
Lexer::literal(source, "==", Token::Equal)
}

pub fn remainder(source: &str) -> Result<Bite, String> {
Lexer::literal(source, "%", Token::Rem)
}
/// Matches a `print` expression.
pub fn print(source: &str) -> Result<Bite, String> {
Lexer::literal(source, "print", Token::Print)
Expand Down
9 changes: 8 additions & 1 deletion src/compiler/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl Parser {
Token::Sub => self.sub(left),
Token::Mul => self.mul(left),
Token::Div => self.div(left),
Token::Rem => self.remainder(left),

Token::Equal => self.equal(left),

Expand Down Expand Up @@ -208,7 +209,8 @@ impl Parser {
| Token::Sub => Prec::AddSub,

Token::Mul
| Token::Div => Prec::MulDiv,
| Token::Div
| Token::Rem => Prec::MulDiv,

// postfix
Token::End
Expand Down Expand Up @@ -549,6 +551,11 @@ impl Parser {
return self.binop(Token::Equal, Prec::Logic, "equal", left);
}

/// Parses an equality, calls out to FFI.
pub fn remainder(&mut self, left: Spanned<AST>) -> Result<Spanned<AST>, Syntax> {
return self.binop(Token::Rem, Prec::MulDiv, "remainder", left);
}

/// Parses a function call.
/// Function calls are a bit magical,
/// because they're just a series of expressions.
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub enum Token {

// Operators
Add, Sub,
Mul, Div,
Mul, Div, Rem,

Equal,

Expand Down Expand Up @@ -72,6 +72,7 @@ impl Display for Token {
Token::Sub => "a subtraction",
Token::Mul => "a multiplication",
Token::Div => "a division",
Token::Rem => "a remainder operator",
Token::Equal => "an equality test",
Token::End => "end of source",
Token::Keyword(k) => { return write!(f, "the pseudokeyword '{}", k); },
Expand Down
14 changes: 14 additions & 0 deletions src/core/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,17 @@ pub fn div(data: Data) -> Result<Data, String> {

return Ok(result);
}

/// remainder of left operand by right operand division.
/// Raises a runtime error if there is a division by zero.
pub fn remainder(data: Data) -> Result<Data, String> {
let result = match binop(data) {
(Data::Real(_), Data::Real(r)) if r == 0.0 => Err("Division by zero")?,
(Data::Real(l), Data::Real(r)) => Data::Real(l.rem_euclid(r)),
(Data::Integer(_), Data::Integer(n)) if n == 0 => Err("Division by zero")?,
(Data::Integer(l), Data::Integer(r)) => Data::Integer(l.rem_euclid(r)),
_ => Err("Division between unsupported datatypes")?,
};

return Ok(result);
}
1 change: 1 addition & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn ffi_core() -> FFI {
ffi.add("sub", FFIFunction::new(Box::new(math::sub))).unwrap();
ffi.add("mul", FFIFunction::new(Box::new(math::mul))).unwrap();
ffi.add("div", FFIFunction::new(Box::new(math::div))).unwrap();
ffi.add("remainder", FFIFunction::new(Box::new(math::remainder))).unwrap();

// io
ffi.add("println", FFIFunction::new(Box::new(io::println))).unwrap();
Expand Down
5 changes: 5 additions & 0 deletions tests/snippets/remainder.pn
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- action: run
-- outcome: success
-- expect: 1.0

print(3.0 % 2.0)

0 comments on commit 6de5bb0

Please sign in to comment.