diff --git a/src/parser/grammar.pest b/src/parser/grammar.pest index 23f246c..53e39ea 100644 --- a/src/parser/grammar.pest +++ b/src/parser/grammar.pest @@ -14,12 +14,14 @@ inner_str = @{ (!"\"" ~ ANY)* } // ================= TOP LEVEL ================= +export = { "export"? } + import_decl = { "import" ~ string_lit ~ ";" } param_list = { param ~ ("," ~ param)* ~ ","? } param = { identifier ~ ":" ~ type_expr } function_decl = { - "function" ~ identifier ~ "(" ~ param_list? ~ ")" ~ (":" ~ type_expr)? ~ block + export ~ "function" ~ identifier ~ "(" ~ param_list? ~ ")" ~ (":" ~ type_expr)? ~ block } // ================= STATEMENTS ================= @@ -30,3 +32,5 @@ statement = _{ "idk" } // ================= EXPRESSIONS ================= type_expr = _{ identifier } + +expr = { identifier | integer | float | boolean | string_lit } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index faaa5e7..0aa6a79 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -39,3 +39,32 @@ pub fn parse(source: &str) -> Result, ParseError> { Ok(statements) } + +impl Expression { + pub fn from_pair(pair: pest::iterators::Pair) -> Self { + match pair.as_rule() { + Rule::expr => { + let inner = pair.into_inner().next().unwrap(); + Expression::from_pair(inner) + } + Rule::identifier => Expression::Identifier(pair.as_str().to_string()), + Rule::integer => { + let value = pair.as_str().parse::().unwrap(); + Expression::IntLiteral(value) + } + Rule::float => { + let value = pair.as_str().parse::().unwrap(); + Expression::FloatLiteral(value) + } + Rule::boolean => { + let value = pair.as_str().parse::().unwrap(); + Expression::BoolLiteral(value) + } + Rule::string_lit => { + let inner_str = pair.into_inner().next().unwrap().as_str(); + Expression::StringLiteral(inner_str.to_string()) + } + _ => unimplemented!("Expression parsing not implemented yet"), + } + } +}