about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/parser.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-01-16T12·11+0300
committertazjin <mail@tazj.in>2021-01-16T13·46+0000
commit29335a8b63ba62bd83019eb193baba7c5b656da3 (patch)
tree2018e86c92ab702ac2549da399bd707769ccd0c7 /users/tazjin/rlox/src/parser.rs
parentf472c824277d80134ce9e55c0a2ce1cb98201134 (diff)
feat(tazjin/rlox): Implement variable depth resolver r/2113
Implements the first part of the resolver from
https://craftinginterpreters.com/resolving-and-binding.html

This is wired up to the execution paths in main, but not yet in the
tests. The resolved depth is also not actually used for variable
lookups (yet).

Change-Id: I3a8615252b7b9b12d5a290c5ddf85988f61b9184
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2403
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/parser.rs')
-rw-r--r--users/tazjin/rlox/src/parser.rs16
1 files changed, 13 insertions, 3 deletions
diff --git a/users/tazjin/rlox/src/parser.rs b/users/tazjin/rlox/src/parser.rs
index 495304686b2f..e28404aa9fce 100644
--- a/users/tazjin/rlox/src/parser.rs
+++ b/users/tazjin/rlox/src/parser.rs
@@ -15,6 +15,7 @@ use std::rc::Rc;
 pub struct Assign {
     pub name: Token,
     pub value: Box<Expr>,
+    pub depth: Option<usize>,
 }
 
 #[derive(Debug)]
@@ -57,7 +58,10 @@ pub struct Call {
 
 // Not to be confused with `Var`, which is for assignment.
 #[derive(Debug)]
-pub struct Variable(pub Token);
+pub struct Variable {
+    pub name: Token,
+    pub depth: Option<usize>,
+}
 
 #[derive(Debug)]
 pub enum Expr {
@@ -413,10 +417,11 @@ impl Parser {
             let equals = self.previous().clone();
             let value = self.assignment()?;
 
-            if let Expr::Variable(Variable(name)) = expr {
+            if let Expr::Variable(Variable { name, .. }) = expr {
                 return Ok(Expr::Assign(Assign {
                     name,
                     value: Box::new(value),
+                    depth: None,
                 }));
             }
 
@@ -549,7 +554,12 @@ impl Parser {
                 return Ok(Expr::Grouping(Grouping(Box::new(expr))));
             }
 
-            TokenKind::Identifier(_) => return Ok(Expr::Variable(Variable(next))),
+            TokenKind::Identifier(_) => {
+                return Ok(Expr::Variable(Variable {
+                    name: next,
+                    depth: None,
+                }))
+            }
 
             unexpected => {
                 eprintln!("encountered {:?}", unexpected);