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