Skip to content

Commit

Permalink
supports reading variables
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 24, 2023
1 parent 31f96f7 commit 3f55d28
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 3 deletions.
4 changes: 4 additions & 0 deletions components/lox-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,9 @@ fn compile_expr(db: &dyn crate::Db, expr: &syntax::Expr, chunk: &mut Chunk) {
}
}
syntax::Expr::Parenthesized(_) => todo!(),
syntax::Expr::Variable(word) => {
let word_str = word.as_str(db);
chunk.emit_byte(Code::Variable(word_str.to_string()))
},
}
}
4 changes: 4 additions & 0 deletions components/lox-execute/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ impl VM {
bytecode::Code::Nil => {
self.push(Value::Nil);
},
bytecode::Code::Variable(name) => {
let value = self.globals.get(&name).expect("variable not found");
self.push(value.clone());
},
}
if let Some(step_inspect) = &mut step_inspect {
step_inspect(instruction, self);
Expand Down
3 changes: 2 additions & 1 deletion components/lox-ir/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub enum Code {
LessEqual,
String(String),
Print,
VarDeclaration(String), // name of the variable
VarDeclaration(String),
Variable(String),
Nil,
}

Expand Down
4 changes: 4 additions & 0 deletions components/lox-ir/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub enum Expr {

// `(expr)`
Parenthesized(Box<Expr>),

// `foo`
Variable(Word),
}

impl<'db> salsa::DebugWithDb<dyn crate::Db + 'db> for Expr {
Expand Down Expand Up @@ -53,6 +56,7 @@ impl<'db> salsa::DebugWithDb<dyn crate::Db + 'db> for Expr {
.finish(),
Expr::BooleanLiteral(value) => write!(f, "BooleanLiteral({})", value),
Expr::StringLiteral(word) => write!(f, "StringLiteral({})", word.as_str(db)),
Expr::Variable(word) => write!(f, "Variable({})", word.as_str(db)),
_ => todo!(),
}
}
Expand Down
2 changes: 2 additions & 0 deletions components/lox-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ impl<'me> Parser<'me> {
Some(Expr::BooleanLiteral(false))
} else if self.eat(Keyword::Nil).is_some() {
Some(Expr::NilLiteral)
} else if let Some((_, word)) = self.eat(Identifier) {
Some(Expr::Variable(word))
} else if let Some((_, word)) = self.eat(Number) {
Some(Expr::NumberLiteral(word))
} else if let Some((_, word)) = self.eat(StringLiteral) {
Expand Down
3 changes: 2 additions & 1 deletion lox_tests/var.lox
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
var a = 1;
var b;
var b;
print a;
4 changes: 4 additions & 0 deletions lox_tests/var/bytecode
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ Chunk {
VarDeclaration(
"b",
),
Variable(
"a",
),
Print,
],
}
12 changes: 12 additions & 0 deletions lox_tests/var/execute
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ execute: VarDeclaration(
)
stack: []

execute: Variable(
"a",
)
stack: [
Number(
1.0,
),
]

execute: Print
stack: []

3 changes: 3 additions & 0 deletions lox_tests/var/syntax
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ Var {
name: "b",
initializer: None,
}
Print {
expr: Variable(a),
}
7 changes: 6 additions & 1 deletion lox_tests/var/token
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TokenTree {
source text: "var a = 1;\nvar b;",
source text: "var a = 1;\nvar b;\nprint a;",
tokens: [
Alphabetic(var),
Whitespace(' '),
Expand All @@ -14,5 +14,10 @@ TokenTree {
Whitespace(' '),
Alphabetic(b),
Semicolon,
Whitespace('\n'),
Alphabetic(print),
Whitespace(' '),
Alphabetic(a),
Semicolon,
],
}

0 comments on commit 3f55d28

Please sign in to comment.