This commit is contained in:
2026-04-11 15:21:03 -04:00
parent 229b026573
commit 2582e8c87e
15 changed files with 301 additions and 199 deletions
+52
View File
@@ -0,0 +1,52 @@
use super::*;
pub struct Body {
pub exprs: Vec<Id<Expr>>,
pub final_semicolon: bool,
}
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,
});
}
_ => (),
}
exprs.push(ctx.parse()?);
while ctx.next_if(&Token::Semicolon) {
final_semicolon = true;
if ctx.peek().is_none_or(|t| *t == Token::CloseCurly) {
break;
}
exprs.push(ctx.parse()?);
final_semicolon = false;
}
Ok(Self {
exprs,
final_semicolon,
})
}
}
impl FmtNode for Body {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
// surely there's a better way to do this
if let Some((last, rest)) = self.exprs.split_last() {
for &i in rest {
writeln!(f, "{}{};", " ".repeat(ctx.indent), i.dsp(ctx))?;
}
if self.final_semicolon {
writeln!(f, "{}{};", " ".repeat(ctx.indent), last.dsp(ctx))?;
} else {
writeln!(f, "{}{}", " ".repeat(ctx.indent), last.dsp(ctx))?;
}
}
Ok(())
}
}
+135 -24
View File
@@ -1,44 +1,159 @@
use crate::parser::VecDspT;
pub use super::*;
pub enum Expr {
Block(Id<Body>),
Group(Id<Expr>),
Ident(Id<Ident>),
Lit(Id<Lit>),
Negate(Id<Expr>),
Assign(Id<Expr>, Id<Expr>),
Call {
target: Id<Expr>,
args: Vec<Id<Expr>>,
},
Assign {
target: Id<Expr>,
val: Id<Expr>,
},
Define {
target: Id<Expr>,
ty: Option<Id<Type>>,
val: Id<Expr>,
},
If {
cond: Id<Expr>,
body: Id<Expr>,
},
Loop {
body: Id<Expr>,
},
While {
cond: Id<Expr>,
body: Id<Expr>,
},
Fn(Id<Func>),
}
impl Parsable for Expr {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let e1 = match ctx.expect_next()? {
let mut res = Self::parse_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)?;
Expr::Assign { target, val }
}
Token::Colon => {
let target = ctx.push_adv(res);
let mut ty = None;
if !ctx.next_if(&Token::Equal) {
ty = Some(ctx.parse()?);
ctx.expect(Token::Equal)?;
}
let val = Self::push_unit(ctx)?;
Expr::Define { target, ty, val }
}
Token::OpenParen => {
let target = ctx.push_adv(res);
let args = ctx.list(Token::Comma, Token::CloseParen)?;
Expr::Call { target, args }
}
_ => break,
}
}
Ok(res)
}
}
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> {
Ok(match ctx.expect_next()? {
Token::Dash => Self::Negate(ctx.parse()?),
Token::Ident(s) => Self::Ident(ctx.ident(s)),
Token::Lit(l) => Self::Lit(ctx.lit(l)),
other => return ctx.unexpected(&other, "an expression"),
};
let Some(next) = ctx.peek() else {
return Ok(e1);
};
Ok(match next {
Token::Equal => {
let e1 = ctx.push_adv(e1);
let e2: Id<Expr> = ctx.parse()?;
Expr::Assign(e1, e2)
Token::Fn => Self::Fn(ctx.parse()?),
Token::If => {
let cond = ctx.parse()?;
let body = ctx.parse()?;
Self::If { cond, body }
}
_ => e1,
Token::While => {
let cond = ctx.parse()?;
let body = ctx.parse()?;
Self::While { cond, body }
}
Token::Loop => {
let body = ctx.parse()?;
Self::Loop { body }
}
Token::OpenParen => {
if ctx.next_if(&Token::CloseParen) {
Self::Lit(ctx.push(Lit::Unit))
} else {
let inner = ctx.parse()?;
ctx.expect(Token::CloseParen)?;
Self::Group(inner)
}
}
Token::OpenCurly => {
let body = ctx.parse()?;
ctx.expect(Token::CloseCurly)?;
Self::Block(body)
}
other => return ctx.unexpected(&other, "an expression"),
})
}
}
impl FmtNode for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
match *self {
Expr::Ident(id) => id.fmt(f, ctx),
Expr::Lit(id) => id.fmt(f, ctx),
Expr::Negate(id) => {
Self::Ident(id) => id.fmt(f, ctx),
Self::Group(id) => write!(f, "({})", id.dsp(ctx)),
Self::Fn(id) => id.fmt(f, ctx),
Self::Lit(id) => id.fmt(f, ctx),
Self::Negate(id) => {
write!(f, "-{}", id.dsp(ctx))
}
Expr::Assign(id1, id2) => {
write!(f, "{} = {}", id1.dsp(ctx), id2.dsp(ctx))
Self::Call { target, ref args } => {
write!(f, "{}({})", target.dsp(ctx), args.dsp(ctx))
}
Self::Assign { target, val } => {
write!(f, "{} = {}", target.dsp(ctx), val.dsp(ctx))
}
Self::Define { target, ty, val } => {
target.fmt(f, ctx)?;
if let Some(ty) = ty {
write!(f, ": {} ", ty.dsp(ctx))?;
} else {
write!(f, " :")?;
}
write!(f, "= {}", val.dsp(ctx))
}
Self::If { cond, body } => {
write!(f, "if {} {}", cond.dsp(ctx), body.dsp(ctx))
}
Self::While { cond, body } => {
write!(f, "while {} {}", cond.dsp(ctx), body.dsp(ctx))
}
Self::Loop { body } => {
write!(f, "loop {}", body.dsp(ctx))
}
Self::Block(body) => {
write!(f, "{{")?;
if !ctx.nodes[body].exprs.is_empty() {
writeln!(f)?;
ctx.indent += 3;
body.fmt(f, ctx)?;
}
write!(f, "}}")?;
Ok(())
}
}
}
@@ -46,10 +161,6 @@ impl FmtNode for Expr {
impl FmtNode for Lit {
fn fmt(&self, f: &mut std::fmt::Formatter, _: DisplayCtx) -> std::fmt::Result {
match self {
Lit::Number(v) => write!(f, "{v}"),
Lit::Bool(v) => write!(f, "{v}"),
Lit::String(v) => write!(f, "{v}"),
}
write!(f, "{self}")
}
}
+29
View File
@@ -0,0 +1,29 @@
use super::*;
pub struct Func {
args: Vec<Id<Ident>>,
body: Id<Expr>,
}
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 })
}
}
impl FmtNode for Func {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
write!(f, "(")?;
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))?;
Ok(())
}
}
-24
View File
@@ -1,24 +0,0 @@
use super::*;
pub enum Item {
Module(Id<Module>),
Statement(Id<Statement>),
}
impl Parsable for Item {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_peek()? {
Token::Fn => Self::Module(ctx.parse()?),
_ => Self::Statement(ctx.parse()?),
})
}
}
impl FmtNode for Item {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
match self {
Item::Module(id) => write!(f, "{}", id.dsp(ctx)),
Item::Statement(id) => write!(f, "{}", id.dsp(ctx)),
}
}
}
+6 -9
View File
@@ -1,14 +1,12 @@
mod body;
mod expr;
mod func;
mod ident;
mod item;
mod module;
mod statement;
mod ty;
pub use body::*;
pub use expr::*;
pub use func::*;
pub use ident::*;
pub use item::*;
pub use module::*;
pub use statement::*;
pub use ty::*;
use super::{DisplayCtx, FmtNode, Id, Lit, Node, NodeVec, Parsable, ParseCtx, Token};
@@ -17,11 +15,10 @@ use crate::io::CompilerMsg;
def_nodes! {
exprs: Expr,
idents: Ident,
statements: Statement,
blocks: Module,
blocks: Body,
lits: Lit,
types: Type,
items: Item,
funcs: Func,
}
macro_rules! def_nodes {
-35
View File
@@ -1,35 +0,0 @@
use super::*;
pub struct Module {
items: Vec<Id<Item>>,
}
impl Parsable for Module {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let mut items = Vec::new();
if ctx.peek().is_none() {
return Ok(Self { items });
}
items.push(ctx.parse()?);
while *ctx.expect_peek()? == Token::Semicolon {
ctx.next();
items.push(ctx.parse()?);
}
Ok(Self { items })
}
}
impl FmtNode for Module {
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
ctx.indent += 3;
write!(f, "{{")?;
if !self.items.is_empty() {
writeln!(f)?;
}
for &i in &self.items {
writeln!(f, "{}{};", " ".repeat(ctx.indent), i.dsp(ctx))?;
}
write!(f, "}}")?;
Ok(())
}
}
-60
View File
@@ -1,60 +0,0 @@
pub use super::*;
pub enum Statement {
Let {
name: Id<Ident>,
ty: Option<Id<Type>>,
val: Id<Expr>,
},
If {
cond: Id<Expr>,
body: Id<Expr>,
},
Expr(Id<Expr>),
}
impl Parsable for Statement {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_peek()? {
Token::Let => {
ctx.next();
let name = ctx.parse()?;
let mut ty = None;
if ctx.next_if(Token::Colon) {
ty = Some(ctx.parse()?);
}
ctx.expect(Token::Equal)?;
Self::Let {
name,
ty,
val: ctx.parse()?,
}
}
Token::If => {
ctx.next();
let cond = ctx.parse()?;
let body = ctx.parse()?;
Self::If { cond, body }
}
_ => Self::Expr(ctx.parse()?),
})
}
}
impl FmtNode for Statement {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
match *self {
Self::If { cond, body } => {
write!(f, "if {} {}", cond.dsp(ctx), body.dsp(ctx))
}
Self::Let { name, ty, val } => {
write!(f, "let {}", name.dsp(ctx))?;
if let Some(ty) = ty {
write!(f, ": {}", ty.dsp(ctx))?;
}
write!(f, " = {}", val.dsp(ctx))
}
Self::Expr(expr) => expr.fmt(f, ctx),
}
}
}