work
This commit is contained in:
+27
-37
@@ -16,11 +16,6 @@ pub enum Expr {
|
||||
target: Id<Expr>,
|
||||
val: Id<Expr>,
|
||||
},
|
||||
Define {
|
||||
target: Id<Expr>,
|
||||
ty: Option<Id<Type>>,
|
||||
val: Id<Expr>,
|
||||
},
|
||||
If {
|
||||
cond: Id<Expr>,
|
||||
body: Id<Expr>,
|
||||
@@ -45,16 +40,6 @@ impl Parsable for Expr {
|
||||
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) {
|
||||
ty = Some(ctx.parse()?);
|
||||
ctx.expect(Token::Equal)?;
|
||||
}
|
||||
let val = ctx.parse_with(Self::unit)?;
|
||||
Expr::Define { target, ty, val }
|
||||
}
|
||||
Token::OpenParen => {
|
||||
let target = ctx.push_adv(res);
|
||||
let args = ctx.list(Token::Comma, Token::CloseParen)?;
|
||||
@@ -76,12 +61,12 @@ impl Expr {
|
||||
Token::Fn => Self::Fn(ctx.parse()?),
|
||||
Token::If => {
|
||||
let cond = ctx.parse()?;
|
||||
let body = cond_body(cond, ctx)?;
|
||||
let body = cond_body(ctx)?;
|
||||
Self::If { cond, body }
|
||||
}
|
||||
Token::While => {
|
||||
let cond = ctx.parse()?;
|
||||
let body = cond_body(cond, ctx)?;
|
||||
let body = cond_body(ctx)?;
|
||||
Self::While { cond, body }
|
||||
}
|
||||
Token::Loop => {
|
||||
@@ -110,6 +95,10 @@ impl Expr {
|
||||
matches!(self, Expr::Group(_))
|
||||
}
|
||||
|
||||
pub fn is_block(&self) -> bool {
|
||||
matches!(self, Expr::Block(_))
|
||||
}
|
||||
|
||||
pub fn block(ctx: &mut ParseCtx) -> Result<Expr, CompilerMsg> {
|
||||
ctx.expect(Token::OpenCurly)?;
|
||||
let id = ctx.parse()?;
|
||||
@@ -118,16 +107,32 @@ impl Expr {
|
||||
}
|
||||
}
|
||||
|
||||
fn cond_body(cond: Id<Expr>, ctx: &mut ParseCtx) -> Result<Id<Expr>, CompilerMsg> {
|
||||
if ctx[cond].is_group() {
|
||||
fn cond_body(ctx: &mut ParseCtx) -> Result<Id<Expr>, CompilerMsg> {
|
||||
if ctx.next_if(Token::Do) {
|
||||
ctx.parse()
|
||||
} else {
|
||||
ctx.parse_with(Expr::block)
|
||||
}
|
||||
}
|
||||
|
||||
impl Id<Expr> {
|
||||
pub fn ends_with_block(&self, nodes: &Nodes) -> bool {
|
||||
match nodes[self] {
|
||||
Expr::Block(..) => true,
|
||||
Expr::Loop { body }
|
||||
| Expr::While { body, .. }
|
||||
| Expr::If { body, .. }
|
||||
| Expr::Negate(body)
|
||||
| Expr::Assign { val: body, .. } => body.ends_with_block(nodes),
|
||||
Expr::Fn(f) => f.ends_with_block(nodes),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FmtNode for Expr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
|
||||
let do_ = |id: Id<Expr>| if ctx.nodes[id].is_block() { "" } else { "do " };
|
||||
match *self {
|
||||
Self::Ident(id) => id.fmt(f, ctx),
|
||||
Self::Group(id) => write!(f, "({})", id.dsp(ctx)),
|
||||
@@ -142,27 +147,18 @@ impl FmtNode for Expr {
|
||||
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))
|
||||
write!(f, "if {} {}{}", cond.dsp(ctx), do_(body), body.dsp(ctx))
|
||||
}
|
||||
Self::While { cond, body } => {
|
||||
write!(f, "while {} {}", cond.dsp(ctx), body.dsp(ctx))
|
||||
write!(f, "while {} {}{}", cond.dsp(ctx), do_(body), body.dsp(ctx))
|
||||
}
|
||||
Self::Loop { body } => {
|
||||
write!(f, "loop {}", body.dsp(ctx))
|
||||
}
|
||||
Self::Block(body) => {
|
||||
write!(f, "{{")?;
|
||||
if !ctx.nodes[body].exprs.is_empty() {
|
||||
if !ctx.nodes[body].items.is_empty() {
|
||||
writeln!(f)?;
|
||||
ctx.indent += 3;
|
||||
body.fmt(f, ctx)?;
|
||||
@@ -173,9 +169,3 @@ impl FmtNode for Expr {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FmtNode for Lit {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, _: DisplayCtx) -> std::fmt::Result {
|
||||
write!(f, "{self}")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user