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 parse_math(mut tokens: Lexer<Token>) -> Option<Expr> {
|
pub fn expect_number(n: Option<Result<Token, ()>>) -> Expr<'static> {
|
||||||
if let Some(left) = parse_math(tokens) {
|
match n {
|
||||||
if let Some(op) = match_operator(tokens) {
|
Some(Ok(Number(res))) => Expr::Number(res),
|
||||||
if let Some(Ok(Number(right))) = tokens.next() {
|
Some(Ok(tok)) => {
|
||||||
return Some(Expr::MathExpr(Math {left: Rc::new(left), right: Rc::new(Expr::Number(right)), operator: op}));
|
println!("Expected a number, got: {}", tok);
|
||||||
}
|
std::process::exit(1);
|
||||||
} else {
|
}
|
||||||
|
_ => {
|
||||||
|
println!("Expected a number, got EOF");
|
||||||
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_global_declaration(mut tokens: Lexer<Token>) -> Option<Expr> {
|
pub fn parse_math(mut tokens: Lexer<Token>) -> Option<Expr> {
|
||||||
let mut tok = None;
|
let mut left = expect_number(tokens.next());
|
||||||
if unwrap!(tokens) == Let && unwrap!(tokens) == Identifier {
|
|
||||||
let name = tokens.slice();
|
|
||||||
|
|
||||||
println!("{:?}", name);
|
loop {
|
||||||
if unwrap!(tokens) == Equal {
|
let op = tokens.next();
|
||||||
let temp_token = unwrap!(tokens);
|
if !match_operator(&op).is_some() { break }
|
||||||
if let Number(n) = temp_token {
|
let right = expect_number(tokens.next());
|
||||||
tok = Some(Expr::GlobalDefinition(Rc::new(VarDefinition {name, value: n})));
|
left = Expr::MathExpr(Math {left: Rc::new(left), right: Rc::new(right), operator: match_operator(&op)?});
|
||||||
}
|
}
|
||||||
}
|
Some(left)
|
||||||
}
|
|
||||||
tok
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn match_operator(mut tokens: Lexer<Token>) -> Option<MathOperator> {
|
pub fn match_operator(token: &Option<Result<Token, ()>>) -> Option<MathOperator> {
|
||||||
if let Some(Ok(token)) = tokens.next() {
|
if let Some(Ok(token)) = token {
|
||||||
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