This commit is contained in:
2026-04-10 16:13:45 -04:00
parent bdf08ce52c
commit 29316e6353
16 changed files with 520 additions and 338 deletions
+34
View File
@@ -0,0 +1,34 @@
use super::*;
pub struct Block {
statements: Vec<Id<Statement>>,
}
impl Parsable for Block {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let mut statements = Vec::new();
if *ctx.expect_peek()? != Token::CloseCurly {
statements.push(ctx.parse()?);
while *ctx.expect_peek()? == Token::Semicolon {
ctx.next();
statements.push(ctx.parse()?);
}
}
Ok(Self { statements })
}
}
impl FmtNode for Block {
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
ctx.indent += 3;
write!(f, "{{")?;
if !self.statements.is_empty() {
writeln!(f)?;
}
for &s in &self.statements {
writeln!(f, "{}{};", " ".repeat(ctx.indent), s.dsp(ctx))?;
}
write!(f, "}}")?;
Ok(())
}
}
+55
View File
@@ -0,0 +1,55 @@
pub use super::*;
pub enum Expr {
Ident(Id<Ident>),
Lit(Id<Lit>),
Negate(Id<Expr>),
Assign(Id<Expr>, Id<Expr>),
}
impl Parsable for Expr {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let e1 = 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)
}
_ => e1,
})
}
}
impl FmtNode for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
match *self {
Expr::Ident(id) => id.fmt(f, ctx),
Expr::Lit(id) => id.fmt(f, ctx),
Expr::Negate(id) => {
write!(f, "-{}", id.dsp(ctx))
}
Expr::Assign(id1, id2) => {
write!(f, "{} = {}", id1.dsp(ctx), id2.dsp(ctx))
}
}
}
}
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}"),
}
}
}
+20
View File
@@ -0,0 +1,20 @@
use super::*;
pub struct Ident {
pub inner: String,
}
impl FmtNode for Ident {
fn fmt(&self, f: &mut std::fmt::Formatter, _: DisplayCtx) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
impl Parsable for Ident {
fn parse(ctx: &mut super::ParseCtx) -> Result<Self, crate::io::CompilerMsg> {
match ctx.expect_next()? {
Token::Ident(ident) => Ok(Self { inner: ident }),
t => ctx.unexpected(t, "an identifier"),
}
}
}
+11
View File
@@ -0,0 +1,11 @@
mod block;
mod expr;
mod ident;
mod statement;
pub use block::*;
pub use expr::*;
pub use ident::*;
pub use statement::*;
use super::{DisplayCtx, FmtNode, Id, Lit, Parsable, ParseCtx, Token};
use crate::io::CompilerMsg;
+41
View File
@@ -0,0 +1,41 @@
pub use super::*;
pub enum Statement {
Let(Id<Ident>, 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()?;
ctx.expect(Token::Equal)?;
Self::Let(name, 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, expr) => {
write!(f, "let {} = {}", name.dsp(ctx), expr.dsp(ctx))
}
Self::Expr(expr) => expr.fmt(f, ctx),
}
}
}