Added REPL.

This commit is contained in:
Goren Barak 2023-12-30 19:28:11 -05:00
parent e31e72af40
commit 2a73930576
2 changed files with 50 additions and 4 deletions

View file

@ -156,7 +156,7 @@ impl<'a> Vm {
let expr = unwrap!(); let expr = unwrap!();
let mut content = String::new(); let mut content = String::new();
let mut next = parser.inner_next(); let mut next = parser.inner_next();
while next != Some("THEN") && next != None { while !matches!(next, Some("THEN")|Some("then")|None) {
content.push_str(next.unwrap()); content.push_str(next.unwrap());
content.push_str(" "); content.push_str(" ");
next = parser.inner_next(); next = parser.inner_next();
@ -168,6 +168,30 @@ impl<'a> Vm {
self.vmrun(&mut Parser::new(content.as_str())); self.vmrun(&mut Parser::new(content.as_str()));
} }
}, },
"(" => {
let mut next = parser.inner_next();
while next != Some(")") && next != None {
next = parser.inner_next();
}
},
"DO"|"do" => {
let start = unwrap!();
let limit = unwrap!();
let mut action = String::new();
let mut next = parser.inner_next();
while !matches!(next, Some("LOOP")|Some("loop")|None) {
action.push_str(next.unwrap());
action.push_str(" ");
next = parser.inner_next();
}
for i in start..limit {
self.dictionary.words.insert("I".to_string(), i.to_string());
self.vmrun(&mut Parser::new(action.as_str()));
}
self.dictionary.words.remove("I");
},
":" => { ":" => {
let mut word_content = String::new(); let mut word_content = String::new();
let Some(Word(word_name)) = parser.next() else { println!("ERROR: Word name not given."); std::process::exit(1); }; let Some(Word(word_name)) = parser.next() else { println!("ERROR: Word name not given."); std::process::exit(1); };
@ -180,6 +204,9 @@ impl<'a> Vm {
self.dictionary.words.insert(word_name.to_string(), word_content); self.dictionary.words.insert(word_name.to_string(), word_content);
}, },
"BYE"|"bye" => {
std::process::exit(0);
}
word => { word => {
let words = self.dictionary.words.clone(); let words = self.dictionary.words.clone();
let Some(word_content) = words.get(word) else { let Some(word_content) = words.get(word) else {

View file

@ -1,19 +1,38 @@
use std::io::Write;
use std::fs::read_to_string; use std::fs::read_to_string;
pub mod parse; pub mod parse;
pub mod backend; pub mod backend;
use parse::Parser; use parse::Parser;
use backend::Vm; use backend::Vm;
fn repl() {
println!("Fourth REPL:");
println!("To exit, use the `bye` command");
let mut code = String::new();
loop {
print!("> ");
std::io::stdout().flush().unwrap();
let mut line = String::new();
let _ = std::io::stdin().read_line(&mut line);
code.push_str(line.as_str());
if !matches!(line.as_str(), "bye"|"BYE") {
Vm::new().vmrun(&mut Parser::new(format!("{} trace stack\n", code).as_str()));
} else {
println!("Bye bye and have a good day!");
std::io::stdout().flush().unwrap();
std::process::exit(1);
}
}
}
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let mut args = std::env::args(); let mut args = std::env::args();
let command = args.next().unwrap(); let _command = args.next().unwrap();
if let Some(filename) = args.next() { if let Some(filename) = args.next() {
let contents = read_to_string(filename)?; let contents = read_to_string(filename)?;
let contents = contents.as_str(); let contents = contents.as_str();
Vm::new().vmrun(&mut Parser::new(contents)); Vm::new().vmrun(&mut Parser::new(contents));
} else { } else {
eprintln!("Usage: {} filename", command); repl();
std::process::exit(1);
} }
Ok(()) Ok(())
} }