Fill out a few more precedences
This commit is contained in:
parent
0da7f7e3a1
commit
7d6f946e22
@ -1,5 +1,6 @@
|
|||||||
use std::slice::Iter;
|
use std::slice::Iter;
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use tokenizer::{Token, Kw};
|
use tokenizer::{Token, Kw};
|
||||||
use tokenizer::Token::*;
|
use tokenizer::Token::*;
|
||||||
@ -32,6 +33,14 @@ macro_rules! expect {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! expect_parse {
|
||||||
|
($parse_fn:ident, $tokens:ident) => (
|
||||||
|
match $parse_fn($tokens) {
|
||||||
|
err@ParseResult::Err(_) => return err,
|
||||||
|
ParseResult::Ok(ast) => ast
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn expect_token(tok: Token, tokens: &mut Tokens) -> bool {
|
fn expect_token(tok: Token, tokens: &mut Tokens) -> bool {
|
||||||
if let Some(n) = tokens.next() {
|
if let Some(n) = tokens.next() {
|
||||||
let next = (*n).clone();
|
let next = (*n).clone();
|
||||||
@ -73,11 +82,7 @@ fn statements(tokens: &mut Tokens) -> ParseResult {
|
|||||||
|
|
||||||
let mut statements = Vec::new();
|
let mut statements = Vec::new();
|
||||||
|
|
||||||
let initial_statement = match statement(tokens) {
|
let initial_statement = expect_parse!(statement, tokens);
|
||||||
err@ParseResult::Err(_) => return err,
|
|
||||||
ParseResult::Ok(ast) => ast
|
|
||||||
};
|
|
||||||
|
|
||||||
statements.push(initial_statement);
|
statements.push(initial_statement);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@ -145,17 +150,10 @@ fn expression(tokens: &mut Tokens) -> ParseResult {
|
|||||||
fn if_expression(tokens: &mut Tokens) -> ParseResult {
|
fn if_expression(tokens: &mut Tokens) -> ParseResult {
|
||||||
|
|
||||||
expect!(Keyword(Kw::If), tokens);
|
expect!(Keyword(Kw::If), tokens);
|
||||||
let if_clause = match expression(tokens) {
|
let if_clause = expect_parse!(expression, tokens);
|
||||||
err@ParseResult::Err(_) => return err,
|
|
||||||
ParseResult::Ok(ast) => ast
|
|
||||||
};
|
|
||||||
|
|
||||||
expect!(Keyword(Kw::Then), tokens);
|
expect!(Keyword(Kw::Then), tokens);
|
||||||
|
let then_clause = expect_parse!(expression, tokens);
|
||||||
let then_clause = match expression(tokens) {
|
|
||||||
err@ParseResult::Err(_) => return err,
|
|
||||||
ParseResult::Ok(ast) => ast
|
|
||||||
};
|
|
||||||
|
|
||||||
let else_clause = match tokens.peek().map(|i| i.clone()) {
|
let else_clause = match tokens.peek().map(|i| i.clone()) {
|
||||||
Some(&Keyword(Kw::Else)) => {
|
Some(&Keyword(Kw::Else)) => {
|
||||||
@ -179,16 +177,11 @@ fn if_expression(tokens: &mut Tokens) -> ParseResult {
|
|||||||
|
|
||||||
fn while_expression(tokens: &mut Tokens) -> ParseResult {
|
fn while_expression(tokens: &mut Tokens) -> ParseResult {
|
||||||
expect!(Keyword(Kw::While), tokens);
|
expect!(Keyword(Kw::While), tokens);
|
||||||
let while_expression = match expression(tokens) {
|
let while_expression = expect_parse!(expression, tokens);
|
||||||
err@ParseResult::Err(_) => return err,
|
|
||||||
ParseResult::Ok(ast) => ast
|
|
||||||
};
|
|
||||||
|
|
||||||
expect!(Separator, tokens);
|
expect!(Separator, tokens);
|
||||||
let statements = match statements(tokens) {
|
|
||||||
err@ParseResult::Err(_) => return err,
|
let statements = expect_parse!(statements, tokens);
|
||||||
ParseResult::Ok(ast) => ast
|
|
||||||
};
|
|
||||||
|
|
||||||
expect!(Keyword(Kw::End), tokens);
|
expect!(Keyword(Kw::End), tokens);
|
||||||
|
|
||||||
@ -201,10 +194,7 @@ fn while_expression(tokens: &mut Tokens) -> ParseResult {
|
|||||||
fn binop_expression(precedence: i32, tokens: &mut Tokens) -> ParseResult {
|
fn binop_expression(precedence: i32, tokens: &mut Tokens) -> ParseResult {
|
||||||
|
|
||||||
//TODO left needs to match on an identifiers vs. a prefix operator and return *that* AST
|
//TODO left needs to match on an identifiers vs. a prefix operator and return *that* AST
|
||||||
let mut left: AST = match simple_expression(tokens) {
|
let mut left: AST = expect_parse!(simple_expression, tokens);
|
||||||
err@ParseResult::Err(_) => return err,
|
|
||||||
ParseResult::Ok(ast) => ast
|
|
||||||
};
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let lookahead: Option<&Token> = tokens.peek().map(|i| i.clone());
|
let lookahead: Option<&Token> = tokens.peek().map(|i| i.clone());
|
||||||
@ -251,8 +241,12 @@ fn get_binop_precedence(token: &Token) -> Option<i32> {
|
|||||||
match &identifier_str[..] {
|
match &identifier_str[..] {
|
||||||
"+" => Some(20),
|
"+" => Some(20),
|
||||||
"-" => Some(20),
|
"-" => Some(20),
|
||||||
"*" => Some(40),
|
"*" => Some(30),
|
||||||
"/" => Some(40),
|
"/" => Some(30),
|
||||||
|
"==" => Some(10),
|
||||||
|
">" => Some(15),
|
||||||
|
"<" => Some(15),
|
||||||
|
"<=>" => Some(15),
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user