Initial
This commit is contained in:
53
src/parser/typ.rs
Normal file
53
src/parser/typ.rs
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user