Add README, update sample file, fix functions
This commit is contained in:
parent
1c6d8a90d4
commit
e30dfcb0a9
26
README.md
Normal file
26
README.md
Normal file
|
@ -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)
|
|
@ -105,7 +105,7 @@ impl Function for InterpreterFunction {
|
||||||
fn execute(&self, env: &Env, map: &FunctionMap, args: &[Value]) -> Result<Value, String> {
|
fn execute(&self, env: &Env, map: &FunctionMap, args: &[Value]) -> Result<Value, String> {
|
||||||
let sub_env = Env::new_with_parent(env);
|
let sub_env = Env::new_with_parent(env);
|
||||||
for (name, value) in self.parameters.iter().zip(args) {
|
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() {
|
for stmt in self.body.iter() {
|
||||||
stmt.eval(&sub_env, map);
|
stmt.eval(&sub_env, map);
|
||||||
|
|
|
@ -52,13 +52,14 @@ root_statement = {
|
||||||
SOI ~ statement* ~ EOI
|
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* ~ "}" }
|
block = { "{" ~ statement* ~ "}" }
|
||||||
|
|
||||||
assignment_let = { "let" ~ identifier ~ "=" ~ expression }
|
assignment_let = { "let" ~ identifier ~ "=" ~ expression }
|
||||||
assignment = { identifier ~ "=" ~ expression }
|
assignment = { identifier ~ "=" ~ expression }
|
||||||
|
|
||||||
|
ret = { "return" ~ expression }
|
||||||
|
|
||||||
while_block = { "while" ~ expression ~ block }
|
while_block = { "while" ~ expression ~ block }
|
||||||
|
|
||||||
|
|
|
@ -119,7 +119,7 @@ fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder)
|
||||||
.next()
|
.next()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_inner()
|
.into_inner()
|
||||||
.map(|a| a.to_string())
|
.map(|a| a.as_str().to_string())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let body = inner.next().unwrap();
|
let body = inner.next().unwrap();
|
||||||
|
@ -138,7 +138,6 @@ fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder)
|
||||||
match pair.as_rule() {
|
match pair.as_rule() {
|
||||||
Rule::expression => {
|
Rule::expression => {
|
||||||
let mut inner = pair.into_inner();
|
let mut inner = pair.into_inner();
|
||||||
println!("{:?}", inner);
|
|
||||||
let expr = parse_expression(inner.next().unwrap());
|
let expr = parse_expression(inner.next().unwrap());
|
||||||
if let Some(c) = inner.next() {
|
if let Some(c) = inner.next() {
|
||||||
let mut inner = c.into_inner();
|
let mut inner = c.into_inner();
|
||||||
|
|
Loading…
Reference in a new issue