diff --git a/src/language_frontend/abstract_syntax_tree/ast.rs b/src/language_frontend/abstract_syntax_tree/ast.rs index b0569ed..46d9571 100644 --- a/src/language_frontend/abstract_syntax_tree/ast.rs +++ b/src/language_frontend/abstract_syntax_tree/ast.rs @@ -24,21 +24,13 @@ pub enum Expr { Call(Call), - Unary { - operator: UnaryOp, - operand: Box, - }, + Unary(Unary), - Binary { - lhs: Box, - operator: BinaryOp, - rhs: Box, - }, + Binary(Binary), - Assignment { - target: Box, - value: Box, - }, + Assignment(Assignment), + + Var(Var), Function(Function), diff --git a/src/language_frontend/abstract_syntax_tree/definitions.rs b/src/language_frontend/abstract_syntax_tree/definitions.rs index 8e4a3d0..b8016bb 100644 --- a/src/language_frontend/abstract_syntax_tree/definitions.rs +++ b/src/language_frontend/abstract_syntax_tree/definitions.rs @@ -99,6 +99,37 @@ pub struct Condition { pub else_body: Option, } + +/// Example: `x = 5` +#[derive(Clone, Debug, PartialEq)] +pub struct Assignment { + pub target: Box, + pub value: Box, +} + +/// Example: `var String: foo = "Test"` +#[derive(Clone, Debug, PartialEq)] +pub struct Var { + pub ty: Option, + pub ident: String, + pub value: Box +} + +/// Example: `x++` +#[derive(Clone, Debug, PartialEq)] +pub struct Unary { + pub operand: Box, + pub operator: UnaryOp, +} + +/// Example: `x++` +#[derive(Clone, Debug, PartialEq)] +pub struct Binary { + pub lhs: Box, + pub operator: BinaryOp, + pub rhs: Box, +} + /// Represents the Structure of a `Function` in AkaiLang /// /// Examples: @@ -110,7 +141,7 @@ pub struct Condition { ///
/// ///```AkaiLang -///fun returnsIntPlusOne(i: i32): i32 { +///fun returnsIntPlusOne(i: int): int { /// -> i + 1 ///} ///``` @@ -126,6 +157,6 @@ pub struct Function { #[derive(Clone, Debug, PartialEq)] pub struct Call { - callee: Box, - arguments: Vec, + pub callee: Box, + pub arguments: Vec, } diff --git a/src/language_frontend/abstract_syntax_tree/parser.rs b/src/language_frontend/abstract_syntax_tree/parser.rs index 173bfbd..973313b 100644 --- a/src/language_frontend/abstract_syntax_tree/parser.rs +++ b/src/language_frontend/abstract_syntax_tree/parser.rs @@ -7,6 +7,8 @@ use crate::language_frontend::{abstract_syntax_tree::{ast::Expr, definitions::*} // goal of parsing is to construct an abstract syntax tree +// todo: improve test-ability of parser + pub fn parse(source: &str) ->Result, Vec>> { let token_iter = Token::lexer(source).spanned().map(|(token, span)| (token.unwrap_or(Token::Error), span.into())); let end_of_input: SimpleSpan = (0..source.len()).into(); @@ -51,12 +53,12 @@ where .then(atom) .then_ignore(just(Token::NewLine).or_not()) .repeated(), - |lhs, (op, rhs)| Expr::Binary { + |lhs, (op, rhs)| Expr::Binary ( Binary { lhs: Box::new(lhs), operator: op, rhs: Box::new(rhs), }, - ); + )); let add_sub = mul_div.clone().foldl( choice(( @@ -66,12 +68,12 @@ where .then(mul_div) .then_ignore(just(Token::NewLine).or_not()) .repeated(), - |lhs, (op, rhs)| Expr::Binary { + |lhs, (op, rhs)| Expr::Binary ( Binary { lhs: Box::new(lhs), operator: op, rhs: Box::new(rhs), }, - ); + )); add_sub }); @@ -81,10 +83,12 @@ where .then_ignore(just(Token::Assign)) .then(expr.clone()) .then_ignore(just(Token::NewLine)) - .map(|(name, rhs)| Expr::Assignment { - target: Box::new(Expr::Ident(name)), - value: Box::new(rhs), - }); + .map(|(name, rhs)| Expr::Var ( Var { + ty: None, + ident: name, + value: Box::new(rhs), + } + )); /* let fun = just(Token::Fun) .ignore_then(ident.clone())