Get parsing REPL working
This commit is contained in:
parent
be98f8387e
commit
247638c4db
@ -11,6 +11,9 @@ use simplerepl::{REPL, ReplState};
|
||||
use tokenizer::tokenize;
|
||||
mod tokenizer;
|
||||
|
||||
use parser::{ParseResult, parse};
|
||||
mod parser;
|
||||
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
@ -55,7 +58,9 @@ impl ReplState for InterpreterState {
|
||||
fn repl_handler(input: &str, state: &mut InterpreterState) -> String {
|
||||
if state.show_tokens {
|
||||
format!("Tokens: {:?}", tokenize(input))
|
||||
} else if state.show_parse{
|
||||
format!("Parse: {:?}", parse(tokenize(input)))
|
||||
} else {
|
||||
format!("{:?}", tokenize(input))
|
||||
format!("{:?}", parse(tokenize(input)))
|
||||
}
|
||||
}
|
||||
|
30
src/parser.rs
Normal file
30
src/parser.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use tokenizer::Token;
|
||||
|
||||
/* grammar
|
||||
|
||||
expr : term ((PLUS|MIMUS) term)*
|
||||
term : factor ((MUL | DIV) factor)*
|
||||
factor : NUM | LPAREN expr RPAREN
|
||||
|
||||
*/
|
||||
|
||||
#[derive(Debug)]
|
||||
enum AST {
|
||||
BinOp(Box<AST>, Box<AST>, Box<AST>),
|
||||
Number(f64),
|
||||
Name(String),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ParseError {
|
||||
err: String
|
||||
}
|
||||
|
||||
pub type ParseResult = Result<AST, ParseError>;
|
||||
|
||||
pub fn parse(input: Vec<Token>) -> ParseResult {
|
||||
let mut current_token: Token;
|
||||
|
||||
return Err(ParseError { err: "error!".to_string() });
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user