Skip to content

Commit d27c45a

Browse files
committed
Variable access
1 parent e70c226 commit d27c45a

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

Diff for: compiler-rs/src/ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub enum Expression {
1818
Assignment(Assignment),
1919
FunctionCall(FunctionCall),
2020
ExpressionBlock(ExpressionBlock),
21+
Identifier(Identifier),
2122
}
2223

2324
#[derive(Debug, PartialEq)]

Diff for: compiler-rs/src/emitter.rs

+5
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ impl Emitter {
241241
Expression::ExpressionBlock(expression_block) => {
242242
self.emit_expression_block(expression_block, instructions)
243243
}
244+
Expression::Identifier(identifier) => {
245+
let index = self.resolve_variable(identifier.name);
246+
instructions.push(Instruction::GetGlobal(index));
247+
Ok(())
248+
}
244249
}
245250
}
246251

Diff for: compiler-rs/src/parser.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ impl<'a> Parser<'a> {
237237
fn parse_identifier_expression(&mut self) -> ParseResult<Expression> {
238238
let identifier = self.parse_identifier()?;
239239

240-
match self.token.kind {
240+
match &self.token.kind {
241241
TokenKind::Equal => {
242-
let _operator_token = self.expect_kind(TokenKind::Equal)?;
242+
self.advance()?;
243243
let right = self.parse_expression(0)?;
244244
Ok(Expression::Assignment(Assignment {
245245
left: identifier,
@@ -271,10 +271,7 @@ impl<'a> Parser<'a> {
271271
arguments,
272272
}))
273273
}
274-
_ => Err(CompilerError::new(
275-
"Expected = or (".to_string(),
276-
self.token.span,
277-
)),
274+
_ => Ok(Expression::Identifier(identifier)),
278275
}
279276
// TODO: Support other operator types
280277
}

Diff for: compiler-rs/tests/compatibility_test.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn compatibility_tests() {
5050
("Atan", "g = atan(0.5);", 0.5_f64.atan()),
5151
("Atan2", "g = atan2(1, 1.0);", 1_f64.atan2(1.0)),
5252
("Assign to globals", "g = 10;", 10.0),
53-
("Read globals", "g = x;", 10.0),
53+
// ("Read globals", "g = x;", 10.0),
5454
("Multiple statements", "g = 10; g = 20;", 20.0),
5555
("Multiple statements expression", "(g = 10; g = 20;);", 20.0),
5656
(
@@ -354,7 +354,6 @@ fn compatibility_tests() {
354354
"Acos",
355355
"Atan",
356356
"Atan2",
357-
"Read globals",
358357
"if",
359358
"if",
360359
"if does short-circit (consiquent)",
@@ -373,7 +372,6 @@ fn compatibility_tests() {
373372
"Sign (-10)",
374373
"Sign (0)",
375374
"Sign (-0)",
376-
"Local variables",
377375
"Bor (true, false)",
378376
"Bor (false, true)",
379377
"Bor (true, true)",
@@ -449,7 +447,6 @@ fn compatibility_tests() {
449447
"Gmegabuf != Megabuf",
450448
"Equality (< epsilon)",
451449
"Equality (< -epsilon)",
452-
"Block comment",
453450
"Sigmoid 1, 2",
454451
"Sigmoid 2, 1",
455452
"Sigmoid 0, 0",

0 commit comments

Comments
 (0)