type checking !?!?

This commit is contained in:
2025-03-22 14:40:32 -04:00
parent 606cb30c6b
commit 7f809d797c
44 changed files with 664 additions and 314 deletions

View File

@@ -1,7 +1,8 @@
use std::fmt::Debug;
use super::{
PIdent, MaybeParsable, Node, Parsable, ParseResult, ParserCtx, ParserMsg, Symbol, Token, PType,
MaybeParsable, Node, PExpr, PIdent, PType, Parsable, ParseResult, ParserCtx, Symbol,
Token, CompilerMsg
};
pub struct PVarDef {
@@ -9,6 +10,11 @@ pub struct PVarDef {
pub ty: Option<Node<PType>>,
}
pub struct PFieldDef {
pub name: Node<PIdent>,
pub val: Option<Node<PExpr>>,
}
impl Parsable for PVarDef {
fn parse(ctx: &mut ParserCtx) -> ParseResult<Self> {
let name = ctx.parse()?;
@@ -21,6 +27,21 @@ impl Parsable for PVarDef {
}
}
impl Parsable for PFieldDef {
fn parse(ctx: &mut ParserCtx) -> ParseResult<Self> {
let name = ctx.parse()?;
if ctx.peek().is_some_and(|n| n.is_symbol(Symbol::Colon)) {
ctx.next();
ctx.parse().map(|ty| Self {
name,
val: Some(ty),
})
} else {
ParseResult::Ok(Self { name, val: None })
}
}
}
pub struct SelfVar {
pub ty: SelfType,
}
@@ -32,7 +53,7 @@ pub enum SelfType {
}
impl MaybeParsable for SelfVar {
fn maybe_parse(ctx: &mut ParserCtx) -> Result<Option<Self>, super::ParserMsg> {
fn maybe_parse(ctx: &mut ParserCtx) -> Result<Option<Self>, CompilerMsg> {
if let Some(mut next) = ctx.peek() {
let mut ty = SelfType::Take;
if next.is_symbol(Symbol::Ampersand) {
@@ -47,7 +68,7 @@ impl MaybeParsable for SelfVar {
}
}
if ty != SelfType::Take {
return Err(ParserMsg::unexpected_token(next, "self"));
return Err(CompilerMsg::unexpected_token(next, "self"));
}
}
Ok(None)
@@ -64,6 +85,16 @@ impl Debug for PVarDef {
}
}
impl Debug for PFieldDef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.name.fmt(f)?;
if let Some(val) = &self.val {
write!(f, ": {:?}", val)?;
}
Ok(())
}
}
impl Debug for SelfVar {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(