work
This commit is contained in:
+75
-66
@@ -2,57 +2,58 @@ use crate::parser::VecDspT;
|
||||
|
||||
pub use super::*;
|
||||
|
||||
pub type BoxExpr = Box<Parsed<Expr>>;
|
||||
pub struct Expr {
|
||||
span: Span,
|
||||
ty: ExprTy,
|
||||
}
|
||||
|
||||
pub enum Expr {
|
||||
Block(Parsed<Body>),
|
||||
Group(BoxExpr),
|
||||
pub enum ExprTy {
|
||||
Block(Body),
|
||||
Group(Box<Expr>),
|
||||
Ident(Ident),
|
||||
Lit(Lit),
|
||||
Negate(BoxExpr),
|
||||
Call {
|
||||
target: BoxExpr,
|
||||
args: Vec<Parsed<Expr>>,
|
||||
},
|
||||
Assign {
|
||||
target: BoxExpr,
|
||||
val: BoxExpr,
|
||||
},
|
||||
If {
|
||||
cond: BoxExpr,
|
||||
body: BoxExpr,
|
||||
},
|
||||
Loop {
|
||||
body: BoxExpr,
|
||||
},
|
||||
While {
|
||||
cond: BoxExpr,
|
||||
body: BoxExpr,
|
||||
},
|
||||
Fn(Box<Parsed<Func>>),
|
||||
Negate(Box<Expr>),
|
||||
Call { target: Box<Expr>, args: Vec<Expr> },
|
||||
Assign { target: Box<Expr>, val: Box<Expr> },
|
||||
If { cond: Box<Expr>, body: Box<Expr> },
|
||||
Loop { body: Box<Expr> },
|
||||
While { cond: Box<Expr>, body: Box<Expr> },
|
||||
Fn(Box<Func>),
|
||||
}
|
||||
|
||||
impl Node for Expr {
|
||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||
let mut res = Self::unit(ctx)?;
|
||||
while let Some(next) = ctx.peek() {
|
||||
res = match next {
|
||||
let ty = match next {
|
||||
Token::Equal => {
|
||||
let target = ctx.push_adv(res).boxed();
|
||||
let val = ctx.parse_with(Self::unit)?.boxed();
|
||||
Expr::Assign { target, val }
|
||||
ctx.next();
|
||||
let target = Box::new(res);
|
||||
let val = Box::new(ctx.parse_with(Self::unit)?);
|
||||
ExprTy::Assign { target, val }
|
||||
}
|
||||
Token::OpenParen => {
|
||||
let target = ctx.push_adv(res).boxed();
|
||||
ctx.next();
|
||||
let target = Box::new(res);
|
||||
let args = ctx.list(Token::Comma, Token::CloseParen)?;
|
||||
Expr::Call { target, args }
|
||||
ExprTy::Call { target, args }
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
};
|
||||
res = Self {
|
||||
ty,
|
||||
span: ctx.span(),
|
||||
};
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
|
||||
self.ty.fmt(f, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl ExprTy {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Ident(ident) => ident.fmt(f, ctx),
|
||||
@@ -92,60 +93,77 @@ impl Node for Expr {
|
||||
}
|
||||
|
||||
impl Expr {
|
||||
pub fn fmt_body(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
|
||||
match self.ty {
|
||||
ExprTy::Block(_) => self.fmt(f, ctx),
|
||||
_ => write!(f, "=> {}", self.dsp(ctx)),
|
||||
}
|
||||
}
|
||||
|
||||
fn unit(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||
Ok(match ctx.expect_next()? {
|
||||
Token::Dash => Self::Negate(ctx.parse_box()?),
|
||||
Token::Ident(s) => Self::Ident(Ident(s)),
|
||||
Token::Lit(l) => Self::Lit(l),
|
||||
Token::Fn => Self::Fn(ctx.parse_box()?),
|
||||
let ty = match ctx.expect_next()? {
|
||||
Token::Dash => ExprTy::Negate(ctx.parse_box()?),
|
||||
Token::Ident(s) => ExprTy::Ident(ctx.ident(s)),
|
||||
Token::Lit(l) => ExprTy::Lit(ctx.lit(l)),
|
||||
Token::Fn => ExprTy::Fn(ctx.parse_box()?),
|
||||
Token::If => {
|
||||
let cond = ctx.parse_box()?;
|
||||
let body = Self::body(ctx)?.boxed();
|
||||
Self::If { cond, body }
|
||||
let body = Box::new(Self::body(ctx)?);
|
||||
ExprTy::If { cond, body }
|
||||
}
|
||||
Token::While => {
|
||||
let cond = ctx.parse_box()?;
|
||||
let body = Self::body(ctx)?.boxed();
|
||||
Self::While { cond, body }
|
||||
let body = Box::new(Self::body(ctx)?);
|
||||
ExprTy::While { cond, body }
|
||||
}
|
||||
Token::Loop => {
|
||||
let body = ctx.parse_box()?;
|
||||
Self::Loop { body }
|
||||
ExprTy::Loop { body }
|
||||
}
|
||||
Token::OpenParen => {
|
||||
if ctx.next_if(Token::CloseParen) {
|
||||
Self::Lit(Lit::Unit)
|
||||
ExprTy::Lit(Lit {
|
||||
ty: LitTy::Unit,
|
||||
span: ctx.span(),
|
||||
})
|
||||
} else {
|
||||
let inner = ctx.parse_box()?;
|
||||
ctx.expect(Token::CloseParen)?;
|
||||
Self::Group(inner)
|
||||
ExprTy::Group(inner)
|
||||
}
|
||||
}
|
||||
Token::OpenCurly => {
|
||||
let body = ctx.parse()?;
|
||||
ctx.expect(Token::CloseCurly)?;
|
||||
Self::Block(body)
|
||||
ExprTy::Block(body)
|
||||
}
|
||||
other => return ctx.unexpected(&other, "an expression"),
|
||||
};
|
||||
Ok(Self {
|
||||
ty,
|
||||
span: ctx.span(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_group(&self) -> bool {
|
||||
matches!(self, Expr::Group(_))
|
||||
matches!(self.ty, ExprTy::Group(_))
|
||||
}
|
||||
|
||||
pub fn is_block(&self) -> bool {
|
||||
matches!(self, Expr::Block(_))
|
||||
matches!(self.ty, ExprTy::Block(_))
|
||||
}
|
||||
|
||||
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))
|
||||
Ok(Expr {
|
||||
ty: ExprTy::Block(id),
|
||||
span: ctx.span(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn body(ctx: &mut ParseCtx) -> Result<Parsed<Expr>, CompilerMsg> {
|
||||
pub fn body(ctx: &mut ParseCtx) -> Result<Expr, CompilerMsg> {
|
||||
if ctx.next_if(Token::DoubleArrow) {
|
||||
ctx.parse()
|
||||
} else {
|
||||
@@ -154,24 +172,15 @@ impl Expr {
|
||||
}
|
||||
|
||||
pub fn ends_with_block(&self) -> bool {
|
||||
match self {
|
||||
Expr::Block(..) => true,
|
||||
Expr::Loop { body }
|
||||
| Expr::While { body, .. }
|
||||
| Expr::If { body, .. }
|
||||
| Expr::Negate(body)
|
||||
| Expr::Assign { val: body, .. } => body.ends_with_block(),
|
||||
Expr::Fn(f) => f.ends_with_block(),
|
||||
match &self.ty {
|
||||
ExprTy::Block(..) => true,
|
||||
ExprTy::Loop { body }
|
||||
| ExprTy::While { body, .. }
|
||||
| ExprTy::If { body, .. }
|
||||
| ExprTy::Negate(body)
|
||||
| ExprTy::Assign { val: body, .. } => body.ends_with_block(),
|
||||
ExprTy::Fn(f) => f.ends_with_block(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsed<Expr> {
|
||||
pub fn fmt_body(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
|
||||
match &self.node {
|
||||
Expr::Block(_) => self.node.fmt(f, ctx),
|
||||
_ => write!(f, "=> {}", self.dsp(ctx)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user