INITIAL GENERICS IMPL

This commit is contained in:
2025-04-15 03:21:57 -04:00
parent 993458f4be
commit 329b1d86ac
18 changed files with 607 additions and 381 deletions
+14 -4
View File
@@ -3,13 +3,14 @@ use std::fmt::Debug;
use crate::parser::ParsableWith;
use super::{
util::parse_list, CompilerMsg, Keyword, Node, PExpr, PFieldDef, PIdent, PType, PVarDef,
Parsable, ParseResult, ParserCtx, Symbol,
util::parse_list, CompilerMsg, Keyword, Node, PExpr, PFieldDef, PIdent, PType, PGenericDef,
PVarDef, Parsable, ParseResult, ParserCtx, Symbol,
};
#[derive(Debug)]
pub struct PStruct {
pub name: Node<PIdent>,
pub generics: Vec<Node<PGenericDef>>,
pub fields: PStructFields,
}
@@ -37,7 +38,15 @@ impl Parsable for PStruct {
fn parse(ctx: &mut ParserCtx) -> ParseResult<Self> {
ctx.expect_kw(Keyword::Struct)?;
let name = ctx.parse()?;
let next = ctx.expect_peek()?;
let mut next = ctx.expect_peek()?;
let args = if next.is_symbol(Symbol::OpenAngle) {
ctx.next();
let res = parse_list(ctx, Symbol::CloseAngle)?;
next = ctx.expect_peek()?;
res
} else {
Vec::new()
};
let fields = if next.is_symbol(Symbol::Semicolon) {
ctx.next();
PStructFields::None
@@ -52,10 +61,11 @@ impl Parsable for PStruct {
ctx.err(msg);
return ParseResult::Recover(PStruct {
name,
generics: args,
fields: PStructFields::None,
});
};
ParseResult::Ok(PStruct { name, fields })
ParseResult::Ok(PStruct { name, generics: args, fields })
}
}
+18 -3
View File
@@ -1,14 +1,16 @@
use std::fmt::Debug;
use super::{
util::parse_list, CompilerMsg, Node, PIdent, Parsable, ParseResult, ParserCtx, Symbol, Token,
};
use super::{util::parse_list, Node, PIdent, Parsable, ParseResult, ParserCtx, Symbol};
pub struct PType {
pub name: Node<PIdent>,
pub args: Vec<Node<PType>>,
}
pub struct PGenericDef {
pub name: Node<PIdent>,
}
impl Parsable for PType {
fn parse(ctx: &mut ParserCtx) -> ParseResult<Self> {
let next = ctx.expect_peek()?;
@@ -35,6 +37,12 @@ impl Parsable for PType {
}
}
impl Parsable for PGenericDef {
fn parse(ctx: &mut ParserCtx) -> ParseResult<Self> {
ParseResult::Ok(Self { name: ctx.parse()? })
}
}
impl Debug for PType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.name)?;
@@ -46,3 +54,10 @@ impl Debug for PType {
Ok(())
}
}
impl Debug for PGenericDef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.name)?;
Ok(())
}
}