Math works!
This commit is contained in:
parent
626f1694f4
commit
ac72d8add8
|
@ -1,4 +1,5 @@
|
|||
#![allow(unused)]
|
||||
use std::string::String;
|
||||
use logos::Logos;
|
||||
use logos::Lexer;
|
||||
use core::iter::Peekable;
|
||||
|
@ -84,11 +85,11 @@ pub enum Token {
|
|||
|
||||
// LITERALS
|
||||
#[regex(r#"("[^"]*")|('[^']*')"#)]
|
||||
String, // A string literal.
|
||||
StringLiteral, // A string literal.
|
||||
#[regex("[0-9]+", |lex| lex.slice().parse().ok())]
|
||||
Number(u64), // An integer.
|
||||
#[regex(r#"[^[0-9]^"^-^[ \t\n\f]^\.^=^(^)^{^}.^,^;^[+-/*%]]+[^"^-^=^\..^[ \t\n\f]^(^)^{^}^,^;^[+-/*%]]*"#)]
|
||||
Identifier, // An identifier.
|
||||
Number(u64), // An integer.
|
||||
#[regex(r#"[^[0-9]^"^-^[ \t\n\f]^\.^=^(^)^{^}.^,^;^[+-/*%]]+[^"^-^=^\..^[ \t\n\f]^(^)^{^}^,^;^[+-/*%]]*"#, |lex| lex.slice().to_owned())]
|
||||
Identifier(String), // An identifier.
|
||||
#[token("true")]
|
||||
True, // true
|
||||
#[token("false")]
|
||||
|
@ -147,7 +148,7 @@ impl std::fmt::Display for Token {
|
|||
GreaterEqual => write!(f, ">="),
|
||||
String => write!(f, "[string]"),
|
||||
Number(n) => write!(f, "{}", n),
|
||||
Identifier => write!(f, "identifier"),
|
||||
Identifier(i) => write!(f, "{}", i),
|
||||
True => write!(f, "true"),
|
||||
False => write!(f, "false"),
|
||||
Null => write!(f, "none"),
|
||||
|
|
|
@ -10,38 +10,34 @@ macro_rules! unwrap {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn parse_math(mut tokens: Lexer<Token>) -> Option<Expr> {
|
||||
if let Some(left) = parse_math(tokens) {
|
||||
if let Some(op) = match_operator(tokens) {
|
||||
if let Some(Ok(Number(right))) = tokens.next() {
|
||||
return Some(Expr::MathExpr(Math {left: Rc::new(left), right: Rc::new(Expr::Number(right)), operator: op}));
|
||||
}
|
||||
} else {
|
||||
|
||||
pub fn expect_number(n: Option<Result<Token, ()>>) -> Expr<'static> {
|
||||
match n {
|
||||
Some(Ok(Number(res))) => Expr::Number(res),
|
||||
Some(Ok(tok)) => {
|
||||
println!("Expected a number, got: {}", tok);
|
||||
std::process::exit(1);
|
||||
}
|
||||
_ => {
|
||||
println!("Expected a number, got EOF");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn parse_global_declaration(mut tokens: Lexer<Token>) -> Option<Expr> {
|
||||
let mut tok = None;
|
||||
if unwrap!(tokens) == Let && unwrap!(tokens) == Identifier {
|
||||
let name = tokens.slice();
|
||||
|
||||
println!("{:?}", name);
|
||||
if unwrap!(tokens) == Equal {
|
||||
let temp_token = unwrap!(tokens);
|
||||
if let Number(n) = temp_token {
|
||||
tok = Some(Expr::GlobalDefinition(Rc::new(VarDefinition {name, value: n})));
|
||||
}
|
||||
}
|
||||
}
|
||||
tok
|
||||
pub fn parse_math(mut tokens: Lexer<Token>) -> Option<Expr> {
|
||||
let mut left = expect_number(tokens.next());
|
||||
|
||||
loop {
|
||||
let op = tokens.next();
|
||||
if !match_operator(&op).is_some() { break }
|
||||
let right = expect_number(tokens.next());
|
||||
left = Expr::MathExpr(Math {left: Rc::new(left), right: Rc::new(right), operator: match_operator(&op)?});
|
||||
}
|
||||
Some(left)
|
||||
}
|
||||
|
||||
pub fn match_operator(mut tokens: Lexer<Token>) -> Option<MathOperator> {
|
||||
if let Some(Ok(token)) = tokens.next() {
|
||||
pub fn match_operator(token: &Option<Result<Token, ()>>) -> Option<MathOperator> {
|
||||
if let Some(Ok(token)) = token {
|
||||
return match token {
|
||||
Plus => Some(MathOperator::OP_ADD),
|
||||
Minus => Some(MathOperator::OP_SUB),
|
||||
|
|
Loading…
Reference in a new issue