-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
exit
as builtin function (#402)
* feat: `exit` as builtin function * feat: optional exit code * fix: clippy issue
- Loading branch information
1 parent
2290bf3
commit eb16c99
Showing
9 changed files
with
95 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use heraclitus_compiler::prelude::*; | ||
use crate::modules::expression::expr::Expr; | ||
use crate::docs::module::DocumentationModule; | ||
use crate::modules::types::{Type, Typed}; | ||
use crate::translate::module::TranslateModule; | ||
use crate::utils::{ParserMetadata, TranslateMetadata}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Exit { | ||
code: Option<Expr> | ||
} | ||
|
||
impl SyntaxModule<ParserMetadata> for Exit { | ||
syntax_name!("Exit"); | ||
|
||
fn new() -> Self { | ||
Exit { | ||
code: None | ||
} | ||
} | ||
|
||
fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult { | ||
token(meta, "exit")?; | ||
|
||
let mut code_expr = Expr::new(); | ||
if syntax(meta, &mut code_expr).is_ok() { | ||
self.code = Some(code_expr); | ||
} | ||
|
||
if let Some(ref code_expr) = self.code { | ||
let code_type = code_expr.get_type(); | ||
if code_type != Type::Num { | ||
let position = code_expr.get_position(meta); | ||
return error_pos!(meta, position => { | ||
message: "Builtin function `exit` can only be used with values of type Num", | ||
comment: format!("Given type: {}, expected type: {}", code_type, Type::Num) | ||
}); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl TranslateModule for Exit { | ||
fn translate(&self, meta: &mut TranslateMetadata) -> String { | ||
let code = self.code.as_ref() | ||
.map(|expr| expr.translate(meta)) | ||
.unwrap_or_else(|| "0".to_string()); | ||
format!("exit {}", code) | ||
} | ||
} | ||
|
||
impl DocumentationModule for Exit { | ||
fn document(&self, _meta: &ParserMetadata) -> String { | ||
"".to_string() | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod cd; | |
pub mod echo; | ||
pub mod mv; | ||
pub mod nameof; | ||
pub mod exit; |
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
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 was deleted.
Oops, something went wrong.
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,3 @@ | ||
main { | ||
exit 37 | ||
} |
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,3 @@ | ||
main { | ||
exit | ||
} |