From f8db60bc7c64b9c957ab55f8c3bc2f1f9840de52 Mon Sep 17 00:00:00 2001 From: Erin Date: Sun, 2 May 2021 17:38:12 +0200 Subject: [PATCH] Added T-Dark block - obeyed clippy --- src/parser/mod.rs | 50 +++++++++++++++++++++++++++++++++++++-------- src/parser/ops.rs | 12 +++++++++-- src/parser/utils.rs | 6 +++++- src/variables.rs | 6 +++--- 4 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 9d31559..d58ed00 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -18,6 +18,7 @@ pub type ParseResult = Result; pub struct Parser<'a> { lexer: PeekableLexer<'a>, ast: Vec, + tdark: bool, } impl<'a> Parser<'a> { @@ -26,20 +27,26 @@ impl<'a> Parser<'a> { Self { lexer: PeekableLexer::lexer(source), ast: Vec::new(), + tdark: false, } } pub fn init(&mut self) -> Result, Error> { loop { let token = self.lexer.next(); - if token.is_none() { - return Ok(self.ast.clone()); - }; - if matches!(token, Some(Token::Comment)) { - continue; + + match token { + Some(Token::Comment) => continue, + Some(Token::TDark) => { + let mut block = self.tdark_block()?; + self.ast.append(&mut block); + } + None => return Ok(self.ast.clone()), + _ => { + let item = self.parse_item(token)?; + self.ast.push(item); + } } - let item = self.parse_item(token)?; - self.ast.push(item); } } @@ -169,7 +176,7 @@ impl<'a> Parser<'a> { Ok(Stmt::BfFDeclaration { iden, body }.into()) } - /// Parse If-expression + /// Parse If-stmt pub fn if_cond(&mut self) -> ParseResult { self.require(Token::LeftParenthesis)?; let cond = self.lexer.next(); @@ -186,4 +193,31 @@ impl<'a> Parser<'a> { } .into()) } + + /// T-Dark block parsing + pub fn tdark_block(&mut self) -> Result, Error> { + self.require(Token::LeftBrace)?; + self.tdark = true; + let mut body = Vec::new(); + loop { + let token = { + match self.lexer.next() { + Some(t) => t, + None => { + return Err(Error { + kind: ErrorKind::EndOfTokenStream, + position: self.lexer.span(), + }) + } + } + }; + + if token == Token::RightBrace { + break; + } + body.push(self.parse_item(Some(token))?); + } + self.tdark = false; + Ok(body) + } } diff --git a/src/parser/ops.rs b/src/parser/ops.rs index 1829b86..c400925 100644 --- a/src/parser/ops.rs +++ b/src/parser/ops.rs @@ -72,9 +72,17 @@ impl<'a> Parser<'a> { match token { Token::Boolean(b) => Ok(Expr::Literal(Value::Bool(b))), Token::Integer(i) => Ok(Expr::Literal(Value::Int(i))), - Token::String(s) => Ok(Expr::Literal(Value::Str(s))), + Token::String(s) => Ok(Expr::Literal(Value::Str(if self.tdark { + s.replace("lang", "script") + } else { + s + }))), Token::Aboolean(a) => Ok(Expr::Literal(Value::Abool(a))), - Token::Identifier(i) => Ok(Expr::Identifier(Iden(i))), + Token::Identifier(i) => Ok(Expr::Identifier(Iden(if self.tdark { + i.replace("lang", "script") + } else { + i + }))), Token::LogNot => { let next = self.lexer.next(); Ok(Expr::Not(Box::new(self.parse_expr(next)?))) diff --git a/src/parser/utils.rs b/src/parser/utils.rs index 1706fbe..451bc18 100644 --- a/src/parser/utils.rs +++ b/src/parser/utils.rs @@ -36,7 +36,11 @@ impl<'a> Parser<'a> { /// Require an identifier on next and return it pub(super) fn require_iden(&mut self) -> Result { if let Some(Token::Identifier(id)) = self.lexer.next() { - Ok(Iden(id)) + if self.tdark { + Ok(Iden(id.replace("lang", "script"))) + } else { + Ok(Iden(id)) + } } else { Err(Error { kind: ErrorKind::InvalidIdentifier, diff --git a/src/variables.rs b/src/variables.rs index fd4a57c..10927c1 100644 --- a/src/variables.rs +++ b/src/variables.rs @@ -7,9 +7,9 @@ pub enum Abool { Always = 1, } -impl Into for Abool { - fn into(self) -> bool { - match self { +impl From for bool { + fn from(val: Abool) -> Self { + match val { Abool::Never => false, Abool::Always => true, Abool::Sometimes => rand::thread_rng().gen(),