From 1342a76786250ecf059368342ccaf6169d3912e5 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 24 Jul 2015 03:08:54 -0700 Subject: [PATCH] Added support for interpreter directives --- src/main.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index fd3b36a..6d9f223 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::io; use std::io::Write; use std::io::BufRead; +use std::process; use tokenizer::tokenize; use parser::{parse, ParseResult}; @@ -19,16 +20,22 @@ fn repl() { let mut stdout = io::stdout(); let mut buf = String::with_capacity(20); loop { + buf.clear(); print!(">> "); stdout.flush().ok(); let line = stdin.lock().read_line(&mut buf); + match line { Ok(_) => { if buf.is_empty() { break; } + + if handle_interpreter_directive(&buf) { + continue; + } + let tokens = tokenize(&buf); - buf.clear(); println!("Tokens: {:?}", tokens); match parse(tokens) { @@ -47,3 +54,25 @@ fn repl() { } } } + +fn handle_interpreter_directive(input: &str) -> bool { + + match input.chars().nth(0) { + Some('.') => (), + _ => return false + } + + let commands: Vec<&str> = input.split(|c: char| c.is_whitespace()).collect(); + match commands.get(0) { + Some(s) if *s == ".quit" => { + println!("Siturei simasu"); + process::exit(0); + }, + Some(s) => { + println!("Unknown directive: {}", s); + }, + None => () //should never happen + } + + return true; +}