Skip to content

Commit

Permalink
feat: add negation syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Ph0enixKM committed Jun 12, 2024
1 parent 69e96ae commit be3e6db
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/modules/expression/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use super::binop::{
};
use super::unop::{
not::Not,
neg::Neg,
cast::Cast,
is::Is
};
Expand All @@ -52,6 +53,7 @@ pub enum ExprType {
Mul(Mul),
Div(Div),
Modulo(Modulo),
Neg(Neg),
And(And),
Or(Or),
Gt(Gt),
Expand Down Expand Up @@ -107,7 +109,7 @@ impl Expr {
// Arithmetic operators
Add, Sub, Mul, Div, Modulo,
// Unary operators
Cast, Not, Nameof, Is,
Cast, Not, Neg, Nameof, Is,
// Literals
Range, Parenthesis, CommandExpr, Bool, Number, Text, Array, Null, Status,
// Function invocation
Expand Down Expand Up @@ -163,4 +165,4 @@ impl TranslateModule for Expr {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
self.translate_match(meta, self.value.as_ref().unwrap())
}
}
}
3 changes: 2 additions & 1 deletion src/modules/expression/unop/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod not;
pub mod cast;
pub mod is;
pub mod is;
pub mod neg;
41 changes: 41 additions & 0 deletions src/modules/expression/unop/neg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use heraclitus_compiler::prelude::*;
use crate::{utils::{metadata::ParserMetadata, TranslateMetadata}, modules::types::{Type, Typed}, translate::{module::TranslateModule, compute::{translate_computation, ArithOp}}};
use super::super::expr::Expr;

#[derive(Debug, Clone)]
pub struct Neg {
expr: Box<Expr>
}

impl Typed for Neg {
fn get_type(&self) -> Type {
Type::Num
}
}

impl SyntaxModule<ParserMetadata> for Neg {
syntax_name!("Neg");

fn new() -> Self {
Neg {
expr: Box::new(Expr::new())
}
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
token(meta, "-")?;
let tok = meta.get_current_token();
syntax(meta, &mut *self.expr)?;
if ! matches!(self.expr.get_type(), Type::Num) {
return error!(meta, tok, "Only numbers can be negated");
}
Ok(())
}
}

impl TranslateModule for Neg {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
let expr = self.expr.translate(meta);
translate_computation(meta, ArithOp::Neg, None, Some(expr))
}
}
2 changes: 1 addition & 1 deletion src/modules/expression/unop/not.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ impl TranslateModule for Not {
let expr = self.expr.translate(meta);
translate_computation(meta, ArithOp::Not, None, Some(expr))
}
}
}
16 changes: 16 additions & 0 deletions src/tests/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ fn sub() {
test_amber!("echo 21 - 7", "14");
}

#[test]
fn neg() {
test_amber!("echo -42", "-42");
}

#[test]
fn text() {
test_amber!("echo \"Hello World\"", "Hello World");
Expand Down Expand Up @@ -99,6 +104,17 @@ fn very_complex_arithmetic() {
test_amber!(code, "24");
}

#[test]
fn neg_complex_arithmetic() {
let code = "
let x = 21
let y = -2
let z = 6
echo x - y * z / -4
";
test_amber!(code, "18");
}

#[test]
fn fractions_with_no_leading_zero() {
let code = "
Expand Down
4 changes: 3 additions & 1 deletion src/translate/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum ArithOp {
Mul,
Div,
Modulo,
Neg,
Gt,
Ge,
Lt,
Expand Down Expand Up @@ -37,6 +38,7 @@ pub fn translate_computation(meta: &TranslateMetadata, operation: ArithOp, left:
math_lib_flag = false;
"%"
},
ArithOp::Neg => "-",
ArithOp::Gt => ">",
ArithOp::Ge => ">=",
ArithOp::Lt => "<",
Expand All @@ -59,4 +61,4 @@ pub fn translate_computation_eval(meta: &mut TranslateMetadata, operation: Arith
let result = translate_computation(meta, operation, left, right);
meta.eval_ctx = old_eval;
result
}
}

0 comments on commit be3e6db

Please sign in to comment.