diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2021-11-17 10:06:04 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2021-11-17 10:06:04 +0200 |
| commit | 08c930867859889045fda0b45029a37c789dff10 (patch) | |
| tree | 7a9bbcd69b2acae8e3d36e44b7a8bd9f2bdf9856 | |
| parent | 15f5516b98d3b26eaa9aef758f8e2292b1f4a570 (diff) | |
Implement four function calc operators
| -rw-r--r-- | samples/sample1.code | 5 | ||||
| -rw-r--r-- | src/grammar.pest | 7 | ||||
| -rw-r--r-- | src/main.rs | 17 |
3 files changed, 22 insertions, 7 deletions
diff --git a/samples/sample1.code b/samples/sample1.code index 95635f9..e21a00e 100644 --- a/samples/sample1.code +++ b/samples/sample1.code @@ -1 +1,4 @@ -f 1 +/* Sample code */ +sum a b = + a b + +sum 2 3
\ No newline at end of file diff --git a/src/grammar.pest b/src/grammar.pest index e930511..333a58e 100644 --- a/src/grammar.pest +++ b/src/grammar.pest @@ -1,9 +1,10 @@ WHITESPACE = _{ " " | "\t" } -COMMENT = _{ "/*" ~ (!"*/" ~ ANY)* ~ "*/" } +COMMENT = _{ "/*" ~ (!"*/" ~ ANY)* ~ "*/" ~ NEWLINE* } program = { SOI ~ statement+ ~ EOI } statement = { (definition | expression) ~ NEWLINE* } definition = { symbol+ ~ "=" ~ expression } -expression = { leaf+ } -leaf = { integer_literal | symbol } +expression = { expression_node+ } +expression_node = { integer_literal | operator | symbol | ("(" ~ expression ~ ")") } +operator = { "+" | "-" | "/" | "*" } integer_literal = @{ "-"? ~ ASCII_DIGIT+ } symbol = @{ ASCII_ALPHA ~ (ASCII_ALPHANUMERIC | "-" | ".")* }
\ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e540192..6874762 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,14 +43,25 @@ mod ast { #[derive(Debug, FromPest)] #[pest_ast(rule(Rule::expression))] pub struct Expression { - pub leaves: Vec<Leaf>, + pub nodes: Vec<ExpressionNode>, } #[derive(Debug, FromPest)] - #[pest_ast(rule(Rule::leaf))] - pub enum Leaf { + #[pest_ast(rule(Rule::expression_node))] + pub enum ExpressionNode { Symbol(Symbol), + Operator(Operator), IntegerLiteral(IntegerLiteral), + Expression(Expression), + } + + #[derive(Debug, FromPest)] + #[pest_ast(rule(Rule::operator))] + pub enum Operator { + SumOp, + SubtractOp, + DivideOp, + MultiplyOp, } #[derive(Debug, FromPest)] |
