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

@@ -1,35 +1,113 @@
use super::{
Function, Keyword, Node, Parsable, ParseResult, ParserError, ParserErrors, TokenCursor,
AsmFunction, Function, Impl, Keyword, Node, Parsable, ParseResult, ParserMsg, ParserOutput,
Struct, Symbol, Token, TokenCursor, Trait,
};
use std::fmt::Debug;
pub struct Module {
pub traits: Vec<Node<Trait>>,
pub structs: Vec<Node<Struct>>,
pub functions: Vec<Node<Function>>,
pub asm_fns: Vec<Node<AsmFunction>>,
pub impls: Vec<Node<Impl>>,
}
impl Parsable for Module {
fn parse(cursor: &mut TokenCursor, errors: &mut ParserErrors) -> ParseResult<Self> {
fn parse(cursor: &mut TokenCursor, errors: &mut ParserOutput) -> ParseResult<Self> {
let mut functions = Vec::new();
let mut structs = Vec::new();
let mut traits = Vec::new();
let mut impls = Vec::new();
let mut asm_fns = Vec::new();
loop {
let Some(next) = cursor.peek() else {
return ParseResult::Ok(Self { functions });
break;
};
if next.is_keyword(Keyword::Fn) {
let res = Node::parse(cursor, errors);
functions.push(res.node);
if res.recover {
return ParseResult::Recover(Self { functions });
if let Token::Keyword(kw) = next.token {
match kw {
Keyword::Fn => {
let res = Node::parse(cursor, errors);
functions.push(res.node);
if res.recover {
break;
}
}
Keyword::Struct => {
let res = Node::parse(cursor, errors);
structs.push(res.node);
if res.recover {
break;
}
}
Keyword::Trait => {
let res = Node::parse(cursor, errors);
traits.push(res.node);
if res.recover {
break;
}
}
Keyword::Impl => {
let res = Node::parse(cursor, errors);
impls.push(res.node);
if res.recover {
break;
}
}
Keyword::Asm => {
let res = Node::parse(cursor, errors);
asm_fns.push(res.node);
if res.recover {
break;
}
}
_ => {
errors.err(ParserMsg::unexpected_token(next, "a definition"));
cursor.next();
}
}
} else if next.is_symbol(Symbol::Semicolon) {
errors.hint(ParserMsg::from_instances(
&[next],
"unneeded semicolon".to_string(),
));
cursor.next();
} else {
errors.add(ParserError::unexpected_token(next, "fn"));
errors.err(ParserMsg::unexpected_token(next, "a definition"));
cursor.next();
}
}
ParseResult::Ok(Self {
functions,
structs,
traits,
impls,
asm_fns,
})
}
}
impl Debug for Module {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.functions.fmt(f)
for st in &self.structs {
st.fmt(f)?;
writeln!(f)?;
}
for t in &self.traits {
t.fmt(f)?;
writeln!(f)?;
}
for t in &self.impls {
t.fmt(f)?;
writeln!(f)?;
}
for func in &self.functions {
func.fmt(f)?;
writeln!(f)?;
}
for func in &self.asm_fns {
func.fmt(f)?;
writeln!(f)?;
}
Ok(())
}
}