-
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: add silent keyword, fail, status, failed
- Loading branch information
Showing
32 changed files
with
526 additions
and
80 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 +1,2 @@ | ||
pub mod echo; | ||
pub mod silent; |
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,45 @@ | ||
use heraclitus_compiler::prelude::*; | ||
use crate::modules::block::Block; | ||
use crate::modules::statement::stmt::Statement; | ||
use crate::translate::module::TranslateModule; | ||
use crate::utils::metadata::{ParserMetadata, TranslateMetadata}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Silent { | ||
block: Box<Block> | ||
} | ||
|
||
impl SyntaxModule<ParserMetadata> for Silent { | ||
syntax_name!("Silent"); | ||
|
||
fn new() -> Self { | ||
Silent { | ||
block: Box::new(Block::new()) | ||
} | ||
} | ||
|
||
fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult { | ||
token(meta, "silent")?; | ||
match token(meta, "{") { | ||
Ok(_) => { | ||
syntax(meta, &mut *self.block)?; | ||
token(meta, "}")?; | ||
}, | ||
Err(_) => { | ||
let mut statement = Statement::new(); | ||
syntax(meta, &mut statement)?; | ||
self.block.push_statement(statement); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl TranslateModule for Silent { | ||
fn translate(&self, meta: &mut TranslateMetadata) -> String { | ||
meta.silenced = true; | ||
let translated = self.block.translate(meta); | ||
meta.silenced = false; | ||
translated | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use heraclitus_compiler::prelude::*; | ||
use crate::modules::block::Block; | ||
use crate::modules::statement::stmt::Statement; | ||
use crate::translate::module::TranslateModule; | ||
use crate::utils::metadata::{ParserMetadata, TranslateMetadata}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Failed { | ||
is_parsed: bool, | ||
is_question_mark: bool, | ||
is_main: bool, | ||
block: Box<Block> | ||
} | ||
|
||
impl SyntaxModule<ParserMetadata> for Failed { | ||
syntax_name!("Failed Expression"); | ||
|
||
fn new() -> Self { | ||
Failed { | ||
is_parsed: false, | ||
is_question_mark: false, | ||
is_main: false, | ||
block: Box::new(Block::new()) | ||
} | ||
} | ||
|
||
fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult { | ||
let tok = meta.get_current_token(); | ||
if let Ok(_) = token(meta, "?") { | ||
if !meta.context.is_fun_ctx && !meta.context.is_main_ctx { | ||
return error!(meta, tok, "The '?' operator can only be used in the main block or function body") | ||
} | ||
self.is_question_mark = true; | ||
self.is_main = meta.context.is_main_ctx; | ||
self.is_parsed = true; | ||
return Ok(()) | ||
} | ||
token(meta, "failed")?; | ||
match token(meta, "{") { | ||
Ok(_) => { | ||
syntax(meta, &mut *self.block)?; | ||
token(meta, "}")?; | ||
}, | ||
Err(_) => { | ||
token(meta, "=>")?; | ||
let mut statement = Statement::new(); | ||
syntax(meta, &mut statement)?; | ||
self.block.push_statement(statement); | ||
} | ||
} | ||
self.is_main = meta.context.is_main_ctx; | ||
self.is_parsed = true; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl TranslateModule for Failed { | ||
fn translate(&self, meta: &mut TranslateMetadata) -> String { | ||
if self.is_parsed { | ||
let block = self.block.translate(meta); | ||
let ret = self.is_main | ||
.then(|| "exit $?") | ||
.unwrap_or("return $?"); | ||
// the condition of '$?' clears the status code thus we need to store it in a variable | ||
if self.is_question_mark { | ||
vec![ | ||
"__AMBER_STATUS=$?;", | ||
"if [ $__AMBER_STATUS != 0 ]; then", | ||
&format!("$(exit $__AMBER_STATUS)"), | ||
ret, | ||
"fi" | ||
].join("\n") | ||
} else { | ||
vec![ | ||
"__AMBER_STATUS=$?;", | ||
"if [ $__AMBER_STATUS != 0 ]; then", | ||
&format!("$(exit $__AMBER_STATUS)"), | ||
&block, | ||
"fi" | ||
].join("\n") | ||
} | ||
} else { | ||
String::new() | ||
} | ||
} | ||
} |
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 ifcond; | ||
pub mod ifchain; | ||
pub mod ternary; | ||
pub mod ternary; | ||
pub mod failed; |
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 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
Oops, something went wrong.