added struct def for variable decl

This commit is contained in:
LunarAkai 2025-08-09 06:15:12 +02:00
commit 8df1f6b719
3 changed files with 51 additions and 24 deletions

View file

@ -24,21 +24,13 @@ pub enum Expr {
Call(Call), Call(Call),
Unary { Unary(Unary),
operator: UnaryOp,
operand: Box<Expr>,
},
Binary { Binary(Binary),
lhs: Box<Expr>,
operator: BinaryOp,
rhs: Box<Expr>,
},
Assignment { Assignment(Assignment),
target: Box<Expr>,
value: Box<Expr>, Var(Var),
},
Function(Function), Function(Function),

View file

@ -99,6 +99,37 @@ pub struct Condition {
pub else_body: Option<BlockStatement>, pub else_body: Option<BlockStatement>,
} }
/// Example: `x = 5`
#[derive(Clone, Debug, PartialEq)]
pub struct Assignment {
pub target: Box<Expr>,
pub value: Box<Expr>,
}
/// Example: `var String: foo = "Test"`
#[derive(Clone, Debug, PartialEq)]
pub struct Var {
pub ty: Option<Type>,
pub ident: String,
pub value: Box<Expr>
}
/// Example: `x++`
#[derive(Clone, Debug, PartialEq)]
pub struct Unary {
pub operand: Box<Expr>,
pub operator: UnaryOp,
}
/// Example: `x++`
#[derive(Clone, Debug, PartialEq)]
pub struct Binary {
pub lhs: Box<Expr>,
pub operator: BinaryOp,
pub rhs: Box<Expr>,
}
/// Represents the Structure of a `Function` in AkaiLang /// Represents the Structure of a `Function` in AkaiLang
/// ///
/// Examples: /// Examples:
@ -110,7 +141,7 @@ pub struct Condition {
/// <br> /// <br>
/// ///
///```AkaiLang ///```AkaiLang
///fun returnsIntPlusOne(i: i32): i32 { ///fun returnsIntPlusOne(i: int): int {
/// -> i + 1 /// -> i + 1
///} ///}
///``` ///```
@ -126,6 +157,6 @@ pub struct Function {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Call { pub struct Call {
callee: Box<Expr>, pub callee: Box<Expr>,
arguments: Vec<Expr>, pub arguments: Vec<Expr>,
} }

View file

@ -7,6 +7,8 @@ use crate::language_frontend::{abstract_syntax_tree::{ast::Expr, definitions::*}
// goal of parsing is to construct an abstract syntax tree // goal of parsing is to construct an abstract syntax tree
// todo: improve test-ability of parser
pub fn parse(source: &str) ->Result<Vec<Expr>, Vec<Rich<'_, Token>>> { pub fn parse(source: &str) ->Result<Vec<Expr>, Vec<Rich<'_, Token>>> {
let token_iter = Token::lexer(source).spanned().map(|(token, span)| (token.unwrap_or(Token::Error), span.into())); 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(); let end_of_input: SimpleSpan = (0..source.len()).into();
@ -51,12 +53,12 @@ where
.then(atom) .then(atom)
.then_ignore(just(Token::NewLine).or_not()) .then_ignore(just(Token::NewLine).or_not())
.repeated(), .repeated(),
|lhs, (op, rhs)| Expr::Binary { |lhs, (op, rhs)| Expr::Binary ( Binary {
lhs: Box::new(lhs), lhs: Box::new(lhs),
operator: op, operator: op,
rhs: Box::new(rhs), rhs: Box::new(rhs),
}, },
); ));
let add_sub = mul_div.clone().foldl( let add_sub = mul_div.clone().foldl(
choice(( choice((
@ -66,12 +68,12 @@ where
.then(mul_div) .then(mul_div)
.then_ignore(just(Token::NewLine).or_not()) .then_ignore(just(Token::NewLine).or_not())
.repeated(), .repeated(),
|lhs, (op, rhs)| Expr::Binary { |lhs, (op, rhs)| Expr::Binary ( Binary {
lhs: Box::new(lhs), lhs: Box::new(lhs),
operator: op, operator: op,
rhs: Box::new(rhs), rhs: Box::new(rhs),
}, },
); ));
add_sub add_sub
}); });
@ -81,10 +83,12 @@ where
.then_ignore(just(Token::Assign)) .then_ignore(just(Token::Assign))
.then(expr.clone()) .then(expr.clone())
.then_ignore(just(Token::NewLine)) .then_ignore(just(Token::NewLine))
.map(|(name, rhs)| Expr::Assignment { .map(|(name, rhs)| Expr::Var ( Var {
target: Box::new(Expr::Ident(name)), ty: None,
ident: name,
value: Box::new(rhs), value: Box::new(rhs),
}); }
));
/* /*
let fun = just(Token::Fun) let fun = just(Token::Fun)
.ignore_then(ident.clone()) .ignore_then(ident.clone())