Skip to content

Commit

Permalink
✨ implement verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
KPMGE committed Mar 19, 2024
1 parent b5469c4 commit ce708ff
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 24 deletions.
104 changes: 104 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"

[dependencies]
gflags = "0.3.12"
62 changes: 39 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ use std::io::{self, BufRead, Write};

use kl_rs::{evaluator::Evaluator, lexer::Lexer, parser::Parser, token::Token};

gflags::define! {
-v, --verbose = false
}

fn main() {
let stdin = std::io::stdin();
let mut handle = stdin.lock();
let mut evaluator = Evaluator::new();
gflags::parse();

loop {
let mut input = String::new();
Expand All @@ -29,37 +34,48 @@ fn main() {
_ => {}
}

let mut lexer = Lexer::new(input.to_string());
let lexer = Lexer::new(input.to_string());
let mut parser = Parser::new(lexer.clone());

println!("Tokens: \n");

let mut token = lexer.next_token();

while token != Token::Eof {
println!("{:?}", token);
token = lexer.next_token();
if VERBOSE.flag {
debug_lexer(lexer.clone());
debug_parser(parser.clone());
}
println!();

let program = parser.parse_program();

println!("Parsed program: ");
if let kl_rs::ast::AstNode::Program { ref statements } = program {
for statement in statements {
println!("statement: {:#?}", statement);
}
}
println!();
let object = evaluator.eval(program);
println!("{}", object.inspect());
}
}

println!("ERRORS: ");
for err in parser.errors {
println!("ERROR: {:?}", err);
fn debug_lexer(mut lexer: Lexer) {
println!("Tokens: \n");

let mut token = lexer.next_token();

while token != Token::Eof {
println!("{:?}", token);
token = lexer.next_token();
}

println!();
}

fn debug_parser(mut parser: Parser) {
let program = parser.parse_program();

println!("Parsed program: ");
if let kl_rs::ast::AstNode::Program { ref statements } = program {
for statement in statements {
println!("statement: {:#?}", statement);
}
println!();
}
println!();

println!("EVALUATED: ");
let object = evaluator.eval(program);
println!("{}", object.inspect());
println!("ERRORS: ");
for err in parser.errors {
println!("ERROR: {:?}", err);
}
println!();
}
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
token::Token,
};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Parser {
pub errors: Vec<String>,
lexer: Lexer,
Expand Down

0 comments on commit ce708ff

Please sign in to comment.