a ton of stuff idk more ir work

This commit is contained in:
2024-10-22 02:30:50 -04:00
parent 14a4fb1ff9
commit 87f755b763
46 changed files with 1967 additions and 540 deletions

View File

@@ -0,0 +1,45 @@
use std::fmt::Debug;
use super::{
util::parse_list, Ident, Keyword, Node, Parsable, ParseResult, ParserMsg, ParserOutput,
Symbol, TokenCursor, Type, VarDef,
};
#[derive(Debug)]
pub struct Struct {
pub name: Node<Ident>,
pub fields: StructFields,
}
#[derive(Debug)]
pub enum StructFields {
Named(Vec<Node<VarDef>>),
Tuple(Vec<Node<Type>>),
None,
}
impl Parsable for Struct {
fn parse(cursor: &mut TokenCursor, errors: &mut ParserOutput) -> ParseResult<Self> {
cursor.expect_kw(Keyword::Struct)?;
let name = Node::parse(cursor, errors)?;
let next = cursor.expect_peek()?;
let fields = if next.is_symbol(Symbol::Semicolon) {
cursor.next();
StructFields::None
} else if next.is_symbol(Symbol::OpenCurly) {
cursor.next();
StructFields::Named(parse_list(cursor, errors, Symbol::CloseCurly)?)
} else if next.is_symbol(Symbol::OpenParen) {
cursor.next();
StructFields::Tuple(parse_list(cursor, errors, Symbol::CloseParen)?)
} else {
errors.err(ParserMsg::unexpected_token(next, "`;`, `(`, or `{`"));
return ParseResult::Recover(Struct {
name,
fields: StructFields::None,
});
};
ParseResult::Ok(Struct { name, fields })
}
}