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),
Unary {
operator: UnaryOp,
operand: Box<Expr>,
},
Unary(Unary),
Binary {
lhs: Box<Expr>,
operator: BinaryOp,
rhs: Box<Expr>,
},
Binary(Binary),
Assignment {
target: Box<Expr>,
value: Box<Expr>,
},
Assignment(Assignment),
Var(Var),
Function(Function),

View file

@ -99,6 +99,37 @@ pub struct Condition {
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
///
/// Examples:
@ -110,7 +141,7 @@ pub struct Condition {
/// <br>
///
///```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<Expr>,
arguments: Vec<Expr>,
pub callee: Box<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
// todo: improve test-ability of parser
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 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())