Skip to content

Commit

Permalink
assignment is an expression
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 25, 2023
1 parent 03ff05a commit ed35a2f
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 6 deletions.
8 changes: 6 additions & 2 deletions components/lox-execute/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ impl VM {
self.push(value.clone());
}
bytecode::Code::Assign(name) => {
let value = self.pop();
self.globals.insert(name, value);
let value = self.peek();
self.globals.insert(name, value.clone());
}
}
if let Some(step_inspect) = &mut step_inspect {
Expand All @@ -255,6 +255,10 @@ impl VM {
self.stack.pop().unwrap()
}

fn peek(&self) -> &Value {
self.stack.last().unwrap()
}

fn push<T>(&mut self, value: T)
where
T: Into<Value>,
Expand Down
7 changes: 6 additions & 1 deletion lox_tests/assignment.lox
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
var a = 1;
a = 2;
print a;
print a;
print a = 3;
var b = a;
var c;
c = b;
print c;
29 changes: 29 additions & 0 deletions lox_tests/assignment/bytecode
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,34 @@ Chunk {
"a",
),
Print,
Constant(
F64(
3.0,
),
),
Assign(
"a",
),
Print,
Variable(
"a",
),
VarDeclaration(
"b",
),
Nil,
VarDeclaration(
"c",
),
Variable(
"b",
),
Assign(
"c",
),
Variable(
"c",
),
Print,
],
}
135 changes: 133 additions & 2 deletions lox_tests/assignment/execute
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ stack: [
execute: Assign(
"a",
)
stack: []
stack: [
Number(
2.0,
),
]

execute: Variable(
"a",
Expand All @@ -37,8 +41,135 @@ stack: [
Number(
2.0,
),
Number(
2.0,
),
]

execute: Print
stack: []
stack: [
Number(
2.0,
),
]

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

execute: Assign(
"a",
)
stack: [
Number(
2.0,
),
Number(
3.0,
),
]

execute: Print
stack: [
Number(
2.0,
),
]

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

execute: VarDeclaration(
"b",
)
stack: [
Number(
2.0,
),
]

execute: Nil
stack: [
Number(
2.0,
),
Nil,
]

execute: VarDeclaration(
"c",
)
stack: [
Number(
2.0,
),
]

execute: Variable(
"b",
)
stack: [
Number(
2.0,
),
Number(
3.0,
),
]

execute: Assign(
"c",
)
stack: [
Number(
2.0,
),
Number(
3.0,
),
]

execute: Variable(
"c",
)
stack: [
Number(
2.0,
),
Number(
3.0,
),
Number(
3.0,
),
]

execute: Print
stack: [
Number(
2.0,
),
Number(
3.0,
),
]

25 changes: 25 additions & 0 deletions lox_tests/assignment/syntax
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,28 @@ Expr {
Print {
expr: Variable(a),
}
Print {
expr: Assign {
name: "a",
value: NumberLiteral(3),
},
}
Var {
name: "b",
initializer: Some(
Variable(a),
),
}
Var {
name: "c",
initializer: None,
}
Expr {
expr: Assign {
name: "c",
value: Variable(b),
},
}
Print {
expr: Variable(c),
}
37 changes: 36 additions & 1 deletion lox_tests/assignment/token
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TokenTree {
source text: "var a = 1;\na = 2;\nprint a;",
source text: "var a = 1;\na = 2;\nprint a;\nprint a = 3;\nvar b = a;\nvar c;\nc = b;\nprint c;",
tokens: [
Alphabetic(var),
Whitespace(' '),
Expand All @@ -21,5 +21,40 @@ TokenTree {
Whitespace(' '),
Alphabetic(a),
Semicolon,
Whitespace('\n'),
Alphabetic(print),
Whitespace(' '),
Alphabetic(a),
Whitespace(' '),
Op(=),
Whitespace(' '),
Number(3),
Semicolon,
Whitespace('\n'),
Alphabetic(var),
Whitespace(' '),
Alphabetic(b),
Whitespace(' '),
Op(=),
Whitespace(' '),
Alphabetic(a),
Semicolon,
Whitespace('\n'),
Alphabetic(var),
Whitespace(' '),
Alphabetic(c),
Semicolon,
Whitespace('\n'),
Alphabetic(c),
Whitespace(' '),
Op(=),
Whitespace(' '),
Alphabetic(b),
Semicolon,
Whitespace('\n'),
Alphabetic(print),
Whitespace(' '),
Alphabetic(c),
Semicolon,
],
}

0 comments on commit ed35a2f

Please sign in to comment.