Skip to content

Commit

Permalink
supports print statement
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 20, 2023
1 parent 3419ec3 commit 73cafa4
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 1 deletion.
4 changes: 4 additions & 0 deletions components/lox-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub fn compile_file(db: &dyn crate::Db, input_file: InputFile) -> Chunk {
for stmt in stmts {
match stmt {
syntax::Stmt::Expr(expr) => compile_expr(db, expr, &mut chunk),
syntax::Stmt::Print(expr) => {
compile_expr(db, expr, &mut chunk);
chunk.emit_byte(Code::Print)
}
}
}
chunk
Expand Down
5 changes: 5 additions & 0 deletions components/lox-execute/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ impl VM {
bytecode::Code::String(s) => {
self.push(s);
}
bytecode::Code::Print => {
let value = self.pop();
// FIXME: This should be a call to a intrinsic function.
println!("{:?}", value);
},
}
if let Some(step_inspect) = &mut step_inspect {
step_inspect(instruction, self);
Expand Down
1 change: 1 addition & 0 deletions components/lox-ir/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum Code {
Less,
LessEqual,
String(String),
Print
}

#[derive(PartialEq, Eq, Debug, Clone, Default)]
Expand Down
1 change: 1 addition & 0 deletions components/lox-ir/src/kw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ define_keywords! {
True => "true",
False => "false",
Nil => "nil",
Print => "print",
}
4 changes: 4 additions & 0 deletions components/lox-ir/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ impl<'db> salsa::DebugWithDb<dyn crate::Db + 'db> for Expr {
pub enum Stmt {
// expression statement, like `1 + 2;`
Expr(Expr),

// print statement, like `print 1 + 2;`
Print(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(),
Stmt::Print(expr) => f.debug_struct("Print").field("expr", &expr.debug(db)).finish(),
}
}
}
15 changes: 14 additions & 1 deletion components/lox-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'me> Parser<'me> {

pub(crate) fn parse(&mut self) -> Vec<Stmt> {
let mut stmts = vec![];
while let Some(stmt) = self.expr_stmt() {
while let Some(stmt) = self.stmt() {
stmts.push(stmt);
}
if self.tokens.peek().is_some() {
Expand All @@ -42,6 +42,19 @@ impl<'me> Parser<'me> {
stmts
}

fn stmt(&mut self) -> Option<Stmt> {
if self.eat(Keyword::Print).is_some() {
return self.print_stmt();
}
self.expr_stmt()
}

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

fn expr_stmt(&mut self) -> Option<Stmt> {
let expr = self.parse_expr()?;
self.eat(Token::Semicolon);
Expand Down
16 changes: 16 additions & 0 deletions lox_tests/print.bytecode
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Chunk {
code: [
Constant(
F64(
1.0,
),
),
Constant(
F64(
2.0,
),
),
Add,
Print,
],
}
35 changes: 35 additions & 0 deletions lox_tests/print.execute
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
execute: Constant(
F64(
1.0,
),
)
stack: [
Number(
1.0,
),
]

execute: Constant(
F64(
2.0,
),
)
stack: [
Number(
1.0,
),
Number(
2.0,
),
]

execute: Add
stack: [
Number(
3.0,
),
]

execute: Print
stack: []

1 change: 1 addition & 0 deletions lox_tests/print.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print 1 + 2
7 changes: 7 additions & 0 deletions lox_tests/print.syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Print {
expr: BinaryOp {
left: NumberLiteral(1),
op: Plus,
right: NumberLiteral(2),
},
}
12 changes: 12 additions & 0 deletions lox_tests/print.token
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
TokenTree {
source text: "print 1 + 2",
tokens: [
Alphabetic(print),
Whitespace(' '),
Number(1),
Whitespace(' '),
Op(+),
Whitespace(' '),
Number(2),
],
}

0 comments on commit 73cafa4

Please sign in to comment.