Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The main block should accept args array as an argument #154

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions src/modules/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::variable::variable_name_extensions;

#[derive(Debug, Clone)]
pub struct Main {
pub args: Vec<String>,
pub args: Option<String>,
pub block: Block,
pub is_skipped: bool
}
Expand All @@ -18,7 +18,7 @@ impl SyntaxModule<ParserMetadata> for Main {

fn new() -> Self {
Self {
args: vec![],
args: None,
block: Block::new(),
is_skipped: false
}
Expand All @@ -38,23 +38,15 @@ impl SyntaxModule<ParserMetadata> for Main {
context!({
meta.context.is_main_ctx = true;
if token(meta, "(").is_ok() {
loop {
if token(meta, ")").is_ok() {
break;
}
self.args.push(variable(meta, variable_name_extensions())?);
match token(meta, ")") {
Ok(_) => break,
Err(_) => token(meta, ",")?
};
}
self.args = Some(variable(meta, variable_name_extensions())?);
token(meta, ")")?;
}
token(meta, "{")?;
// Create a new scope for variables
meta.push_scope();
// Create variables
for arg in self.args.iter() {
meta.add_var(arg, Type::Text);
meta.add_var(arg, Type::Array(Box::new(Type::Text)));
}
// Parse the block
syntax(meta, &mut self.block)?;
Expand All @@ -71,13 +63,16 @@ impl SyntaxModule<ParserMetadata> for Main {

impl TranslateModule for Main {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
let variables = self.args.iter().enumerate()
.map(|(index, name)| format!("{name}=${}", index + 1))
.collect::<Vec<_>>().join("\n");
if self.is_skipped {
String::new()
} else {
format!("{variables}\n{}", self.block.translate(meta))
let quote = meta.gen_quote();
let dollar = meta.gen_dollar();
let args = self.args.clone().map_or_else(
|| String::new(),
|name| format!("{name}=({quote}{dollar}@{quote})")
);
format!("{args}\n{}", self.block.translate(meta))
}
}
}