Skip to content

Commit

Permalink
Add if-else data (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonidas1712 committed Apr 11, 2024
1 parent 8dc573c commit a3b37ae
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions compiler/oxidate/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl Compiler {
Expr::BlockExpr(blk) => {
Compiler::compile_block(blk, arr)?;
}
_ => todo!(),
}

Ok(())
Expand Down
39 changes: 36 additions & 3 deletions src/parser/src/if_else.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::Decl;
// use crate::Decl::*;
use crate::Expr;
use crate::IfElseData;
use crate::ParseError;
use crate::Parser;
// use crate::{BinOpType, UnOpType};
Expand All @@ -15,11 +16,33 @@ impl<'inp> Parser<'inp> {
self.consume_token_type(Token::OpenParen, "Expected open parenthesis")?;
dbg!("OK", &self.prev_tok);

let cond = self.parse_expr(min_bp);
let cond = self.parse_expr(min_bp)?.to_expr()?;
dbg!(&cond); // got cond

dbg!("peek after:", &self.lexer.peek()); // OpenBrace
Ok(Decl::ExprStmt(Expr::Bool(true)))

// go past OpenBrace, put in prev_tok
self.consume_token_type(
Token::OpenBrace,
&format!("Expected {} for if block", Token::OpenBrace),
)?;

let if_blk = self.parse_blk(min_bp)?.to_block()?;

dbg!("after parse_blk", &if_blk);

dbg!("peek after parse_blk", &self.lexer.peek());

let stmt = IfElseData {
cond,
if_blk,
else_blk: None,
};

let expr = Expr::IfElseExpr(Box::new(stmt));

// parse else blk if avail
Ok(Decl::ExprStmt(expr))
}
}

Expand All @@ -31,9 +54,19 @@ mod tests {
fn parse_if_basic() {
let t = r"
if (true) {
30;
40;
}
";
// test_parse(t, "");

// let t = r"
// if (true) {
// 30;
// } else {
// 40;
// }
// ";
// test_parse(t, "");
}
}
2 changes: 1 addition & 1 deletion src/parser/src/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'inp> Parser<'inp> {
// dbg!("prev_tok:", &self.prev_tok);

let expr = self.parse_decl()?;
// dbg!("Got expr:", &expr);
dbg!("Got expr:", &expr);
// dbg!("Peek:", &self.lexer.peek());

// end of block: lexer empty OR curly brace (TODO add curly later)
Expand Down
30 changes: 30 additions & 0 deletions src/parser/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub enum Expr {
UnOpExpr(UnOpType, Box<Expr>),
BinOpExpr(BinOpType, Box<Expr>, Box<Expr>),
BlockExpr(BlockSeq), // expr can be a block
IfElseExpr(Box<IfElseData>),
}

impl Display for Expr {
Expand All @@ -81,6 +82,8 @@ impl Display for Expr {
}
Expr::Symbol(val) => val.to_string(),
Expr::BlockExpr(seq) => format!("{{ {} }}", seq),
// Expr::BlockExpr(seq) => seq.to_string(),
Expr::IfElseExpr(expr) => expr.to_string(),
};

write!(f, "{}", string)
Expand Down Expand Up @@ -118,6 +121,24 @@ impl Display for AssignStmt {
}
}

#[derive(Debug, Clone)]
pub struct IfElseData {
pub cond: Expr,
pub if_blk: BlockSeq,
pub else_blk: Option<BlockSeq>,
}

impl Display for IfElseData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut s = format!("if {} {}", self.cond, self.if_blk);
if let Some(ref else_blk) = self.else_blk {
s.push(' ');
s.push_str(&format!("else {}", else_blk));
}

write!(f, "{}", s)
}
}
// Later: LetStmt, IfStmt, FnDef, etc.
#[derive(Debug, Clone)]
pub enum Decl {
Expand All @@ -140,6 +161,15 @@ impl Decl {
Self::ExprStmt(expr) => Ok(expr.clone()),
}
}

pub fn to_block(&self) -> Result<BlockSeq, ParseError> {
if let Self::ExprStmt(Expr::BlockExpr(seq)) = &self {
return Ok(seq.clone());
}

let e = format!("Expected block but got '{}'", self);
Err(ParseError::new(&e))
}
}

impl Display for Decl {
Expand Down
1 change: 1 addition & 0 deletions src/types/src/type_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ impl<'prog> TypeChecker<'prog> {
return self.check_binop(op, lhs, rhs);
}
Expr::BlockExpr(blk) => self.check_block(blk)?,
_ => todo!(),
};

if local_errs.is_ok() {
Expand Down

0 comments on commit a3b37ae

Please sign in to comment.