[fix] conditionals

TODO: conditionals (second layer and beyond)
This commit is contained in:
hkau 2024-04-02 21:38:00 -04:00
parent 5e40183d20
commit 74352a5118
3 changed files with 47 additions and 50 deletions

View File

@ -1,9 +1,11 @@
use std::{env, ops::Index};
#[allow(dead_code)]
pub fn collect_arguments() -> Vec<String> {
return env::args().collect::<Vec<String>>();
}
#[allow(dead_code)]
pub fn get_named_argument(args: &Vec<String>, name: &str) -> Option<String> {
for (i, v) in args.iter().enumerate() {
// if name does not match, continue

View File

@ -30,9 +30,9 @@ RETURN = { "return" ~ any }
CLASSDEC = { ("class" | "meta" | "%:") ~ IDENTIFIER ~ ":" ~ BLOCKS* ~ "<end>" }
FNDEC = { ("def" | "fn" | "function" | "@:") ~ IDENTIFIER ~ "(" ~ any* ~ "):" ~ BLOCKS* ~ "<end>" }
CONDITIONAL_IF = { "if" ~ FNCALL ~ (":" | "then") ~ BLOCKS+ ~ "<end>" ~ (CONDITIONAL_ELSE | CONDITIONAL_ELIF)? }
CONDITIONAL_IF = { "if" ~ QUICK_CONDITIONAL_IF* ~ (":" | "then") ~ BLOCKS+ ~ "<end>" ~ (CONDITIONAL_ELSE | CONDITIONAL_ELIF)? }
CONDITIONAL_ELSE = { "else:" ~ BLOCKS* ~ "<end>" }
CONDITIONAL_ELIF = { "elif" ~ FNCALL ~ ":" ~ BLOCKS+ ~ "<end>" ~ (CONDITIONAL_ELSE | CONDITIONAL_ELIF)? }
CONDITIONAL_ELIF = { "elif" ~ QUICK_CONDITIONAL_IF ~ ":" ~ BLOCKS+ ~ "<end>" ~ (CONDITIONAL_ELSE | CONDITIONAL_ELIF)? }
QUICK_CONDITIONAL_IF = { any ~ (OPERATOR ~ any)+ }
QUICK_CONDITIONAL_BLOCK = _{ "(" ~ QUICK_CONDITIONAL_IF ~ ")" }

View File

@ -380,62 +380,57 @@ pub fn from_tree(
&variables,
);
if res.rule == Rule::bool {
if res.value.to_lowercase() == String::from("true") {
from_tree(
&inner_real,
Option::Some(variables.clone()),
Option::Some(rt_functions.clone()),
if res.value.to_lowercase() == String::from("true") {
from_tree(
&inner_real,
Option::Some(variables.clone()),
Option::Some(rt_functions.clone()),
);
} else if res.value.to_lowercase() == String::from("false") {
// check for else if block
let elif_block = inner.iter().find(|b| b.as_rule() == Rule::CONDITIONAL_ELIF);
if elif_block.is_some() {
let elif_inner = elif_block.unwrap().clone().into_inner();
let elif_inner_vec = elif_inner.clone().collect::<Vec<Pair<'_, Rule>>>();
// ...
let res = get_block_value(
elif_inner_vec.get(0).unwrap().to_owned(),
true,
Option::None,
&functions,
&rt_functions,
&variables,
);
} else if res.value.to_lowercase() == String::from("false") {
// check for else if block
let elif_block =
inner.iter().find(|b| b.as_rule() == Rule::CONDITIONAL_ELIF);
if elif_block.is_some() {
let elif_inner = elif_block.unwrap().clone().into_inner();
let elif_inner_vec =
elif_inner.clone().collect::<Vec<Pair<'_, Rule>>>();
// ...
let res = get_block_value(
elif_inner_vec.get(0).unwrap().to_owned(),
true,
Option::None,
&functions,
&rt_functions,
&variables,
);
// run body
if res.value == "true" {
from_tree(
&elif_inner,
Option::Some(variables.clone()),
Option::Some(rt_functions.clone()),
);
continue;
} else {
// TODO: rerun search but a layer down (block = elif_block)
// this is because all chained CONDITIONAL_ELSE and CONDITIONAL_ELIF blocks
// are children of the conditional block before them
}
}
// check for else block
let else_block =
inner.iter().find(|b| b.as_rule() == Rule::CONDITIONAL_ELSE);
if else_block.is_some() {
let else_inner = else_block.unwrap().clone().into_inner();
// run body
if res.value == "true" {
from_tree(
&else_inner,
&elif_inner,
Option::Some(variables.clone()),
Option::Some(rt_functions.clone()),
);
continue;
} else {
// TODO: rerun search but a layer down (block = elif_block)
// this is because all chained CONDITIONAL_ELSE and CONDITIONAL_ELIF blocks
// are children of the conditional block before them
}
}
// check for else block
let else_block = inner.iter().find(|b| b.as_rule() == Rule::CONDITIONAL_ELSE);
if else_block.is_some() {
let else_inner = else_block.unwrap().clone().into_inner();
from_tree(
&else_inner,
Option::Some(variables.clone()),
Option::Some(rt_functions.clone()),
);
}
}
}
// for loop