[add] grammar: ARRAY_SELECT

[add] interpret: ability to select items from an array
arr[i]
This commit is contained in:
hkau 2024-02-23 19:13:34 -05:00
parent b952f61eb2
commit 878a785dd9
2 changed files with 43 additions and 3 deletions

View file

@ -9,6 +9,7 @@ BLOCKS = {
| RETURN
| METHODCALL
| FNCALL
| ARRAY_SELECT
| TUPLE_SELECT
| JSON_ASSIGN
| JSON_SELECT
@ -54,7 +55,8 @@ EXP = @{ ("E" | "e") ~ ("+" | "-")? ~ INT }
string = @{ ("\"" | "'") ~ INNER ~ ("\"" | "'") }
INNER = @{ (!("\"" | "\\" | "\u{0000}" | "\u{001F}") ~ ANY)* ~ (ESCAPE ~ INNER)? }
array = @{ "[" ~ any ~ ("," ~ WHITESPACE? ~ any)* ~ ","? ~ "]" | "[" ~ "]" }
array = @{ "[" ~ any ~ ("," ~ WHITESPACE? ~ any)* ~ ","? ~ "]" | "[" ~ "]" }
ARRAY_SELECT = { any ~ "[" ~ INT ~ "]" }
tuple = @{ "(" ~ any ~ ("," ~ WHITESPACE? ~ any)* ~ ","? ~ ")" | "(" ~ ")" }
TUPLE_SELECT = { any ~ "." ~ INT }

View file

@ -154,10 +154,10 @@ pub struct KvKfnStore {
impl KvKfnStore {
fn from(variables: VariablesStore, rt_functions: RuntimeFunctionsStore) -> KvKfnStore {
return KvKfnStore {
KvKfnStore {
kv: variables,
k_fn: rt_functions,
};
}
}
}
@ -1442,6 +1442,44 @@ fn get_block_value(
};
}
// array select (alias of array::__getitem__)
if block.as_rule() == Rule::ARRAY_SELECT {
let inner = block.into_inner().collect::<Vec<Pair<'_, Rule>>>();
let acting_on = inner
.get(0)
.expect("could not select 'acting_on' (ARRAY_SELECT)");
let index = inner
.get(1)
.expect("could not select 'index' (ARRAY_SELECT)");
// create args
let mut args: Vec<ReturnValue> = Vec::new();
args.push(ReturnValue {
rule: Rule::number,
value: index.as_span().as_str().to_string().replace("\"", ""),
children: Option::None,
attributes: Option::None,
});
// create vars
let mut vars: VariablesStore = HashMap::new();
vars.insert(
String::from("self"),
get_block_value(
acting_on.to_owned(),
false,
Option::None,
&functions,
&rt_functions,
&variables,
),
);
// return
return crate::modules::methods::array::__getitem__(args, vars);
}
// tuple
if btype == Rule::tuple {
// parse