This commit is contained in:
2024-12-21 03:22:07 +02:00
commit 54b6df5862
31 changed files with 2217 additions and 0 deletions

53
src/parser/typ.rs Normal file
View File

@@ -0,0 +1,53 @@
use anyhow::Result;
use crate::tokeniser::Token;
use super::{ast::{typ::Type, TokenType}, expr::parse_expr, utils, Keyword, Punctuation};
pub fn parse_type(tokens: &mut Vec<Token>) -> Result<Type> {
let mut ref_cnt = Vec::new();
while let Some(tok) = utils::check_consume(tokens, TokenType::Punct(Punctuation::Ampersand)) {
if let Some(tok) = utils::check_consume(tokens, TokenType::Keyword(Keyword::Mut)) {
ref_cnt.push(tok.clone());
} else {
ref_cnt.push(tok.clone());
}
}
let mut typ;
if let Some(_) = utils::check(tokens, TokenType::Delim(super::Delimiter::SquareL)) {
let itm_typ = parse_type(tokens)?;
if let Some(_) = utils::check_consume(tokens, TokenType::Punct(Punctuation::Semi)) {
let count = parse_expr(tokens, 0, false)?.unwrap();
typ = Type::ArrayRepeat {
inner: Box::new(itm_typ),
count
}
} else {
typ = Type::Array {
inner: Box::new(itm_typ),
}
}
} else {
let ident = utils::check_consume_or_err(tokens, TokenType::ident(""), "a")?;
typ = Type::Owned(ident.tt().unwrap_ident());
}
while let Some(reft) = ref_cnt.pop() {
match reft.tt() {
TokenType::Keyword(Keyword::Mut) => {
typ = Type::Ref {
inner: Box::new(typ),
mutable: true
}
},
TokenType::Punct(Punctuation::Ampersand) => {
typ = Type::Ref {
inner: Box::new(typ),
mutable: false
}
}
_ => unreachable!()
}
}
Ok(typ)
}