Replaced all the *Box with a generic LocBox<T>

This commit is contained in:
Gvidas Juknevičius 2024-12-22 02:17:43 +02:00
parent b8728b8f8d
commit d5f02cf1d5
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
15 changed files with 241 additions and 281 deletions

View File

@ -1,6 +1,4 @@
use std::fmt::Display;
use std::fmt::{Debug, Display};
#[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq)]
pub struct Loc {
@ -9,6 +7,7 @@ pub struct Loc {
col: usize,
}
#[allow(dead_code)]
impl Loc {
pub fn new(s: impl ToString, line: usize, col: usize) -> Self {
Self {
@ -27,6 +26,28 @@ impl Loc {
}
}
#[derive(Debug, Clone)]
pub struct LocBox<T: Clone + Debug> {
inner: T,
loc: Loc
}
impl<T: Clone + Debug> LocBox<T> {
pub fn new(loc: &Loc, inner: T) -> Self {
Self { loc: loc.clone(), inner }
}
pub fn inner(&self) -> &T {
&self.inner
}
pub fn inner_mut(&mut self) -> &mut T {
&mut self.inner
}
pub fn loc(&self) -> &Loc {
&self.loc
}
}
impl Display for Loc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}:{}", self.file, self.line, self.col)

View File

@ -1,76 +1,55 @@
use std::collections::HashMap;
use crate::{common::Loc, tokeniser::tokentype::*};
use crate::{common::loc::LocBox, tokeniser::tokentype::*};
use super::{typ::{Type, TypeBox}, Ast};
#[derive(Debug, Clone)]
pub struct ExprBox {
inner: Expr,
loc: Loc
}
impl ExprBox {
pub fn new(loc: &Loc, expr: Expr) -> Self {
Self { loc: loc.clone(), inner: expr }
}
pub fn inner(&self) -> &Expr {
&self.inner
}
pub fn inner_mut(&mut self) -> &mut Expr {
&mut self.inner
}
pub fn loc(&self) -> &Loc {
&self.loc
}
}
use super::{typ::Type, Ast};
#[derive(Debug, Clone)]
pub enum Expr {
// Comment(Comment),
Group(Box<ExprBox>),
Group(Box<LocBox<Expr>>),
UnOp {
typ: Punctuation,
right: Box<ExprBox>,
right: Box<LocBox<Expr>>,
},
BinOp {
typ: Punctuation,
left: Box<ExprBox>,
right: Box<ExprBox>,
left: Box<LocBox<Expr>>,
right: Box<LocBox<Expr>>,
},
Literal(super::literal::Literal),
ArrayIndex {
name: Box<ExprBox>,
index: Box<ExprBox>,
name: Box<LocBox<Expr>>,
index: Box<LocBox<Expr>>,
},
Path(Path),
Call {
path: Box<ExprBox>,
params: CallParams, // ExprBox ~ (, Expr)*
path: Box<LocBox<Expr>>,
params: CallParams, // LocBox<Expr> ~ (, Expr)*
},
//MethodCall {
// var_name: Box<ExprBox>,
// var_name: Box<LocBox<Expr>>,
// method_name: Ident,
// params: CallParams,
//},
/// the left side only exists on the /.|->/ chain
FieldAccess {
left: Box<Option<ExprBox>>,
right: Box<ExprBox>,
left: Box<Option<LocBox<Expr>>>,
right: Box<LocBox<Expr>>,
},
PtrFieldAccess {
left: Box<Option<ExprBox>>,
right: Box<ExprBox>,
left: Box<Option<LocBox<Expr>>>,
right: Box<LocBox<Expr>>,
},
ForLoop {
init: Box<Ast>,
test: Box<ExprBox>,
on_loop: Box<ExprBox>,
test: Box<LocBox<Expr>>,
on_loop: Box<LocBox<Expr>>,
body: Block,
},
WhileLoop {
test: Box<ExprBox>,
test: Box<LocBox<Expr>>,
body: Block,
},
InfLoop {
@ -79,20 +58,20 @@ pub enum Expr {
If(IfExpr),
Struct {
path: Path,
fields: HashMap<Ident, ExprBox>,
fields: HashMap<Ident, LocBox<Expr>>,
},
Return(Box<Option<ExprBox>>),
Return(Box<Option<LocBox<Expr>>>),
Break,
Continue,
Cast {
left: Box<ExprBox>,
right: Box<TypeBox>
left: Box<LocBox<Expr>>,
right: Box<LocBox<Type>>
},
}
impl ExprBox {
impl Expr {
pub fn unwrap_path(&self) -> Path {
let Expr::Path(p) = self.inner.clone() else {panic!("Unwrapping")};
let Expr::Path(p) = self else {panic!("Unwrapping")};
p.clone()
}
}
@ -100,7 +79,7 @@ impl ExprBox {
#[derive(Debug, Clone)]
pub struct CallParams(pub Vec<ExprBox>);
pub struct CallParams(pub Vec<LocBox<Expr>>);
#[derive(Debug, Clone)]
pub struct Block(pub Vec<Ast>);
@ -111,7 +90,7 @@ pub struct Path(pub Vec<Ident>);
#[derive(Debug, Clone)]
pub struct IfExpr {
pub test: Box<ExprBox>,
pub test: Box<LocBox<Expr>>,
pub body: Block,
pub else_if: Option<IfBranchExpr>
}

View File

@ -1,8 +1,8 @@
use std::collections::HashMap;
use crate::tokeniser::tokentype::*;
use crate::{common::loc::LocBox, tokeniser::tokentype::*};
use super::{expr::ExprBox, typ::TypeBox, Ast};
use super::{expr::Expr, typ::Type, Ast};
#[derive(Debug, Clone)]
pub enum Literal {
@ -10,10 +10,10 @@ pub enum Literal {
Ident(Ident),
String(TString),
Char(Char),
Array(Vec<ExprBox>),
Array(Vec<LocBox<Expr>>),
ArrayRepeat {
typ: Box<TypeBox>,
count: Box<ExprBox>,
typ: Box<LocBox<Type>>,
count: Box<LocBox<Expr>>,
},
Struct {
name: Ident,

View File

@ -2,6 +2,7 @@ use std::collections::HashMap;
use statement::{Enum, Function, Struct, TypeAlias};
use crate::common::loc::LocBox;
pub use crate::tokeniser::tokentype::*;
pub mod expr;
@ -21,8 +22,8 @@ pub struct Program {
#[derive(Debug, Clone)]
pub enum Ast {
Expr(expr::ExprBox),
Statement(statement::StatementBox),
Expr(LocBox<expr::Expr>),
Statement(LocBox<statement::Statement>),
}

View File

@ -1,28 +1,7 @@
use crate::common::Loc;
use crate::common::{loc::LocBox, Loc};
use super::{expr::{Block, ExprBox}, typ::{Type, TypeBox}, Ident, TString};
#[derive(Debug, Clone)]
pub struct StatementBox {
inner: Statement,
loc: Loc
}
impl StatementBox {
pub fn new(loc: &Loc, stat: Statement) -> Self {
Self { loc: loc.clone(), inner: stat }
}
pub fn inner(&self) -> &Statement {
&self.inner
}
pub fn inner_mut(&mut self) -> &mut Statement {
&mut self.inner
}
pub fn loc(&self) -> &Loc {
&self.loc
}
}
use super::{expr::{Block, Expr}, typ::Type, Ident, TString};
#[derive(Debug, Clone)]
pub enum Statement {
@ -32,31 +11,31 @@ pub enum Statement {
Enum(Enum),
ConstVar {
name: Ident,
typ: TypeBox,
val: ExprBox
typ: LocBox<Type>,
val: LocBox<Expr>
},
StaticVar {
name: Ident,
typ: TypeBox,
val: ExprBox,
typ: LocBox<Type>,
val: LocBox<Expr>,
},
Let {
name: Ident,
typ: Option<TypeBox>,
val: Option<ExprBox>,
typ: Option<LocBox<Type>>,
val: Option<LocBox<Expr>>,
},
}
#[derive(Debug, Clone)]
pub struct TypeAlias {
pub name: Ident,
pub typ: TypeBox,
pub typ: LocBox<Type>,
}
#[derive(Debug, Clone)]
pub struct Struct {
pub name: Ident,
pub fields: Vec<(Ident, TypeBox)>,
pub fields: Vec<(Ident, LocBox<Type>)>,
}
#[derive(Debug, Clone)]
@ -69,8 +48,8 @@ pub struct Enum {
pub struct Function {
pub struct_name: Option<Ident>,
pub name: Ident,
pub params: Vec<(Ident, TypeBox)>,
pub ret_type: Option<TypeBox>,
pub params: Vec<(Ident, LocBox<Type>)>,
pub ret_type: Option<LocBox<Type>>,
pub qual_const: bool,
pub qual_extern: Option<TString>, // abi
pub body: Option<Block>, // If None then its a type declaration

View File

@ -1,27 +1,6 @@
use crate::common::Loc;
use crate::common::{loc::LocBox, Loc};
use super::{expr::ExprBox, Ident};
#[derive(Debug, Clone)]
pub struct TypeBox {
inner: Type,
loc: Loc
}
impl TypeBox {
pub fn new(loc: &Loc, stat: Type) -> Self {
Self { loc: loc.clone(), inner: stat }
}
pub fn inner(&self) -> &Type {
&self.inner
}
pub fn inner_mut(&mut self) -> &mut Type {
&mut self.inner
}
pub fn loc(&self) -> &Loc {
&self.loc
}
}
use super::{expr::Expr, Ident};
#[derive(Debug, Clone)]
pub enum Type {
@ -34,7 +13,7 @@ pub enum Type {
},
ArrayRepeat {
inner: Box<Type>,
count: ExprBox,
count: LocBox<Expr>,
},
Owned(Ident),
}

View File

@ -2,9 +2,9 @@ use std::collections::HashMap;
use anyhow::{bail, Result};
use crate::{debug, error, lerror, parser::{typ::parse_type, Punctuation}, tokeniser::Token};
use crate::{common::loc::LocBox, debug, error, lerror, parser::{typ::parse_type, Punctuation}, tokeniser::Token};
use super::{ast::{expr::{Block, CallParams, Expr, ExprBox, IfBranchExpr, IfExpr, Path}, literal::Literal, TokenType}, parse_item, utils, Delimiter, Keyword};
use super::{ast::{expr::{Block, CallParams, Expr, IfBranchExpr, IfExpr, Path}, literal::Literal, TokenType}, parse_item, utils, Delimiter, Keyword};
const BINOP_LIST: &[TokenType] = &[
TokenType::Punct(Punctuation::Plus),
@ -37,14 +37,14 @@ const BINOP_LIST: &[TokenType] = &[
TokenType::Punct(Punctuation::Ge),
];
pub fn parse_expr(tokens: &mut Vec<Token>, precedence: usize, consume_semi: bool) -> Result<Option<ExprBox>> {
pub fn parse_expr(tokens: &mut Vec<Token>, precedence: usize, consume_semi: bool) -> Result<Option<LocBox<Expr>>> {
let res = if let Some(_) = utils::check(tokens, TokenType::Delim(Delimiter::ParenL)) {
Some(parse_group(tokens)?)
} else
if let Some(_) = utils::check(tokens, TokenType::ident("")) {
let p = parse_path(tokens)?;
if let Some(_) = utils::check(tokens, TokenType::Delim(Delimiter::CurlyL)) {
Some(parse_struct_literal(tokens, p.unwrap_path())?)
Some(parse_struct_literal(tokens, p.inner().unwrap_path())?)
} else {
Some(p)
}
@ -74,11 +74,11 @@ pub fn parse_expr(tokens: &mut Vec<Token>, precedence: usize, consume_semi: bool
} else if let Some(_) = utils::check(tokens, TokenType::Keyword(Keyword::Return)) {
return Ok(Some(parse_return(tokens)?));
} else if let Some(kw) = utils::check_consume(tokens, TokenType::Keyword(Keyword::Break)) {
return Ok(Some(ExprBox::new(kw.loc(), Expr::Break)));
return Ok(Some(LocBox::new(kw.loc(), Expr::Break)));
} else if let Some(kw) = utils::check_consume(tokens, TokenType::Keyword(Keyword::Continue)) {
return Ok(Some(ExprBox::new(kw.loc(), Expr::Continue)));
return Ok(Some(LocBox::new(kw.loc(), Expr::Continue)));
} else if let Some(kw) = utils::check(tokens, TokenType::Keyword(Keyword::If)) {
return Ok(Some(ExprBox::new(&kw.loc().clone(), Expr::If(parse_if(tokens)?))));
return Ok(Some(LocBox::new(&kw.loc().clone(), Expr::If(parse_if(tokens)?))));
} else {
None
};
@ -113,16 +113,16 @@ pub fn parse_expr(tokens: &mut Vec<Token>, precedence: usize, consume_semi: bool
Ok(res)
}
fn parse_return(tokens: &mut Vec<Token>) -> Result<ExprBox> {
fn parse_return(tokens: &mut Vec<Token>) -> Result<LocBox<Expr>> {
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Return), "")?;
let item = parse_expr(tokens, 0, true)?;
Ok(ExprBox::new(kw.loc(), Expr::Return(Box::new(item))))
Ok(LocBox::new(kw.loc(), Expr::Return(Box::new(item))))
}
fn parse_cast(tokens: &mut Vec<Token>, left: ExprBox) -> Result<ExprBox> {
fn parse_cast(tokens: &mut Vec<Token>, left: LocBox<Expr>) -> Result<LocBox<Expr>> {
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::As), "")?;
let typ = parse_type(tokens)?;
Ok(ExprBox::new(kw.loc(), Expr::Cast {
Ok(LocBox::new(kw.loc(), Expr::Cast {
left: Box::new(left),
right: Box::new(typ)
}))
@ -168,19 +168,19 @@ fn parse_if(tokens: &mut Vec<Token>) -> Result<IfExpr> {
})
}
}
fn parse_while_loop(tokens: &mut Vec<Token>) -> Result<ExprBox> {
fn parse_while_loop(tokens: &mut Vec<Token>) -> Result<LocBox<Expr>> {
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::While), "")?;
let Some(test) = parse_expr(tokens, 0, false)? else {
lerror!(kw.loc(), "Expected test comparrison for while loop, got nothing");
bail!("")
};
let block = parse_block(tokens)?;
Ok(ExprBox::new(kw.loc(), Expr::WhileLoop {
Ok(LocBox::new(kw.loc(), Expr::WhileLoop {
test: Box::new(test),
body: block
}))
}
fn parse_for_loop(tokens: &mut Vec<Token>) -> Result<ExprBox> {
fn parse_for_loop(tokens: &mut Vec<Token>) -> Result<LocBox<Expr>> {
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::For), "")?;
let Some(pre) = parse_item(tokens)? else {
lerror!(kw.loc(), "Expected init stat for a for loop, got nothing");
@ -198,19 +198,19 @@ fn parse_for_loop(tokens: &mut Vec<Token>) -> Result<ExprBox> {
};
let block = parse_block(tokens)?;
Ok(ExprBox::new(kw.loc(), Expr::ForLoop {
Ok(LocBox::new(kw.loc(), Expr::ForLoop {
init: Box::new(pre),
test: Box::new(test),
on_loop: Box::new(post),
body: block
}))
}
fn parse_inf_loop(tokens: &mut Vec<Token>) -> Result<ExprBox> {
fn parse_inf_loop(tokens: &mut Vec<Token>) -> Result<LocBox<Expr>> {
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Loop), "")?;
let block = parse_block(tokens)?;
Ok(ExprBox::new(kw.loc(), Expr::InfLoop { body: block }))
Ok(LocBox::new(kw.loc(), Expr::InfLoop { body: block }))
}
fn parse_fn_call(tokens: &mut Vec<Token>, left: ExprBox) -> Result<ExprBox> {
fn parse_fn_call(tokens: &mut Vec<Token>, left: LocBox<Expr>) -> Result<LocBox<Expr>> {
let start = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::ParenL), "")?;
let mut params = Vec::new();
@ -228,22 +228,22 @@ fn parse_fn_call(tokens: &mut Vec<Token>, left: ExprBox) -> Result<ExprBox> {
}
}
_ = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::ParenR), "");
Ok(ExprBox::new(start.loc(), Expr::Call { path: Box::new(left), params: CallParams(params) }))
Ok(LocBox::new(start.loc(), Expr::Call { path: Box::new(left), params: CallParams(params) }))
}
fn parse_array_index(tokens: &mut Vec<Token>, left: ExprBox) -> Result<ExprBox> {
fn parse_array_index(tokens: &mut Vec<Token>, left: LocBox<Expr>) -> Result<LocBox<Expr>> {
let start = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::SquareL), "")?;
let Some(idx) = parse_expr(tokens, 0, false)? else {
lerror!(start.loc(), "Expected index for in array index but found nothing.");
bail!("")
};
_ = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::SquareR), "");
Ok(ExprBox::new(start.loc(), Expr::ArrayIndex {
Ok(LocBox::new(start.loc(), Expr::ArrayIndex {
name: Box::new(left),
index: Box::new(idx)
}))
}
fn parse_field_access(tokens: &mut Vec<Token>, left: ExprBox) -> Result<ExprBox> {
fn parse_field_access(tokens: &mut Vec<Token>, left: LocBox<Expr>) -> Result<LocBox<Expr>> {
let start = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Fieldaccess), "unreachable")?;
let right = if let Some(_) = utils::check_2_last(tokens, TokenType::Punct(Punctuation::Arrow)) {
@ -255,13 +255,13 @@ fn parse_field_access(tokens: &mut Vec<Token>, left: ExprBox) -> Result<ExprBox>
} else {
parse_path(tokens)?
};
Ok(ExprBox::new(start.loc(), Expr::FieldAccess {
Ok(LocBox::new(start.loc(), Expr::FieldAccess {
left: Box::new(Some(left)),
right: Box::new(right)
}))
}
fn parse_ptr_field_access(tokens: &mut Vec<Token>, left: ExprBox) -> Result<ExprBox> {
fn parse_ptr_field_access(tokens: &mut Vec<Token>, left: LocBox<Expr>) -> Result<LocBox<Expr>> {
let start = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Arrow), "unreachable")?;
let right = if let Some(_) = utils::check_2_last(tokens, TokenType::Punct(Punctuation::Arrow)) {
let right = parse_path(tokens)?;
@ -272,28 +272,28 @@ fn parse_ptr_field_access(tokens: &mut Vec<Token>, left: ExprBox) -> Result<Expr
} else {
parse_path(tokens)?
};
Ok(ExprBox::new(start.loc(), Expr::PtrFieldAccess {
Ok(LocBox::new(start.loc(), Expr::PtrFieldAccess {
left: Box::new(Some(left)),
right: Box::new(right)
}))
}
fn parse_literal(tokens: &mut Vec<Token>) -> Result<ExprBox> {
fn parse_literal(tokens: &mut Vec<Token>) -> Result<LocBox<Expr>> {
if let Some(tkn) = utils::check_consume(tokens, TokenType::string("", false)) {
let TokenType::String(str) = tkn.tt() else {unreachable!()};
return Ok(ExprBox::new(tkn.loc(), Expr::Literal(Literal::String(str.clone()))));
return Ok(LocBox::new(tkn.loc(), Expr::Literal(Literal::String(str.clone()))));
} else
if let Some(tkn) = utils::check_consume(tokens, TokenType::number(0, 0, false)) {
let TokenType::Number(val) = tkn.tt() else {unreachable!()};
return Ok(ExprBox::new(tkn.loc(), Expr::Literal(Literal::Number(val.clone()))));
return Ok(LocBox::new(tkn.loc(), Expr::Literal(Literal::Number(val.clone()))));
} else
if let Some(tkn) = utils::check_consume(tokens, TokenType::char('\0')) {
let TokenType::Char(val) = tkn.tt() else {unreachable!()};
return Ok(ExprBox::new(tkn.loc(), Expr::Literal(Literal::Char(val.clone()))));
return Ok(LocBox::new(tkn.loc(), Expr::Literal(Literal::Char(val.clone()))));
} else
if let Some(start) = utils::check_consume(tokens, TokenType::Delim(Delimiter::SquareL)) {
if let Some(_) = utils::check_consume(tokens, TokenType::Delim(Delimiter::SquareR)) {
return Ok(ExprBox::new(start.loc(), Expr::Literal(Literal::Array(Vec::new()))));
return Ok(LocBox::new(start.loc(), Expr::Literal(Literal::Array(Vec::new()))));
}
if *tokens[tokens.len()-2].tt() == TokenType::Punct(Punctuation::Comma) {
let first = parse_expr(tokens, 0, false)?;
@ -310,12 +310,12 @@ fn parse_literal(tokens: &mut Vec<Token>) -> Result<ExprBox> {
}
}
utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::SquareR), "")?;
return Ok(ExprBox::new(start.loc(), Expr::Literal(Literal::Array(values))));
return Ok(LocBox::new(start.loc(), Expr::Literal(Literal::Array(values))));
} else if *tokens[tokens.len()-2].tt() == TokenType::Punct(Punctuation::Semi) {
let typ = parse_type(tokens)?;
let count = parse_expr(tokens, 0, false)?.unwrap();
utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::SquareR), "")?;
return Ok(ExprBox::new(start.loc(), Expr::Literal(Literal::ArrayRepeat {
return Ok(LocBox::new(start.loc(), Expr::Literal(Literal::ArrayRepeat {
typ: Box::new(typ),
count: Box::new(count)
})));
@ -331,7 +331,7 @@ fn parse_literal(tokens: &mut Vec<Token>) -> Result<ExprBox> {
unreachable!()
}
fn parse_struct_literal(tokens: &mut Vec<Token>, name: Path) -> Result<ExprBox> {
fn parse_struct_literal(tokens: &mut Vec<Token>, name: Path) -> Result<LocBox<Expr>> {
let start = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::CurlyL), "")?;
let mut fields = HashMap::new();
while !tokens.is_empty() {
@ -348,10 +348,10 @@ fn parse_struct_literal(tokens: &mut Vec<Token>, name: Path) -> Result<ExprBox>
break;
}
}
Ok(ExprBox::new(start.loc(), Expr::Struct { path: name, fields }))
Ok(LocBox::new(start.loc(), Expr::Struct { path: name, fields }))
}
fn parse_group(tokens: &mut Vec<Token>) -> Result<ExprBox> {
fn parse_group(tokens: &mut Vec<Token>) -> Result<LocBox<Expr>> {
let start = utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::ParenL), "")?;
let Some(expr) = parse_expr(tokens, 0, false)? else {
lerror!(start.loc(), "Expected expr found nothing");
@ -359,10 +359,10 @@ fn parse_group(tokens: &mut Vec<Token>) -> Result<ExprBox> {
};
utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::ParenR), "")?;
Ok(ExprBox::new(start.loc(), Expr::Group(Box::new(expr))))
Ok(LocBox::new(start.loc(), Expr::Group(Box::new(expr))))
}
fn parse_path(tokens: &mut Vec<Token>) -> Result<ExprBox> {
fn parse_path(tokens: &mut Vec<Token>) -> Result<LocBox<Expr>> {
let mut buf = Vec::new();
let part = utils::check_consume(tokens, TokenType::ident("")).unwrap();
@ -375,10 +375,10 @@ fn parse_path(tokens: &mut Vec<Token>) -> Result<ExprBox> {
buf.push(part.tt().unwrap_ident());
}
Ok(ExprBox::new(part.loc(), Expr::Path(Path(buf))))
Ok(LocBox::new(part.loc(), Expr::Path(Path(buf))))
}
fn parse_unop(tokens: &mut Vec<Token>) -> Result<ExprBox> {
fn parse_unop(tokens: &mut Vec<Token>) -> Result<LocBox<Expr>> {
let typ = utils::check_consume_or_err_from_many(tokens, &[
TokenType::Punct(Punctuation::Not),
TokenType::Punct(Punctuation::Plus),
@ -393,13 +393,13 @@ fn parse_unop(tokens: &mut Vec<Token>) -> Result<ExprBox> {
lerror!(&loc, "Expected expression after unary token, found nothing");
bail!("")
};
Ok(ExprBox::new(&loc, Expr::UnOp {
Ok(LocBox::new(&loc, Expr::UnOp {
typ,
right: Box::new(right)
}))
}
fn parse_binop(tokens: &mut Vec<Token>, mut lhs: ExprBox, precedence: usize) -> Result<ExprBox> {
fn parse_binop(tokens: &mut Vec<Token>, mut lhs: LocBox<Expr>, precedence: usize) -> Result<LocBox<Expr>> {
// TODO: https://en.wikipedia.org/wiki/Operator-precedence_parser#Pseudocode
loop {
@ -431,7 +431,7 @@ fn parse_binop(tokens: &mut Vec<Token>, mut lhs: ExprBox, precedence: usize) ->
_ = tokens.pop();
let Some(rhs) = parse_expr(tokens, rp, false)? else {break;};
lhs = ExprBox::new(&op_loc, Expr::BinOp {
lhs = LocBox::new(&op_loc, Expr::BinOp {
typ: op,
left: Box::new(lhs),
right: Box::new(rhs)

View File

@ -1,19 +1,20 @@
use anyhow::bail;
use crate::common::loc::LocBox;
use crate::lerror;
use crate::parser::ast::TokenType;
use crate::parser::expr::parse_expr;
use crate::parser::{Delimiter, Ident, Keyword, Punctuation};
use crate::tokeniser::Token;
use super::ast::typ::{Type, TypeBox};
use super::ast::typ::Type;
use super::expr::parse_block;
use super::typ::parse_type;
use super::utils;
use super::ast::statement::{Enum, Function, Statement, StatementBox, Struct, TypeAlias};
use super::ast::statement::{Enum, Function, Statement, Struct, TypeAlias};
type Result<T> = anyhow::Result<T>;
pub fn parse_statement(tokens: &mut Vec<Token>) -> Result<Option<StatementBox>> {
pub fn parse_statement(tokens: &mut Vec<Token>) -> Result<Option<LocBox<Statement>>> {
if let Some(_) = utils::check(tokens, TokenType::Keyword(Keyword::Fn)) {
Ok(Some(parse_fn(tokens)?))
} else
@ -39,7 +40,7 @@ pub fn parse_statement(tokens: &mut Vec<Token>) -> Result<Option<StatementBox>>
}
}
fn parse_enum(tokens: &mut Vec<Token>) -> Result<StatementBox> {
fn parse_enum(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Enum), "")?;
let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident();
_ = utils::check_consume(tokens, TokenType::Delim(Delimiter::CurlyL));
@ -60,10 +61,10 @@ fn parse_enum(tokens: &mut Vec<Token>) -> Result<StatementBox> {
fields.push(field_name);
}
Ok(StatementBox::new(kw.loc(), Statement::Enum(Enum { name, fields })))
Ok(LocBox::new(kw.loc(), Statement::Enum(Enum { name, fields })))
}
fn parse_struct(tokens: &mut Vec<Token>) -> Result<StatementBox> {
fn parse_struct(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Struct), "")?;
let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident();
_ = utils::check_consume(tokens, TokenType::Delim(Delimiter::CurlyL));
@ -86,10 +87,10 @@ fn parse_struct(tokens: &mut Vec<Token>) -> Result<StatementBox> {
fields.push((field_name, typ));
}
Ok(StatementBox::new(kw.loc(), Statement::Struct(Struct { name, fields })))
Ok(LocBox::new(kw.loc(), Statement::Struct(Struct { name, fields })))
}
fn parse_static(tokens: &mut Vec<Token>) -> Result<StatementBox> {
fn parse_static(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Static), "")?;
let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident();
@ -101,10 +102,10 @@ fn parse_static(tokens: &mut Vec<Token>) -> Result<StatementBox> {
bail!("")
};
_ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Semi), "")?;
Ok(StatementBox::new(kw.loc(), Statement::StaticVar { name, typ, val }))
Ok(LocBox::new(kw.loc(), Statement::StaticVar { name, typ, val }))
}
fn parse_let(tokens: &mut Vec<Token>) -> Result<StatementBox> {
fn parse_let(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Let), "")?;
let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident();
let mut typ = None;
@ -120,9 +121,9 @@ fn parse_let(tokens: &mut Vec<Token>) -> Result<StatementBox> {
val = Some(_val);
}
_ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Semi), "")?;
Ok(StatementBox::new(kw.loc(), Statement::Let { name, typ, val }))
Ok(LocBox::new(kw.loc(), Statement::Let { name, typ, val }))
}
fn parse_constant(tokens: &mut Vec<Token>) -> Result<StatementBox> {
fn parse_constant(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Const), "")?;
if let Some(_) = utils::check(tokens, TokenType::Keyword(Keyword::Fn)) {
@ -137,20 +138,20 @@ fn parse_constant(tokens: &mut Vec<Token>) -> Result<StatementBox> {
bail!("")
};
_ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Semi), "")?;
Ok(StatementBox::new(kw.loc(), Statement::ConstVar { name, typ, val }))
Ok(LocBox::new(kw.loc(), Statement::ConstVar { name, typ, val }))
}
fn parse_type_alias(tokens: &mut Vec<Token>) -> Result<StatementBox> {
fn parse_type_alias(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Type), "")?;
let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident();
_ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Eq), "")?;
let typ = parse_type(tokens)?;
_ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Semi), "")?;
Ok(StatementBox::new(kw.loc(), Statement::TypeAlias(TypeAlias { name, typ })))
Ok(LocBox::new(kw.loc(), Statement::TypeAlias(TypeAlias { name, typ })))
}
fn parse_fn(tokens: &mut Vec<Token>) -> Result<StatementBox> {
fn parse_fn(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
// Just remove the kw since we checked it before
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Fn), "")?;
@ -177,7 +178,7 @@ fn parse_fn(tokens: &mut Vec<Token>) -> Result<StatementBox> {
body = None;
}
Ok(StatementBox::new(kw.loc(), Statement::Fn(Function{
Ok(LocBox::new(kw.loc(), Statement::Fn(Function{
struct_name,
name,
params,
@ -190,7 +191,7 @@ fn parse_fn(tokens: &mut Vec<Token>) -> Result<StatementBox> {
fn parse_fn_params(tokens: &mut Vec<Token>) -> Result<Vec<(Ident, TypeBox)>> {
fn parse_fn_params(tokens: &mut Vec<Token>) -> Result<Vec<(Ident, LocBox<Type>)>> {
let mut args = Vec::new();
utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::ParenL), "")?;
while !tokens.is_empty() {

View File

@ -1,10 +1,10 @@
use anyhow::Result;
use crate::{parser::Delimiter, tokeniser::Token};
use crate::{common::loc::LocBox, parser::Delimiter, tokeniser::Token};
use super::{ast::{typ::{Type, TypeBox}, TokenType}, expr::parse_expr, utils, Keyword, Punctuation};
use super::{ast::{typ::Type, TokenType}, expr::parse_expr, utils, Keyword, Punctuation};
pub fn parse_type(tokens: &mut Vec<Token>) -> Result<TypeBox> {
pub fn parse_type(tokens: &mut Vec<Token>) -> Result<LocBox<Type>> {
let mut ref_cnt = Vec::new();
let mut loc = None;
while let Some(tok) = utils::check_consume(tokens, TokenType::Punct(Punctuation::Ampersand)) {
@ -60,5 +60,5 @@ pub fn parse_type(tokens: &mut Vec<Token>) -> Result<TypeBox> {
_ => unreachable!()
}
}
Ok(TypeBox::new(&loc.unwrap(), typ))
Ok(LocBox::new(&loc.unwrap(), typ))
}

View File

@ -2,7 +2,7 @@ Program {
ast: Block(
[
Statement(
StatementBox {
LocBox {
inner: Enum(
Enum {
name: Ident(
@ -19,7 +19,7 @@ Program {
},
),
Statement(
StatementBox {
LocBox {
inner: Enum(
Enum {
name: Ident(

View File

@ -2,20 +2,20 @@ Program {
ast: Block(
[
Statement(
StatementBox {
LocBox {
inner: Let {
name: Ident(
"a",
),
typ: None,
val: Some(
ExprBox {
LocBox {
inner: BinOp {
typ: EqEq,
left: ExprBox {
left: LocBox {
inner: BinOp {
typ: Star,
left: ExprBox {
left: LocBox {
inner: Literal(
Number(
Number {
@ -31,7 +31,7 @@ Program {
col: 10,
},
},
right: ExprBox {
right: LocBox {
inner: Literal(
Number(
Number {
@ -54,7 +54,7 @@ Program {
col: 12,
},
},
right: ExprBox {
right: LocBox {
inner: Literal(
Number(
Number {
@ -87,20 +87,20 @@ Program {
},
),
Statement(
StatementBox {
LocBox {
inner: Let {
name: Ident(
"b",
),
typ: None,
val: Some(
ExprBox {
LocBox {
inner: BinOp {
typ: EqEq,
left: ExprBox {
left: LocBox {
inner: BinOp {
typ: Div,
left: ExprBox {
left: LocBox {
inner: Literal(
Number(
Number {
@ -116,7 +116,7 @@ Program {
col: 10,
},
},
right: ExprBox {
right: LocBox {
inner: Literal(
Number(
Number {
@ -139,10 +139,10 @@ Program {
col: 11,
},
},
right: ExprBox {
right: LocBox {
inner: UnOp {
typ: Star,
right: ExprBox {
right: LocBox {
inner: Path(
Path(
[
@ -182,22 +182,22 @@ Program {
},
),
Statement(
StatementBox {
LocBox {
inner: Let {
name: Ident(
"c",
),
typ: None,
val: Some(
ExprBox {
LocBox {
inner: BinOp {
typ: Div,
left: ExprBox {
left: LocBox {
inner: Group(
ExprBox {
LocBox {
inner: PtrFieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -214,10 +214,10 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: FieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -234,10 +234,10 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: PtrFieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -254,7 +254,7 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: Path(
Path(
[
@ -298,7 +298,7 @@ Program {
col: 10,
},
},
right: ExprBox {
right: LocBox {
inner: Literal(
Number(
Number {
@ -331,17 +331,17 @@ Program {
},
),
Statement(
StatementBox {
LocBox {
inner: Let {
name: Ident(
"d",
),
typ: None,
val: Some(
ExprBox {
LocBox {
inner: BinOp {
typ: Div,
left: ExprBox {
left: LocBox {
inner: Literal(
Number(
Number {
@ -357,10 +357,10 @@ Program {
col: 10,
},
},
right: ExprBox {
right: LocBox {
inner: PtrFieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -377,10 +377,10 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: FieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -397,10 +397,10 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: PtrFieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -417,7 +417,7 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: Path(
Path(
[
@ -471,20 +471,20 @@ Program {
},
),
Statement(
StatementBox {
LocBox {
inner: Let {
name: Ident(
"e",
),
typ: None,
val: Some(
ExprBox {
LocBox {
inner: BinOp {
typ: Div,
left: ExprBox {
left: LocBox {
inner: PtrFieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -501,10 +501,10 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: FieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -521,10 +521,10 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: PtrFieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -541,7 +541,7 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: Path(
Path(
[
@ -578,7 +578,7 @@ Program {
col: 11,
},
},
right: ExprBox {
right: LocBox {
inner: Literal(
Number(
Number {
@ -611,20 +611,20 @@ Program {
},
),
Statement(
StatementBox {
LocBox {
inner: Let {
name: Ident(
"f",
),
typ: None,
val: Some(
ExprBox {
LocBox {
inner: BinOp {
typ: Div,
left: ExprBox {
left: LocBox {
inner: FieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -641,10 +641,10 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: FieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -661,10 +661,10 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: FieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -681,7 +681,7 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: Path(
Path(
[
@ -718,7 +718,7 @@ Program {
col: 11,
},
},
right: ExprBox {
right: LocBox {
inner: Literal(
Number(
Number {
@ -751,22 +751,22 @@ Program {
},
),
Statement(
StatementBox {
LocBox {
inner: Let {
name: Ident(
"g",
),
typ: None,
val: Some(
ExprBox {
LocBox {
inner: BinOp {
typ: Star,
left: ExprBox {
left: LocBox {
inner: ArrayIndex {
name: ExprBox {
name: LocBox {
inner: FieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -783,7 +783,7 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: Path(
Path(
[
@ -806,13 +806,13 @@ Program {
col: 11,
},
},
index: ExprBox {
index: LocBox {
inner: UnOp {
typ: Star,
right: ExprBox {
right: LocBox {
inner: FieldAccess {
left: Some(
ExprBox {
LocBox {
inner: Path(
Path(
[
@ -829,7 +829,7 @@ Program {
},
},
),
right: ExprBox {
right: LocBox {
inner: Path(
Path(
[
@ -866,7 +866,7 @@ Program {
col: 13,
},
},
right: ExprBox {
right: LocBox {
inner: Literal(
Number(
Number {

View File

@ -2,7 +2,7 @@ Program {
ast: Block(
[
Statement(
StatementBox {
LocBox {
inner: Fn(
Function {
struct_name: None,
@ -14,7 +14,7 @@ Program {
Ident(
"argc",
),
TypeBox {
LocBox {
inner: Owned(
Ident(
"i32",
@ -31,7 +31,7 @@ Program {
Ident(
"argv",
),
TypeBox {
LocBox {
inner: Ref {
inner: Array {
inner: Owned(
@ -51,7 +51,7 @@ Program {
),
],
ret_type: Some(
TypeBox {
LocBox {
inner: Owned(
Ident(
"i32",
@ -70,10 +70,10 @@ Program {
Block(
[
Expr(
ExprBox {
LocBox {
inner: Return(
Some(
ExprBox {
LocBox {
inner: Literal(
Number(
Number {
@ -111,7 +111,7 @@ Program {
},
),
Statement(
StatementBox {
LocBox {
inner: Fn(
Function {
struct_name: Some(
@ -127,7 +127,7 @@ Program {
Ident(
"self",
),
TypeBox {
LocBox {
inner: Ref {
inner: Owned(
Ident(
@ -147,7 +147,7 @@ Program {
Ident(
"a",
),
TypeBox {
LocBox {
inner: Ref {
inner: Owned(
Ident(
@ -167,7 +167,7 @@ Program {
Ident(
"b",
),
TypeBox {
LocBox {
inner: Ref {
inner: Owned(
Ident(
@ -185,7 +185,7 @@ Program {
),
],
ret_type: Some(
TypeBox {
LocBox {
inner: Ref {
inner: Owned(
Ident(
@ -214,7 +214,7 @@ Program {
},
),
Statement(
StatementBox {
LocBox {
inner: Fn(
Function {
struct_name: Some(
@ -230,7 +230,7 @@ Program {
Ident(
"a",
),
TypeBox {
LocBox {
inner: Ref {
inner: Owned(
Ident(
@ -250,7 +250,7 @@ Program {
Ident(
"b",
),
TypeBox {
LocBox {
inner: Ref {
inner: Owned(
Ident(
@ -268,7 +268,7 @@ Program {
),
],
ret_type: Some(
TypeBox {
LocBox {
inner: Ref {
inner: Owned(
Ident(

View File

@ -2,13 +2,13 @@ Program {
ast: Block(
[
Expr(
ExprBox {
LocBox {
inner: If(
IfExpr {
test: ExprBox {
test: LocBox {
inner: BinOp {
typ: Gt,
left: ExprBox {
left: LocBox {
inner: Path(
Path(
[
@ -24,7 +24,7 @@ Program {
col: 5,
},
},
right: ExprBox {
right: LocBox {
inner: Literal(
Number(
Number {

View File

@ -2,17 +2,17 @@ Program {
ast: Block(
[
Expr(
ExprBox {
LocBox {
inner: ForLoop {
init: Statement(
StatementBox {
LocBox {
inner: Let {
name: Ident(
"i",
),
typ: None,
val: Some(
ExprBox {
LocBox {
inner: Literal(
Number(
Number {
@ -37,10 +37,10 @@ Program {
},
},
),
test: ExprBox {
test: LocBox {
inner: BinOp {
typ: Lt,
left: ExprBox {
left: LocBox {
inner: Path(
Path(
[
@ -56,7 +56,7 @@ Program {
col: 17,
},
},
right: ExprBox {
right: LocBox {
inner: Literal(
Number(
Number {
@ -79,10 +79,10 @@ Program {
col: 19,
},
},
on_loop: ExprBox {
on_loop: LocBox {
inner: BinOp {
typ: AddEq,
left: ExprBox {
left: LocBox {
inner: Path(
Path(
[
@ -98,7 +98,7 @@ Program {
col: 25,
},
},
right: ExprBox {
right: LocBox {
inner: Literal(
Number(
Number {
@ -133,12 +133,12 @@ Program {
},
),
Expr(
ExprBox {
LocBox {
inner: WhileLoop {
test: ExprBox {
test: LocBox {
inner: BinOp {
typ: Gt,
left: ExprBox {
left: LocBox {
inner: Path(
Path(
[
@ -154,7 +154,7 @@ Program {
col: 8,
},
},
right: ExprBox {
right: LocBox {
inner: Literal(
Number(
Number {
@ -189,7 +189,7 @@ Program {
},
),
Expr(
ExprBox {
LocBox {
inner: InfLoop {
body: Block(
[],

View File

@ -2,7 +2,7 @@ Program {
ast: Block(
[
Statement(
StatementBox {
LocBox {
inner: Struct(
Struct {
name: Ident(
@ -19,7 +19,7 @@ Program {
},
),
Statement(
StatementBox {
LocBox {
inner: Struct(
Struct {
name: Ident(
@ -30,7 +30,7 @@ Program {
Ident(
"a",
),
TypeBox {
LocBox {
inner: Ref {
inner: Owned(
Ident(