-
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.
Co-authored-by: Daniele Scasciafratte <[email protected]>
- Loading branch information
Showing
9 changed files
with
111 additions
and
35 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,75 @@ | ||
use crate::docs::module::DocumentationModule; | ||
use crate::modules::expression::expr::Expr; | ||
use crate::modules::expression::unop::UnOp; | ||
use crate::modules::types::{Type, Typed}; | ||
use crate::translate::module::TranslateModule; | ||
use crate::utils::{ParserMetadata, TranslateMetadata}; | ||
use heraclitus_compiler::prelude::*; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Len { | ||
value: Box<Expr>, | ||
} | ||
|
||
impl Typed for Len { | ||
fn get_type(&self) -> Type { | ||
Type::Num | ||
} | ||
} | ||
|
||
impl UnOp for Len { | ||
fn set_expr(&mut self, expr: Expr) { | ||
self.value = Box::new(expr); | ||
} | ||
|
||
fn parse_operator(&mut self, meta: &mut ParserMetadata) -> SyntaxResult { | ||
token(meta, "len")?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl SyntaxModule<ParserMetadata> for Len { | ||
syntax_name!("Length"); | ||
|
||
fn new() -> Self { | ||
Len { | ||
value: Box::new(Expr::new()), | ||
} | ||
} | ||
|
||
fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult { | ||
if !matches!(self.value.get_type(), Type::Text | Type::Array(_)) { | ||
let msg = self | ||
.value | ||
.get_error_message(meta) | ||
.message("Length can only be applied to text or array types"); | ||
return Err(Failure::Loud(msg)); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl TranslateModule for Len { | ||
fn translate(&self, meta: &mut TranslateMetadata) -> String { | ||
let value = self.value.translate(meta); | ||
if self.value.get_type() == Type::Text { | ||
meta.stmt_queue.push_back(format!("__AMBER_LEN={value}")); | ||
return String::from("\"${#__AMBER_LEN}\"") | ||
} | ||
// Case for Array passed as a reference | ||
if value.starts_with("\"${!") { | ||
meta.stmt_queue.push_back(format!("__AMBER_LEN=({value})")); | ||
String::from("\"${#__AMBER_LEN[@]}\"") | ||
} else { | ||
format!("\"${{#{}", value.trim_start_matches("\"${")) | ||
.trim_end() | ||
.to_string() | ||
} | ||
} | ||
} | ||
|
||
impl DocumentationModule for Len { | ||
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ pub mod echo; | |
pub mod mv; | ||
pub mod nameof; | ||
pub mod exit; | ||
pub mod len; |
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 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,22 @@ | ||
// Output | ||
// Text literal: 5 | ||
// Array literal: 3 | ||
// Text variable: 6 | ||
// Array variable: 4 | ||
// Text reference: 6 | ||
// Array reference: 4 | ||
|
||
let name = "Andrew" | ||
let fruits = ["apple", "banana", "cherry", "date"] | ||
|
||
fun foo(ref text, ref arr) { | ||
echo "Text reference: {len text}" | ||
echo "Array reference: {len arr}" | ||
} | ||
|
||
echo "Text literal: {len "Hello"}" | ||
echo "Array literal: {len [1, 2, 3]}" | ||
echo "Text variable: {len name}" | ||
echo "Array variable: {len fruits}" | ||
|
||
foo(name, fruits) |