This commit is contained in:
2026-04-12 17:26:39 -04:00
parent 2582e8c87e
commit f702f47714
9 changed files with 121 additions and 49 deletions
+10 -16
View File
@@ -8,25 +8,19 @@ pub struct Body {
impl Parsable for Body {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let mut exprs = Vec::new();
let mut final_semicolon = false;
match ctx.peek() {
None | Some(Token::CloseCurly) => {
return Ok(Self {
exprs,
final_semicolon,
});
}
_ => (),
fn at_end(ctx: &mut ParseCtx) -> bool {
ctx.peek().is_none_or(|t| *t == Token::CloseCurly)
}
exprs.push(ctx.parse()?);
while ctx.next_if(&Token::Semicolon) {
final_semicolon = true;
if ctx.peek().is_none_or(|t| *t == Token::CloseCurly) {
break;
let final_semicolon = loop {
if at_end(ctx) {
break true;
}
exprs.push(ctx.parse()?);
final_semicolon = false;
}
if at_end(ctx) {
break false;
}
ctx.expect(Token::Semicolon)?;
};
Ok(Self {
exprs,
final_semicolon,
+27 -12
View File
@@ -37,22 +37,22 @@ pub enum Expr {
impl Parsable for Expr {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let mut res = Self::parse_unit(ctx)?;
let mut res = Self::unit(ctx)?;
while let Some(next) = ctx.peek() {
res = match next {
Token::Equal => {
let target = ctx.push_adv(res);
let val = Self::push_unit(ctx)?;
let val = ctx.parse_with(Self::unit)?;
Expr::Assign { target, val }
}
Token::Colon => {
let target = ctx.push_adv(res);
let mut ty = None;
if !ctx.next_if(&Token::Equal) {
if !ctx.next_if(Token::Equal) {
ty = Some(ctx.parse()?);
ctx.expect(Token::Equal)?;
}
let val = Self::push_unit(ctx)?;
let val = ctx.parse_with(Self::unit)?;
Expr::Define { target, ty, val }
}
Token::OpenParen => {
@@ -68,11 +68,7 @@ impl Parsable for Expr {
}
impl Expr {
fn push_unit(ctx: &mut ParseCtx) -> Result<Id<Self>, CompilerMsg> {
let res = Self::parse_unit(ctx)?;
Ok(ctx.push(res))
}
fn parse_unit(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
fn unit(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_next()? {
Token::Dash => Self::Negate(ctx.parse()?),
Token::Ident(s) => Self::Ident(ctx.ident(s)),
@@ -80,12 +76,12 @@ impl Expr {
Token::Fn => Self::Fn(ctx.parse()?),
Token::If => {
let cond = ctx.parse()?;
let body = ctx.parse()?;
let body = cond_body(cond, ctx)?;
Self::If { cond, body }
}
Token::While => {
let cond = ctx.parse()?;
let body = ctx.parse()?;
let body = cond_body(cond, ctx)?;
Self::While { cond, body }
}
Token::Loop => {
@@ -93,7 +89,7 @@ impl Expr {
Self::Loop { body }
}
Token::OpenParen => {
if ctx.next_if(&Token::CloseParen) {
if ctx.next_if(Token::CloseParen) {
Self::Lit(ctx.push(Lit::Unit))
} else {
let inner = ctx.parse()?;
@@ -109,6 +105,25 @@ impl Expr {
other => return ctx.unexpected(&other, "an expression"),
})
}
pub fn is_group(&self) -> bool {
matches!(self, Expr::Group(_))
}
pub fn block(ctx: &mut ParseCtx) -> Result<Expr, CompilerMsg> {
ctx.expect(Token::OpenCurly)?;
let id = ctx.parse()?;
ctx.expect(Token::CloseCurly)?;
Ok(Expr::Block(id))
}
}
fn cond_body(cond: Id<Expr>, ctx: &mut ParseCtx) -> Result<Id<Expr>, CompilerMsg> {
if ctx[cond].is_group() {
ctx.parse()
} else {
ctx.parse_with(Expr::block)
}
}
impl FmtNode for Expr {
+18 -5
View File
@@ -1,7 +1,8 @@
use super::*;
pub struct Func {
args: Vec<Id<Ident>>,
args: Vec<Id<Param>>,
ret: Option<Id<Type>>,
body: Id<Expr>,
}
@@ -9,21 +10,33 @@ impl Parsable for Func {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
ctx.expect(Token::OpenParen)?;
let args = ctx.list(Token::Comma, Token::CloseParen)?;
let body = ctx.parse()?;
Ok(Self { args, body })
let mut ret = None;
if ctx.next_if(Token::Arrow) {
ret = Some(ctx.parse()?);
}
let body = if ret.is_some() {
ctx.parse_with(Expr::block)
} else {
ctx.parse()
}?;
Ok(Self { args, ret, body })
}
}
impl FmtNode for Func {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
write!(f, "(")?;
write!(f, "fn(")?;
if let Some((last, rest)) = self.args.split_last() {
for arg in rest {
write!(f, "{}, ", arg.dsp(ctx))?;
}
write!(f, "{}", last.dsp(ctx))?;
}
write!(f, ") {}", self.body.dsp(ctx))?;
write!(f, ") ")?;
if let Some(ret) = self.ret {
write!(f, "-> {} ", ret.dsp(ctx))?;
}
self.body.fmt(f, ctx)?;
Ok(())
}
}
+3
View File
@@ -2,11 +2,13 @@ mod body;
mod expr;
mod func;
mod ident;
mod param;
mod ty;
pub use body::*;
pub use expr::*;
pub use func::*;
pub use ident::*;
pub use param::*;
pub use ty::*;
use super::{DisplayCtx, FmtNode, Id, Lit, Node, NodeVec, Parsable, ParseCtx, Token};
@@ -19,6 +21,7 @@ def_nodes! {
lits: Lit,
types: Type,
funcs: Func,
params: Param,
}
macro_rules! def_nodes {
+27
View File
@@ -0,0 +1,27 @@
use super::*;
pub struct Param {
name: Id<Ident>,
ty: Option<Id<Type>>,
}
impl Parsable for Param {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let name = ctx.parse()?;
let mut ty = None;
if ctx.next_if(Token::Colon) {
ty = Some(ctx.parse()?);
}
Ok(Self { name, ty })
}
}
impl FmtNode for Param {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
self.name.fmt(f, ctx)?;
if let Some(ty) = self.ty {
write!(f, ": {}", ty.dsp(ctx))?;
}
Ok(())
}
}