about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/interpret/walker.rs2
-rw-r--r--src/main.rs39
-rw-r--r--src/parse/parser.rs2
3 files changed, 33 insertions, 10 deletions
diff --git a/src/interpret/walker.rs b/src/interpret/walker.rs
index 576cfb5..16f3f1c 100644
--- a/src/interpret/walker.rs
+++ b/src/interpret/walker.rs
@@ -46,7 +46,7 @@ impl Walker {
         Ok(result)
     }
 
-    fn walk_expression(&mut self, node: &Expression) -> Result<Value> {
+    pub fn walk_expression(&mut self, node: &Expression) -> Result<Value> {
         match node {
             Expression::Binary { left, op, right } => {
                 let new_value = match op {
diff --git a/src/main.rs b/src/main.rs
index d433a16..e4dfc4f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,18 +1,41 @@
+use std::{env, fs, path::Path};
+
 mod interpret;
 mod lex;
 mod parse;
 
-use std::io::{self, Write};
-
 use lex::lexer::Lexer;
 
 use interpret::walker::Walker;
 use parse::parser::Parser;
-
-const PROMPT: &'static str = "🐇: ";
+use std::io::{self, Write};
 
 fn main() {
-    let walker = Walker::new();
+    let args: Vec<String> = env::args().collect();
+
+    if let Some(filename) = args.get(1) {
+        file(filename)
+    } else {
+        repl()
+    }
+}
+
+fn file(filename: impl AsRef<Path>) {
+    let contents = fs::read_to_string(filename).expect("Failed reading file.");
+
+    let lexer = Lexer::new(&contents);
+    let mut parser = Parser::new(lexer);
+
+    let node = parser.parse().expect("Failed parsing.");
+    let mut walker = Walker::new();
+
+    walker.walk(&node);
+}
+
+fn repl() {
+    const PROMPT: &'static str = "🐇: ";
+
+    let mut walker = Walker::new();
 
     let mut input_buffer;
 
@@ -28,9 +51,9 @@ fn main() {
         let lexer = Lexer::new(input_buffer.trim());
         let mut parser = Parser::new(lexer);
 
-        let node = parser.parse().expect("Failed parsing.");
-        let result = walker.walk(&node).expect("Failed interpreting.");
+        let node = parser.expression().expect("Failed parsing.");
+        let result = walker.walk_expression(&node).expect("Failed interpreting.");
 
-        println!("🥕: {}\n", result);
+        println!("🥕: {:?}\n", result);
     }
 }
diff --git a/src/parse/parser.rs b/src/parse/parser.rs
index 0e865da..41fd954 100644
--- a/src/parse/parser.rs
+++ b/src/parse/parser.rs
@@ -70,7 +70,7 @@ impl<T: Iterator<Item = Token>> Parser<T> {
         Ok(Statement::Expression(expression))
     }
 
-    fn expression(&mut self) -> Result<Expression> {
+    pub fn expression(&mut self) -> Result<Expression> {
         self.assignment_expression()
     }