-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add constant declarations (#423)
Co-authored-by: Daniele Scasciafratte <[email protected]>
- Loading branch information
Showing
10 changed files
with
109 additions
and
11 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
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,73 @@ | ||
use heraclitus_compiler::prelude::*; | ||
use crate::docs::module::DocumentationModule; | ||
use crate::modules::types::{Typed, Type}; | ||
use crate::modules::expression::expr::Expr; | ||
use crate::translate::module::TranslateModule; | ||
use crate::utils::metadata::{ParserMetadata, TranslateMetadata}; | ||
use super::{variable_name_extensions, handle_identifier_name}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ConstInit { | ||
name: String, | ||
expr: Box<Expr>, | ||
global_id: Option<usize>, | ||
is_fun_ctx: bool | ||
} | ||
|
||
impl ConstInit { | ||
fn handle_add_const(&mut self, meta: &mut ParserMetadata, name: &str, kind: Type, tok: Option<Token>) -> SyntaxResult { | ||
handle_identifier_name(meta, name, tok)?; | ||
self.global_id = meta.add_var(name, kind, true); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl SyntaxModule<ParserMetadata> for ConstInit { | ||
syntax_name!("Constant Initialize"); | ||
|
||
fn new() -> Self { | ||
ConstInit { | ||
name: String::new(), | ||
expr: Box::new(Expr::new()), | ||
global_id: None, | ||
is_fun_ctx: false | ||
} | ||
} | ||
|
||
fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult { | ||
token(meta, "const")?; | ||
// Get the variable name | ||
let tok = meta.get_current_token(); | ||
self.name = variable(meta, variable_name_extensions())?; | ||
context!({ | ||
token(meta, "=")?; | ||
syntax(meta, &mut *self.expr)?; | ||
// Add a variable to the memory | ||
self.handle_add_const(meta, &self.name.clone(), self.expr.get_type(), tok)?; | ||
self.is_fun_ctx = meta.context.is_fun_ctx; | ||
Ok(()) | ||
}, |position| { | ||
error_pos!(meta, position, format!("Expected '=' after variable name '{}'", self.name)) | ||
}) | ||
} | ||
} | ||
|
||
impl TranslateModule for ConstInit { | ||
fn translate(&self, meta: &mut TranslateMetadata) -> String { | ||
let name = self.name.clone(); | ||
let mut expr = self.expr.translate(meta); | ||
if let Type::Array(_) = self.expr.get_type() { | ||
expr = format!("({expr})"); | ||
} | ||
match self.global_id { | ||
Some(id) => format!("declare -r __{id}_{name}={expr}"), | ||
None => format!("declare -r {name}={expr}") | ||
} | ||
} | ||
} | ||
|
||
impl DocumentationModule for ConstInit { | ||
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
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,15 @@ | ||
// Output | ||
// 1 | ||
// 42 | ||
// 1 | ||
// 42 | ||
|
||
main { | ||
const x = 42 | ||
unsafe $ x=123 $ | ||
echo status // will output 1 if reassignment didnt succeed | ||
echo x | ||
unsafe $ unset x $ | ||
echo status // will output 1 if unsetting did not succeed | ||
echo x | ||
} |
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