about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/interpreter.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-12-20T22·55+0100
committertazjin <mail@tazj.in>2020-12-22T10·13+0000
commit75ae25daa9e7686bca60b518d9cf442bcfba3bf7 (patch)
treead9fa978d10be144010fd41b26b272f3e0ad0fd5 /users/tazjin/rlox/src/interpreter.rs
parentc3bbb861b8a97f7ea0df51f56bacf14145350a42 (diff)
feat(tazjin/rlox): Add support for statements r/2024
First part of
https://craftinginterpreters.com/statements-and-state.html

Supports print statements, as well as evaluation for the sake of
it (i.e. future side-effects).

Change-Id: Ic6653b568f98d6cfe3f297615b7113c0ba1d9a70
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2287
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/interpreter.rs')
-rw-r--r--users/tazjin/rlox/src/interpreter.rs42
1 files changed, 25 insertions, 17 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs
index 6c411ded5e11..c68101cf68a6 100644
--- a/users/tazjin/rlox/src/interpreter.rs
+++ b/users/tazjin/rlox/src/interpreter.rs
@@ -1,33 +1,25 @@
 use crate::errors::{report, Error, ErrorKind};
-use crate::parser::{self, Expr, Literal};
-use crate::scanner::{self, Token, TokenKind};
+use crate::parser::{self, Expr, Literal, Program, Statement};
+use crate::scanner::{self, TokenKind};
 
 // Run some Lox code and print it to stdout
 pub fn run(code: &str) {
     let chars: Vec<char> = code.chars().collect();
 
     match scanner::scan(&chars) {
-        Ok(tokens) => {
-            print_tokens(&tokens);
-            match parser::parse(tokens) {
-                Ok(expr) => {
-                    println!("Expression:\n{:?}", expr);
-                    println!("Result: {:?}", eval(&expr));
+        Ok(tokens) => match parser::parse(tokens) {
+            Ok(program) => {
+                println!("Program:\n{:?}", program);
+                if let Err(err) = run_program(&program) {
+                    println!("Error in program: {:?}", err);
                 }
-                Err(errors) => report_errors(errors),
             }
-        }
+            Err(errors) => report_errors(errors),
+        },
         Err(errors) => report_errors(errors),
     }
 }
 
-fn print_tokens<'a>(tokens: &Vec<Token<'a>>) {
-    println!("Tokens:");
-    for token in tokens {
-        println!("{:?}", token);
-    }
-}
-
 fn report_errors(errors: Vec<Error>) {
     for error in errors {
         report(&error);
@@ -111,3 +103,19 @@ fn eval<'a>(expr: &Expr<'a>) -> Result<Literal, Error> {
         Expr::Binary(binary) => eval_binary(binary),
     }
 }
+
+fn run_program<'a>(program: &Program<'a>) -> Result<(), Error> {
+    for stmt in program {
+        match stmt {
+            Statement::Expr(expr) => {
+                eval(expr)?;
+            }
+            Statement::Print(expr) => {
+                let result = eval(expr)?;
+                println!("{:?}", result)
+            }
+        }
+    }
+
+    Ok(())
+}