Implement interpret from file resolves #6
This commit is contained in:
21
src/main.rs
21
src/main.rs
@@ -3,6 +3,10 @@ mod interpreter;
|
|||||||
mod lexer;
|
mod lexer;
|
||||||
mod parser;
|
mod parser;
|
||||||
mod primitives;
|
mod primitives;
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
|
use std::fs;
|
||||||
|
use std::env;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
|
||||||
@@ -32,14 +36,29 @@ fn interpret_code(scm_code: &str) {
|
|||||||
println!("{:?}", result);
|
println!("{:?}", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn interpret_file() {}
|
fn interpret_file(filename: &Path) {
|
||||||
|
match fs::read_to_string(filename) {
|
||||||
|
Ok(scm_code) => {
|
||||||
|
let tokens = lexer::read(scm_code.as_str());
|
||||||
|
let datum = parser::parse(tokens);
|
||||||
|
let mut env = environment::get_global_environment();
|
||||||
|
let result = interpreter::interpret(&datum, &mut env);
|
||||||
|
println!("{:?}", result);
|
||||||
|
},
|
||||||
|
Err(error) => println!("error: {} {:?}", error, filename),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
// let scm_code = "(begin (define (f n) (if (= n 1) 1 (* n (f (- n 1))))) (f 10))";
|
// let scm_code = "(begin (define (f n) (if (= n 1) 1 (* n (f (- n 1))))) (f 10))";
|
||||||
let scm_code = "";
|
let scm_code = "";
|
||||||
|
|
||||||
if scm_code.len() > 0 {
|
if scm_code.len() > 0 {
|
||||||
interpret_code(scm_code);
|
interpret_code(scm_code);
|
||||||
|
} else if args.len() == 2 {
|
||||||
|
let arg_path = Path::new(&args[1]);
|
||||||
|
interpret_file(arg_path);
|
||||||
} else {
|
} else {
|
||||||
repl();
|
repl();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user