Implement simple CLI
- Implement running files - Implement interactive mode - Enable printing tokens & ast with flags
This commit is contained in:
parent
49ada446f8
commit
c49a5ec0e2
@ -1,6 +1,6 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::parser::{Expression, BinOpType, UnOpType, Ast, Statement};
|
use crate::{parser::{Expression, BinOpType, UnOpType, Ast, Statement, parse}, lexer::lex};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
@ -19,6 +19,20 @@ impl Interpreter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn run_str(&mut self, code: &str, print_tokens: bool, print_ast: bool) {
|
||||||
|
let tokens = lex(code);
|
||||||
|
if print_tokens {
|
||||||
|
println!("Tokens: {:?}", tokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
let ast = parse(tokens);
|
||||||
|
if print_ast {
|
||||||
|
println!("{:#?}", ast);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.run(ast);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn run(&mut self, prog: Ast) {
|
pub fn run(&mut self, prog: Ast) {
|
||||||
for stmt in prog.prog {
|
for stmt in prog.prog {
|
||||||
match stmt {
|
match stmt {
|
||||||
|
|||||||
63
src/main.rs
63
src/main.rs
@ -1,34 +1,55 @@
|
|||||||
use nek_lang::{lexer::lex, parser::parse, interpreter::Interpreter};
|
use std::{env::args, fs, io::{stdout, Write, stdin}};
|
||||||
|
|
||||||
|
use nek_lang::interpreter::Interpreter;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct CliConfig {
|
||||||
|
print_tokens: bool,
|
||||||
|
print_ast: bool,
|
||||||
|
interactive: bool,
|
||||||
|
file: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
|
let mut conf = CliConfig::default();
|
||||||
|
|
||||||
|
// Go through all commandline arguments except the first (filename)
|
||||||
|
for arg in args().skip(1) {
|
||||||
|
match arg.as_str() {
|
||||||
|
"--token" | "-t" => conf.print_tokens = true,
|
||||||
|
"--ast" | "-a" => conf.print_ast = true,
|
||||||
|
"--interactive" | "-i" => conf.interactive = true,
|
||||||
|
file if conf.file.is_none() => conf.file = Some(file.to_string()),
|
||||||
|
_ => panic!("Invalid argument: '{}'", arg),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut interpreter = Interpreter::new();
|
let mut interpreter = Interpreter::new();
|
||||||
|
|
||||||
// let mut code = String::new();
|
if let Some(file) = &conf.file {
|
||||||
let code = "
|
let code = fs::read_to_string(file).expect(&format!("File not found: '{}'", file));
|
||||||
a <- 5;
|
interpreter.run_str(&code, conf.print_tokens, conf.print_ast);
|
||||||
// nek-lang best lang
|
}
|
||||||
a * 2;
|
|
||||||
";
|
|
||||||
|
|
||||||
// loop {
|
if conf.interactive || conf.file.is_none() {
|
||||||
// print!(">> ");
|
let mut code = String::new();
|
||||||
// std::io::stdout().flush().unwrap();
|
|
||||||
|
|
||||||
// code.clear();
|
loop {
|
||||||
// std::io::stdin().read_line(&mut code).unwrap();
|
print!(">> ");
|
||||||
// let code = code.trim();
|
stdout().flush().unwrap();
|
||||||
|
|
||||||
let tokens = lex(&code);
|
code.clear();
|
||||||
|
stdin().read_line(&mut code).unwrap();
|
||||||
|
|
||||||
|
if code.trim() == "exit" {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
println!("Tokens: {:?}\n", tokens);
|
interpreter.run_str(&code, conf.print_tokens, conf.print_ast);
|
||||||
|
}
|
||||||
|
|
||||||
let ast = parse(tokens);
|
}
|
||||||
|
|
||||||
println!("Ast: {:#?}\n", ast);
|
|
||||||
|
|
||||||
interpreter.run(ast);
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user