Added T-Dark block

- obeyed clippy
This commit is contained in:
Erin 2021-05-02 17:38:12 +02:00 committed by ondra05
parent 5a8dd5051f
commit ecce080378
4 changed files with 60 additions and 14 deletions

View file

@ -18,6 +18,7 @@ pub type ParseResult = Result<Item, Error>;
pub struct Parser<'a> {
lexer: PeekableLexer<'a>,
ast: Vec<Item>,
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<Vec<Item>, 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<Vec<Item>, 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)
}
}

View file

@ -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)?)))

View file

@ -36,7 +36,11 @@ impl<'a> Parser<'a> {
/// Require an identifier on next and return it
pub(super) fn require_iden(&mut self) -> Result<Iden, Error> {
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,

View file

@ -7,9 +7,9 @@ pub enum Abool {
Always = 1,
}
impl Into<bool> for Abool {
fn into(self) -> bool {
match self {
impl From<Abool> for bool {
fn from(val: Abool) -> Self {
match val {
Abool::Never => false,
Abool::Always => true,
Abool::Sometimes => rand::thread_rng().gen(),