Skip to content

Commit

Permalink
Added more config options and tested recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
mschae23 committed Aug 6, 2022
1 parent d12d3ff commit df79f1e
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "density_function_lang"
version = "3.1.2"
version = "3.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
35 changes: 35 additions & 0 deletions example/test/main.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
template loop_for_index(start, end, step, f) {
loop_for_index_impl(start, end, step, start, f, false)
}

template loop_for_index_impl(start, end, step, current, f, result) {
if (current == end || (start + step > start && current >= end) || (start + step < start && current <= end)) {
result
} else {
loop_for_index_impl(start, end, step, builtin.static(current + step), f, f(current))
}
}

template iteration(i) {
i // builtin.error("Iteration", i)
}

export loop_for_index_test = loop_for_index(0, 20, 1, iteration);

template array_fold(array, start_acc, f) {
array_fold_impl(array, start_acc, f, 0)
}

template array_fold_impl(array, acc, f, index) {
if (index >= array.length)
acc
else {
array_fold_impl(array, f(acc, builtin.static(array[index])), f, builtin.static(index + 1))
}
}

template array_fold_iteration(acc, element) {
builtin.static(acc + element)
}

export array_fold_test = array_fold([7, 3, 5, 1, 2, 8, 10, 9, 6, 4], 0, array_fold_iteration);
1 change: 1 addition & 0 deletions example/test/target/array_fold_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
55
1 change: 1 addition & 0 deletions example/test/target/loop_for_index_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
19
18 changes: 17 additions & 1 deletion src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ pub enum Expr {
receiver: Box<Expr>,
name: Token,
},
Index {
receiver: Box<Expr>,
operator: Token,
index: Box<Expr>,
},
BuiltinFunctionCall {
name: Token,
args: Vec<Expr>,
Expand Down Expand Up @@ -144,6 +149,7 @@ impl Debug for Expr {
.collect::<Vec<String>>().join(", "))
},
Expr::Member { receiver, name } => write!(f, "({:?}.{})", receiver, name.source()),
Expr::Index { receiver, index, .. } => write!(f, "({:?}[{:?}])", receiver, index),
Expr::BuiltinFunctionCall { name, args } => {
write!(f, "(builtin.{}({}))", name.source(), args.iter()
.map(|expr| format!("{:?}", expr))
Expand Down Expand Up @@ -188,7 +194,17 @@ pub struct Template {
pub file_path: Rc<RefCell<PathBuf>>,
}

#[derive(Debug)]
impl PartialEq for Template {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.receiver == other.receiver
&& self.args == other.args
&& self.expr == other.expr
&& self.file_path == other.file_path
}
}

#[derive(Debug, PartialEq)]
pub struct Module {
pub name: String,
pub sub_modules: Vec<Rc<RefCell<Module>>>,
Expand Down
Loading

0 comments on commit df79f1e

Please sign in to comment.