From e30dfcb0a93081dd578fe4e697b8e865c2b0d33c Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Sun, 3 Jul 2022 23:45:34 +0200 Subject: [PATCH] Add README, update sample file, fix functions --- README.md | 26 ++++++++++++++++++++++++++ src/function/mod.rs | 2 +- src/grammar.pest | 3 ++- src/pest_parser.rs | 3 +-- test.foo | 14 ++++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..7b8652f --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Name TBD +> i guess it's an embedded language? + +# Features +Math: `+`, `-`, `*`, `/`, `%` +Equality: `==`, `!=`, `>`, `<`, `>=`, `<=` + +Some basic types: String, char, int, float, bool + +Custom rust types: if it implements `CustomValue`, it can be used + +Functions: `foo(x)` + +Methods: `x.foo()` + +Any function is also a method, as long as it has at least one parameter. +The same also works the other way around. + +Some control flow: `while` and `if/else if/else` + +Functions: can be defined in both the language and in Rust. + +The functions defined in the language currently do not allow returning values. +The `return` keyword does already exist however, it just doesn't do anything yet. + +For some sample code, check `test.foo` (extension also TBD) diff --git a/src/function/mod.rs b/src/function/mod.rs index ad4b7ca..ffd4d2a 100644 --- a/src/function/mod.rs +++ b/src/function/mod.rs @@ -105,7 +105,7 @@ impl Function for InterpreterFunction { fn execute(&self, env: &Env, map: &FunctionMap, args: &[Value]) -> Result { let sub_env = Env::new_with_parent(env); for (name, value) in self.parameters.iter().zip(args) { - sub_env.set(name, value.clone()); + sub_env.set_here(name, value.clone()); } for stmt in self.body.iter() { stmt.eval(&sub_env, map); diff --git a/src/grammar.pest b/src/grammar.pest index 66a815d..98409f2 100644 --- a/src/grammar.pest +++ b/src/grammar.pest @@ -52,13 +52,14 @@ root_statement = { SOI ~ statement* ~ EOI } -statement = { ( assignment | assignment_let | expression ) ~ ";" | def | while_block | if_block | block } +statement = { ( ret | assignment | assignment_let | expression ) ~ ";" | def | while_block | if_block | block } block = { "{" ~ statement* ~ "}" } assignment_let = { "let" ~ identifier ~ "=" ~ expression } assignment = { identifier ~ "=" ~ expression } +ret = { "return" ~ expression } while_block = { "while" ~ expression ~ block } diff --git a/src/pest_parser.rs b/src/pest_parser.rs index eeb1599..1b310df 100644 --- a/src/pest_parser.rs +++ b/src/pest_parser.rs @@ -119,7 +119,7 @@ fn handle_rules(mut p: pest::iterators::Pairs) -> (Statement, FuncHolder) .next() .unwrap() .into_inner() - .map(|a| a.to_string()) + .map(|a| a.as_str().to_string()) .collect(); let body = inner.next().unwrap(); @@ -138,7 +138,6 @@ fn handle_rules(mut p: pest::iterators::Pairs) -> (Statement, FuncHolder) match pair.as_rule() { Rule::expression => { let mut inner = pair.into_inner(); - println!("{:?}", inner); let expr = parse_expression(inner.next().unwrap()); if let Some(c) = inner.next() { let mut inner = c.into_inner(); diff --git a/test.foo b/test.foo index 3f6ab67..548ad23 100644 --- a/test.foo +++ b/test.foo @@ -1,2 +1,16 @@ print("hello".eq("world")); + print((40 + 2).is_42() == true); + +let y = 0; + +def test_func(x) { + print(x); + while x < 100 { + print(x); + x = x + 1; + } +} + +test_func(y); +