From ecd972e55dc3257f1fde256d0ba3fe4cdeaf719e Mon Sep 17 00:00:00 2001 From: Erin Date: Sun, 2 May 2021 16:48:33 +0200 Subject: [PATCH] Parser production ready - TODO: T-Dark block - cargo fmt - Obeyed our clippy overlord --- README.md | 2 +- src/lexer.rs | 9 +++++++++ src/parser/item.rs | 7 +++++++ src/parser/mod.rs | 4 +++- src/parser/ops.rs | 20 ++++++++++++++++++-- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e6b1e41..e833a80 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Ablescript +# AbleScript ![Lines of code](https://img.shields.io/tokei/lines/github/abletheabove/able-script) Bravely going where some languages have gone before. diff --git a/src/lexer.rs b/src/lexer.rs index f3038e4..2fcb4bb 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -174,6 +174,15 @@ pub enum Token { #[token("!=")] OpNeq, + #[token("&")] + LogAnd, + + #[token("|")] + LogOr, + + #[token("!")] + LogNot, + /// Base52 based character ('a') #[token("'.*'")] Char, diff --git a/src/parser/item.rs b/src/parser/item.rs index 1d805fb..e9e8a84 100644 --- a/src/parser/item.rs +++ b/src/parser/item.rs @@ -27,6 +27,13 @@ pub enum Expr { Subtract { left: Box, right: Box }, Multiply { left: Box, right: Box }, Divide { left: Box, right: Box }, + Lt { left: Box, right: Box }, + Gt { left: Box, right: Box }, + Eq { left: Box, right: Box }, + Neq { left: Box, right: Box }, + And { left: Box, right: Box }, + Or { left: Box, right: Box }, + Not(Box), Literal(Value), Identifier(Iden), } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 9084b01..9d31559 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -59,7 +59,9 @@ impl<'a> Parser<'a> { | Token::Aboolean(_) | Token::Boolean(_) | Token::Integer(_) - | Token::String(_) => self.parse_ops(token), + | Token::String(_) + | Token::LogNot => self.parse_ops(token), + // Control flow Token::If => self.if_cond(), diff --git a/src/parser/ops.rs b/src/parser/ops.rs index 69d82c1..1829b86 100644 --- a/src/parser/ops.rs +++ b/src/parser/ops.rs @@ -32,6 +32,12 @@ impl<'a> Parser<'a> { Some(Token::Subtract) => self.subtract(buf)?, Some(Token::Multiply) => self.multiply(buf)?, Some(Token::Divide) => self.divide(buf)?, + Some(Token::OpLt) => self.cmplt(buf)?, + Some(Token::OpGt) => self.cmpgt(buf)?, + Some(Token::OpEq) => self.cmpeq(buf)?, + Some(Token::OpNeq) => self.cmpneq(buf)?, + Some(Token::LogAnd) => self.logand(buf)?, + Some(Token::LogOr) => self.logor(buf)?, Some(Token::Print) => { self.lexer.next(); self.require(Token::Semicolon)?; @@ -48,10 +54,16 @@ impl<'a> Parser<'a> { subtract => Subtract; multiply => Multiply; divide => Divide; + cmplt => Lt; + cmpgt => Gt; + cmpeq => Eq; + cmpneq => Neq; + logand => And; + logor => Or; } /// Ensure that input token is an expression - fn parse_expr(&mut self, token: Option) -> ExprResult { + pub(super) fn parse_expr(&mut self, token: Option) -> ExprResult { let token = token.ok_or(Error { kind: ErrorKind::EndOfTokenStream, position: self.lexer.span(), @@ -63,6 +75,10 @@ impl<'a> Parser<'a> { Token::String(s) => Ok(Expr::Literal(Value::Str(s))), Token::Aboolean(a) => Ok(Expr::Literal(Value::Abool(a))), Token::Identifier(i) => Ok(Expr::Identifier(Iden(i))), + Token::LogNot => { + let next = self.lexer.next(); + Ok(Expr::Not(Box::new(self.parse_expr(next)?))) + } Token::LeftParenthesis => self.parse_paren(), t => Err(self.unexpected_token(Some(t))), } @@ -95,7 +111,7 @@ impl<'a> Parser<'a> { self.lexer.next(); return Ok(buf); } - _ => return Ok(buf.into()), + _ => return Ok(buf), }; } }