Skip to content

Commit

Permalink
expression statement
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 20, 2023
1 parent 3d66271 commit 3419ec3
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 90 deletions.
8 changes: 5 additions & 3 deletions components/lox-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use lox_ir::{

#[salsa::tracked]
pub fn compile_file(db: &dyn crate::Db, input_file: InputFile) -> Chunk {
let exprs = lox_parse::parse_file(db, input_file);
let stmts = lox_parse::parse_file(db, input_file);
let mut chunk = Chunk::default();
for expr in exprs {
compile_expr(db, expr, &mut chunk);
for stmt in stmts {
match stmt {
syntax::Stmt::Expr(expr) => compile_expr(db, expr, &mut chunk),
}
}
chunk
}
Expand Down
9 changes: 9 additions & 0 deletions components/lox-ir/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ impl<'db> salsa::DebugWithDb<dyn crate::Db + 'db> for Expr {
}


#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Stmt {
// expression statement, like `1 + 2;`
Expr(Expr),
}

impl salsa::DebugWithDb<dyn crate::Db> for Stmt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &dyn crate::Db, _include_all_fields: bool) -> std::fmt::Result {
match self {
Stmt::Expr(expr) => f.debug_struct("Expr").field("expr", &expr.debug(db)).finish(),
}
}
}
6 changes: 3 additions & 3 deletions components/lox-parse/src/file_parser.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::parser::Parser;

use lox_ir::{input_file::InputFile, syntax::Expr};
use lox_ir::{input_file::InputFile, syntax::Stmt};

#[salsa::tracked(return_ref)]
pub fn parse_file(db: &dyn crate::Db, input_file: InputFile) -> Vec<Expr> {
pub fn parse_file(db: &dyn crate::Db, input_file: InputFile) -> Vec<Stmt> {
let token_tree = lox_lex::lex_file(db, input_file);
let mut parser = Parser::new(db, token_tree);
parser.parse_exprs()
parser.parse()
}
22 changes: 11 additions & 11 deletions components/lox-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ impl<'me> Parser<'me> {
}
}

pub(crate) fn parse_exprs(&mut self) -> Vec<Expr> {
let mut exprs = vec![];
while let Some(expr) = self.parse_expr() {
exprs.push(expr);
pub(crate) fn parse(&mut self) -> Vec<Stmt> {
let mut stmts = vec![];
while let Some(stmt) = self.expr_stmt() {
stmts.push(stmt);
}
if self.tokens.peek().is_some() {
let span = self.tokens.peek_span();
self.error(span, "extra tokens after expression")
self.error(span, "extra tokens after statement")
.emit(self.db);
}
exprs
stmts
}

// fn expr_stmt(&mut self) -> Option<Stmt> {
// let expr = self.parse_expr()?;
// self.eat(Token::Semicolon);
// Some(Stmt::Expr(expr))
// }
fn expr_stmt(&mut self) -> Option<Stmt> {
let expr = self.parse_expr()?;
self.eat(Token::Semicolon);
Some(Stmt::Expr(expr))
}

// expression → equality ;
// equality → comparison ( ( "!=" | "==" ) comparison )* ;
Expand Down
62 changes: 35 additions & 27 deletions lox_tests/arithmetic.syntax
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
BinaryOp {
left: NumberLiteral(1),
op: Plus,
right: NumberLiteral(2),
Expr {
expr: BinaryOp {
left: NumberLiteral(1),
op: Plus,
right: NumberLiteral(2),
},
}
BinaryOp {
left: NumberLiteral(1),
op: Plus,
right: BinaryOp {
left: NumberLiteral(2),
op: Star,
right: NumberLiteral(3),
Expr {
expr: BinaryOp {
left: NumberLiteral(1),
op: Plus,
right: BinaryOp {
left: NumberLiteral(2),
op: Star,
right: NumberLiteral(3),
},
},
}
BinaryOp {
left: NumberLiteral(1),
op: Minus,
right: BinaryOp {
left: NumberLiteral(2),
op: Slash,
right: NumberLiteral(3),
Expr {
expr: BinaryOp {
left: NumberLiteral(1),
op: Minus,
right: BinaryOp {
left: NumberLiteral(2),
op: Slash,
right: NumberLiteral(3),
},
},
}
BinaryOp {
left: NumberLiteral(1),
op: Plus,
right: BinaryOp {
left: NumberLiteral(2),
op: Star,
right: UnaryOp {
op: Minus,
expr: NumberLiteral(3),
Expr {
expr: BinaryOp {
left: NumberLiteral(1),
op: Plus,
right: BinaryOp {
left: NumberLiteral(2),
op: Star,
right: UnaryOp {
op: Minus,
expr: NumberLiteral(3),
},
},
},
}
20 changes: 14 additions & 6 deletions lox_tests/boolean.syntax
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
BooleanLiteral(true)
BooleanLiteral(false)
UnaryOp {
op: Bang,
Expr {
expr: BooleanLiteral(true),
}
UnaryOp {
op: Bang,
Expr {
expr: BooleanLiteral(false),
}
Expr {
expr: UnaryOp {
op: Bang,
expr: BooleanLiteral(true),
},
}
Expr {
expr: UnaryOp {
op: Bang,
expr: BooleanLiteral(false),
},
}
40 changes: 24 additions & 16 deletions lox_tests/comparison.syntax
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
BinaryOp {
left: NumberLiteral(1),
op: Less,
right: NumberLiteral(2),
Expr {
expr: BinaryOp {
left: NumberLiteral(1),
op: Less,
right: NumberLiteral(2),
},
}
BinaryOp {
left: NumberLiteral(1),
op: LessEqual,
right: NumberLiteral(1),
Expr {
expr: BinaryOp {
left: NumberLiteral(1),
op: LessEqual,
right: NumberLiteral(1),
},
}
BinaryOp {
left: NumberLiteral(1),
op: Greater,
right: NumberLiteral(2),
Expr {
expr: BinaryOp {
left: NumberLiteral(1),
op: Greater,
right: NumberLiteral(2),
},
}
BinaryOp {
left: NumberLiteral(1),
op: GreaterEqual,
right: NumberLiteral(1),
Expr {
expr: BinaryOp {
left: NumberLiteral(1),
op: GreaterEqual,
right: NumberLiteral(1),
},
}
20 changes: 12 additions & 8 deletions lox_tests/equality.syntax
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
BinaryOp {
left: NumberLiteral(1),
op: EqualEqual,
right: NumberLiteral(2),
Expr {
expr: BinaryOp {
left: NumberLiteral(1),
op: EqualEqual,
right: NumberLiteral(2),
},
}
BinaryOp {
left: NumberLiteral(1),
op: NotEqual,
right: NumberLiteral(2),
Expr {
expr: BinaryOp {
left: NumberLiteral(1),
op: NotEqual,
right: NumberLiteral(2),
},
}
22 changes: 6 additions & 16 deletions lox_tests/string.syntax
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
BinaryOp {
left: StringLiteral(hello),
op: Plus,
right: StringLiteral( world),
}
Diagnostic {
severity: Error,
span: @14..@15,
message: "extra tokens after expression",
labels: [
Label {
span: @14..@15,
message: "here",
},
],
children: [],
Expr {
expr: BinaryOp {
left: StringLiteral(hello),
op: Plus,
right: StringLiteral( world),
},
}

0 comments on commit 3419ec3

Please sign in to comment.