-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters