fixed function calls, added array example
This commit is contained in:
parent
fe99fddc29
commit
cefff00db3
5 changed files with 81 additions and 11 deletions
68
examples/array.pr
Normal file
68
examples/array.pr
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
|
||||||
|
for: (from, to, f) => {
|
||||||
|
index: from;
|
||||||
|
loop {
|
||||||
|
if not(inf(index, to)) break true;
|
||||||
|
f(index);
|
||||||
|
index <- add(index, 1)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
array_new: () => {
|
||||||
|
r: obj();
|
||||||
|
r <- set(r, "len", 0);
|
||||||
|
r
|
||||||
|
};
|
||||||
|
|
||||||
|
array_len: (self) => {
|
||||||
|
get(self, "len")
|
||||||
|
};
|
||||||
|
|
||||||
|
array_get: (self, index) => {
|
||||||
|
get(self, str(index))
|
||||||
|
};
|
||||||
|
|
||||||
|
array_push: (self, e) => {
|
||||||
|
i: array_len(self);
|
||||||
|
self <- set(self, str(i), e);
|
||||||
|
|
||||||
|
len: add (i, 1);
|
||||||
|
self <- set(self, "len", len);
|
||||||
|
self
|
||||||
|
};
|
||||||
|
|
||||||
|
array_pop: (self) => {
|
||||||
|
l: array_len(self);
|
||||||
|
if eq(l, 0) return false;
|
||||||
|
|
||||||
|
i: sub(l, 1);
|
||||||
|
e: array_get(self, i);
|
||||||
|
self <- set(self, str(i), false);
|
||||||
|
|
||||||
|
r: obj();
|
||||||
|
r <- set(r, "tail", e);
|
||||||
|
r <- set(r, "rest", self);
|
||||||
|
r
|
||||||
|
};
|
||||||
|
|
||||||
|
array_print: (self) => {
|
||||||
|
l: array_len(self);
|
||||||
|
r: "[";
|
||||||
|
for(0, l, (i) => {
|
||||||
|
r <- add(r, array_get(self, i));
|
||||||
|
r <- add(r, ", ")
|
||||||
|
});
|
||||||
|
r <- add(r, "]");
|
||||||
|
out(r)
|
||||||
|
};
|
||||||
|
|
||||||
|
main: () => {
|
||||||
|
a: array_new();
|
||||||
|
a <- array_push(a, 1);
|
||||||
|
a <- array_push(a, 2);
|
||||||
|
a <- array_push(a, 3);
|
||||||
|
a <- array_push(a, 4);
|
||||||
|
array_print(a)
|
||||||
|
};
|
||||||
|
|
||||||
|
main();
|
|
@ -187,7 +187,7 @@ impl Parser {
|
||||||
parameter_names,
|
parameter_names,
|
||||||
} = function_definition;
|
} = function_definition;
|
||||||
|
|
||||||
let parser_scope = parser_scope.make_child_common();
|
let parser_scope = parser_scope.make_child_function();
|
||||||
let parameter_ids = parameter_names
|
let parameter_ids = parameter_names
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|name| parser_scope.add_name(name))
|
.map(|name| parser_scope.add_name(name))
|
||||||
|
@ -209,7 +209,7 @@ impl Parser {
|
||||||
|
|
||||||
let variable_id = parser_scope
|
let variable_id = parser_scope
|
||||||
.get_variable_id(&name)
|
.get_variable_id(&name)
|
||||||
.expect("call of undeclared function");
|
.expect(&format!("call of undeclared function '{name}'"));
|
||||||
let parameters = arguments
|
let parameters = arguments
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|argument| self.parse_expression(argument, parser_scope))
|
.map(|argument| self.parse_expression(argument, parser_scope))
|
||||||
|
|
|
@ -132,7 +132,11 @@ fn set(args: Vec<Value>) -> Value {
|
||||||
let mut object = args.get(0).unwrap().as_object().unwrap().clone();
|
let mut object = args.get(0).unwrap().as_object().unwrap().clone();
|
||||||
let name = args.get(1).unwrap().as_string().unwrap().to_string();
|
let name = args.get(1).unwrap().as_string().unwrap().to_string();
|
||||||
let value = args.get(2).unwrap().clone();
|
let value = args.get(2).unwrap().clone();
|
||||||
object.insert(name, value);
|
if let Value::Bool(false) = value {
|
||||||
|
object.remove(&name);
|
||||||
|
} else {
|
||||||
|
object.insert(name, value);
|
||||||
|
}
|
||||||
object.into()
|
object.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -142,7 +142,12 @@ fn test_function_definition_parser() {
|
||||||
pub fn function_call_parser(expression: impl AbstractParser<Expr>) -> impl AbstractParser<FnCall> {
|
pub fn function_call_parser(expression: impl AbstractParser<Expr>) -> impl AbstractParser<FnCall> {
|
||||||
let parameters = expression.separated_by(just(',').padded());
|
let parameters = expression.separated_by(just(',').padded());
|
||||||
name()
|
name()
|
||||||
.then(parameters.padded().delimited_by(just('('), just(')')))
|
.then(
|
||||||
|
parameters
|
||||||
|
.padded()
|
||||||
|
.delimited_by(just('('), just(')'))
|
||||||
|
.padded(),
|
||||||
|
)
|
||||||
.map(|(name, arguments)| FnCall { arguments, name })
|
.map(|(name, arguments)| FnCall { arguments, name })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -100,13 +100,6 @@ impl Value {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_object_mut(&mut self) -> Option<&mut HashMap<String, Value>> {
|
|
||||||
match self {
|
|
||||||
Self::Object(h) => Some(h),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_function(&self) -> Option<&Function> {
|
pub fn as_function(&self) -> Option<&Function> {
|
||||||
match self {
|
match self {
|
||||||
Self::Function(function) => Some(function),
|
Self::Function(function) => Some(function),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue