gaming
This commit is contained in:
+27
-12
@@ -37,22 +37,22 @@ pub enum Expr {
|
||||
|
||||
impl Parsable for Expr {
|
||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
||||
let mut res = Self::parse_unit(ctx)?;
|
||||
let mut res = Self::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)?;
|
||||
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) {
|
||||
if !ctx.next_if(Token::Equal) {
|
||||
ty = Some(ctx.parse()?);
|
||||
ctx.expect(Token::Equal)?;
|
||||
}
|
||||
let val = Self::push_unit(ctx)?;
|
||||
let val = ctx.parse_with(Self::unit)?;
|
||||
Expr::Define { target, ty, val }
|
||||
}
|
||||
Token::OpenParen => {
|
||||
@@ -68,11 +68,7 @@ impl Parsable for Expr {
|
||||
}
|
||||
|
||||
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> {
|
||||
fn 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)),
|
||||
@@ -80,12 +76,12 @@ impl Expr {
|
||||
Token::Fn => Self::Fn(ctx.parse()?),
|
||||
Token::If => {
|
||||
let cond = ctx.parse()?;
|
||||
let body = ctx.parse()?;
|
||||
let body = cond_body(cond, ctx)?;
|
||||
Self::If { cond, body }
|
||||
}
|
||||
Token::While => {
|
||||
let cond = ctx.parse()?;
|
||||
let body = ctx.parse()?;
|
||||
let body = cond_body(cond, ctx)?;
|
||||
Self::While { cond, body }
|
||||
}
|
||||
Token::Loop => {
|
||||
@@ -93,7 +89,7 @@ impl Expr {
|
||||
Self::Loop { body }
|
||||
}
|
||||
Token::OpenParen => {
|
||||
if ctx.next_if(&Token::CloseParen) {
|
||||
if ctx.next_if(Token::CloseParen) {
|
||||
Self::Lit(ctx.push(Lit::Unit))
|
||||
} else {
|
||||
let inner = ctx.parse()?;
|
||||
@@ -109,6 +105,25 @@ impl Expr {
|
||||
other => return ctx.unexpected(&other, "an expression"),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_group(&self) -> bool {
|
||||
matches!(self, Expr::Group(_))
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
fn cond_body(cond: Id<Expr>, ctx: &mut ParseCtx) -> Result<Id<Expr>, CompilerMsg> {
|
||||
if ctx[cond].is_group() {
|
||||
ctx.parse()
|
||||
} else {
|
||||
ctx.parse_with(Expr::block)
|
||||
}
|
||||
}
|
||||
|
||||
impl FmtNode for Expr {
|
||||
|
||||
Reference in New Issue
Block a user