work
This commit is contained in:
+135
-24
@@ -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}")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user