clippy's
This commit is contained in:
parent
e079451f0a
commit
827a401fbc
7 changed files with 131 additions and 140 deletions
|
|
@ -96,13 +96,13 @@ impl AstCompiler {
|
|||
) -> Result<Option<Statement>, CompilerErrorAtLine> {
|
||||
let expected_indent = *self.indent.last().unwrap();
|
||||
// skip empty lines
|
||||
while self.check(Eol) {
|
||||
while self.check(&Eol) {
|
||||
self.advance();
|
||||
}
|
||||
|
||||
let mut indent_on_line = 0;
|
||||
// keep track of indent level
|
||||
while self.match_token(vec![Indent]) {
|
||||
while self.match_token(&[Indent]) {
|
||||
indent_on_line += 1;
|
||||
}
|
||||
if indent_on_line > expected_indent {
|
||||
|
|
@ -116,13 +116,13 @@ impl AstCompiler {
|
|||
}
|
||||
|
||||
fn declaration(&mut self, symbol_table: &mut SymbolTable) -> Stmt {
|
||||
if self.match_token(vec![Fn]) {
|
||||
if self.match_token(&[Fn]) {
|
||||
self.function_declaration(symbol_table)
|
||||
} else if self.match_token(vec![Let]) {
|
||||
} else if self.match_token(&[Let]) {
|
||||
self.let_declaration(symbol_table)
|
||||
} else if self.match_token(vec![Object]) {
|
||||
} else if self.match_token(&[Object]) {
|
||||
self.object_declaration(symbol_table)
|
||||
} else if self.match_token(vec![TokenType::Pipe]) {
|
||||
} else if self.match_token(&[TokenType::Pipe]) {
|
||||
self.guard_declaration(symbol_table)
|
||||
} else {
|
||||
self.statement(symbol_table)
|
||||
|
|
@ -139,10 +139,10 @@ impl AstCompiler {
|
|||
}
|
||||
|
||||
fn guard_if_expr(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
while !self.check(SingleRightArrow) {
|
||||
if self.match_token(vec![Slash]) {
|
||||
while !self.check(&SingleRightArrow) {
|
||||
if self.match_token(&[Slash]) {
|
||||
return self.path_guard_expr();
|
||||
} else if self.match_token(vec![TokenType::Question]) {
|
||||
} else if self.match_token(&[TokenType::Question]) {
|
||||
return self.query_guard_expr(symbol_table);
|
||||
} else {
|
||||
return Err(self.raise(Expected("-> or ?")));
|
||||
|
|
@ -154,9 +154,9 @@ impl AstCompiler {
|
|||
}
|
||||
|
||||
fn query_guard_expr(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
if self.match_token(vec![LeftBrace]) {
|
||||
if self.match_token(&[LeftBrace]) {
|
||||
let query_params = self.expression(symbol_table)?;
|
||||
self.consume(RightBrace, Expected("'}' after guard expression."))?;
|
||||
self.consume(&RightBrace, Expected("'}' after guard expression."))?;
|
||||
Ok(query_params)
|
||||
} else {
|
||||
Ok(Stop {
|
||||
|
|
@ -166,9 +166,9 @@ impl AstCompiler {
|
|||
}
|
||||
|
||||
fn path_guard_expr(&mut self) -> Expr {
|
||||
if self.match_token(vec![LeftBrace]) {
|
||||
if self.match_token(&[LeftBrace]) {
|
||||
let path_params = self.match_expression()?;
|
||||
self.consume(RightBrace, Expected("'}' after guard expression."))?;
|
||||
self.consume(&RightBrace, Expected("'}' after guard expression."))?;
|
||||
Ok(path_params)
|
||||
} else {
|
||||
Ok(Stop {
|
||||
|
|
@ -182,15 +182,15 @@ impl AstCompiler {
|
|||
}
|
||||
|
||||
fn object_declaration(&mut self, symbol_table: &mut SymbolTable) -> Stmt {
|
||||
let type_name = self.consume(Identifier, Expected("object name."))?;
|
||||
self.consume(Colon, Expected("':' after object name."))?;
|
||||
self.consume(Eol, Expected("end of line."))?;
|
||||
let type_name = self.consume(&Identifier, Expected("object name."))?;
|
||||
self.consume(&Colon, Expected("':' after object name."))?;
|
||||
self.consume(&Eol, Expected("end of line."))?;
|
||||
|
||||
let mut fields = vec![];
|
||||
|
||||
let expected_indent = self.indent.last().unwrap() + 1;
|
||||
let mut done = false;
|
||||
while !done && !self.match_token(vec![Eof]) {
|
||||
while !done && !self.match_token(&[Eof]) {
|
||||
for _ in 0..expected_indent {
|
||||
if self.peek().token_type == Indent {
|
||||
self.advance();
|
||||
|
|
@ -199,8 +199,8 @@ impl AstCompiler {
|
|||
}
|
||||
}
|
||||
if !done {
|
||||
let field_name = self.consume(Identifier, Expected("an object field name."))?;
|
||||
self.consume(Colon, Expected("':' after field name."))?;
|
||||
let field_name = self.consume(&Identifier, Expected("an object field name."))?;
|
||||
self.consume(&Colon, Expected("':' after field name."))?;
|
||||
let field_type = self.peek().token_type.clone();
|
||||
if field_type.is_type() {
|
||||
self.advance();
|
||||
|
|
@ -213,7 +213,7 @@ impl AstCompiler {
|
|||
});
|
||||
}
|
||||
}
|
||||
self.consume(Eol, Expected("end of line."))?;
|
||||
self.consume(&Eol, Expected("end of line."))?;
|
||||
|
||||
let type_name_as_str = type_name.lexeme.clone();
|
||||
symbol_table.insert(
|
||||
|
|
@ -231,16 +231,16 @@ impl AstCompiler {
|
|||
}
|
||||
|
||||
fn function_declaration(&mut self, symbol_table: &mut SymbolTable) -> Stmt {
|
||||
let name_token = self.consume(Identifier, Expected("function name."))?;
|
||||
self.consume(LeftParen, Expected("'(' after function name."))?;
|
||||
let name_token = self.consume(&Identifier, Expected("function name."))?;
|
||||
self.consume(&LeftParen, Expected("'(' after function name."))?;
|
||||
let mut parameters = vec![];
|
||||
while !self.check(RightParen) {
|
||||
while !self.check(&RightParen) {
|
||||
if parameters.len() >= 25 {
|
||||
return Err(self.raise(TooManyParameters));
|
||||
}
|
||||
let parm_name = self.consume(Identifier, Expected("a parameter name."))?;
|
||||
let parm_name = self.consume(&Identifier, Expected("a parameter name."))?;
|
||||
|
||||
self.consume(Colon, Expected(": after parameter name"))?;
|
||||
self.consume(&Colon, Expected(": after parameter name"))?;
|
||||
let var_type = self.peek().token_type.clone();
|
||||
|
||||
self.advance();
|
||||
|
|
@ -252,15 +252,15 @@ impl AstCompiler {
|
|||
self.advance();
|
||||
}
|
||||
}
|
||||
self.consume(RightParen, Expected(" ')' after parameters."))?;
|
||||
let return_type = if self.check(SingleRightArrow) {
|
||||
self.consume(SingleRightArrow, Expected("->"))?;
|
||||
self.consume(&RightParen, Expected(" ')' after parameters."))?;
|
||||
let return_type = if self.check(&SingleRightArrow) {
|
||||
self.consume(&SingleRightArrow, Expected("->"))?;
|
||||
self.advance().token_type.clone()
|
||||
} else {
|
||||
TokenType::Void
|
||||
};
|
||||
self.consume(Colon, Expected("colon (:) after function declaration."))?;
|
||||
self.consume(Eol, Expected("end of line."))?;
|
||||
self.consume(&Colon, Expected("colon (:) after function declaration."))?;
|
||||
self.consume(&Eol, Expected("end of line."))?;
|
||||
|
||||
let current_indent = self.indent.last().unwrap();
|
||||
self.indent.push(current_indent + 1);
|
||||
|
|
@ -283,16 +283,16 @@ impl AstCompiler {
|
|||
self.peek().token_type.clone(),
|
||||
)));
|
||||
}
|
||||
let name_token = self.consume(Identifier, Expected("variable name."))?;
|
||||
let name_token = self.consume(&Identifier, Expected("variable name."))?;
|
||||
|
||||
let declared_type = if self.check(Colon) {
|
||||
let declared_type = if self.check(&Colon) {
|
||||
self.advance();
|
||||
Some(self.advance().token_type.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if self.match_token(vec![Equal]) {
|
||||
if self.match_token(&[Equal]) {
|
||||
let initializer = self.expression(symbol_table)?;
|
||||
let declared_type = declared_type.unwrap_or(Unknown);
|
||||
let inferred_type = infer_type(&initializer, symbol_table);
|
||||
|
|
@ -306,7 +306,7 @@ impl AstCompiler {
|
|||
},
|
||||
);
|
||||
|
||||
self.consume(Eol, Expected("end of line after initializer."))?;
|
||||
self.consume(&Eol, Expected("end of line after initializer."))?;
|
||||
|
||||
Ok(Statement::VarStmt {
|
||||
name: name_token,
|
||||
|
|
@ -319,7 +319,7 @@ impl AstCompiler {
|
|||
}
|
||||
|
||||
fn statement(&mut self, symbol_table: &mut SymbolTable) -> Stmt {
|
||||
if self.match_token(vec![Print]) {
|
||||
if self.match_token(&[Print]) {
|
||||
self.print_statement(symbol_table)
|
||||
} else {
|
||||
self.expr_statement(symbol_table)
|
||||
|
|
@ -328,14 +328,14 @@ impl AstCompiler {
|
|||
|
||||
fn print_statement(&mut self, symbol_table: &mut SymbolTable) -> Stmt {
|
||||
let expr = self.expression(symbol_table)?;
|
||||
self.consume(Eol, Expected("end of line after print statement."))?;
|
||||
self.consume(&Eol, Expected("end of line after print statement."))?;
|
||||
Ok(Statement::PrintStmt { value: expr })
|
||||
}
|
||||
|
||||
fn expr_statement(&mut self, symbol_table: &mut SymbolTable) -> Stmt {
|
||||
let expr = self.expression(symbol_table)?;
|
||||
if !self.is_at_end() {
|
||||
self.consume(Eol, Expected("end of line after expression."))?;
|
||||
self.consume(&Eol, Expected("end of line after expression."))?;
|
||||
}
|
||||
Ok(Statement::ExpressionStmt { expression: expr })
|
||||
}
|
||||
|
|
@ -346,33 +346,33 @@ impl AstCompiler {
|
|||
|
||||
fn or(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let expr = self.and(symbol_table)?;
|
||||
self.binary(vec![TokenType::LogicalOr], expr, symbol_table)
|
||||
self.binary(&[TokenType::LogicalOr], expr, symbol_table)
|
||||
}
|
||||
|
||||
fn and(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let expr = self.bit_and(symbol_table)?;
|
||||
self.binary(vec![TokenType::LogicalAnd], expr, symbol_table)
|
||||
self.binary(&[TokenType::LogicalAnd], expr, symbol_table)
|
||||
}
|
||||
|
||||
fn bit_and(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let expr = self.bit_or(symbol_table)?;
|
||||
self.binary(vec![TokenType::BitAnd], expr, symbol_table)
|
||||
self.binary(&[TokenType::BitAnd], expr, symbol_table)
|
||||
}
|
||||
|
||||
fn bit_or(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let expr = self.bit_xor(symbol_table)?;
|
||||
self.binary(vec![TokenType::Pipe], expr, symbol_table)
|
||||
self.binary(&[TokenType::Pipe], expr, symbol_table)
|
||||
}
|
||||
|
||||
fn bit_xor(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let expr = self.equality(symbol_table)?;
|
||||
self.binary(vec![TokenType::BitXor], expr, symbol_table)
|
||||
self.binary(&[TokenType::BitXor], expr, symbol_table)
|
||||
}
|
||||
|
||||
fn equality(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let expr = self.comparison(symbol_table)?;
|
||||
self.binary(
|
||||
vec![TokenType::EqualEqual, TokenType::BangEqual],
|
||||
&[TokenType::EqualEqual, TokenType::BangEqual],
|
||||
expr,
|
||||
symbol_table,
|
||||
)
|
||||
|
|
@ -381,7 +381,7 @@ impl AstCompiler {
|
|||
fn comparison(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let expr = self.bitshift(symbol_table)?;
|
||||
self.binary(
|
||||
vec![Greater, GreaterEqual, Less, LessEqual],
|
||||
&[Greater, GreaterEqual, Less, LessEqual],
|
||||
expr,
|
||||
symbol_table,
|
||||
)
|
||||
|
|
@ -389,26 +389,26 @@ impl AstCompiler {
|
|||
|
||||
fn bitshift(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let expr = self.term(symbol_table)?;
|
||||
self.binary(vec![GreaterGreater, LessLess], expr, symbol_table)
|
||||
self.binary(&[GreaterGreater, LessLess], expr, symbol_table)
|
||||
}
|
||||
|
||||
fn term(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let expr = self.factor(symbol_table)?;
|
||||
self.binary(vec![Minus, Plus], expr, symbol_table)
|
||||
self.binary(&[Minus, Plus], expr, symbol_table)
|
||||
}
|
||||
|
||||
fn factor(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let expr = self.unary(symbol_table)?;
|
||||
self.binary(vec![Slash, Star], expr, symbol_table)
|
||||
self.binary(&[Slash, Star], expr, symbol_table)
|
||||
}
|
||||
|
||||
fn binary(
|
||||
&mut self,
|
||||
types: Vec<TokenType>,
|
||||
types: &[TokenType],
|
||||
mut expr: Expression,
|
||||
symbol_table: &mut SymbolTable,
|
||||
) -> Expr {
|
||||
while self.match_token(types.clone()) {
|
||||
while self.match_token(&types) {
|
||||
let operator = self.previous().clone();
|
||||
let right = self.comparison(symbol_table)?;
|
||||
expr = Expression::Binary {
|
||||
|
|
@ -422,7 +422,7 @@ impl AstCompiler {
|
|||
}
|
||||
|
||||
fn unary(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
if self.match_token(vec![Bang, Minus]) {
|
||||
if self.match_token(&[Bang, Minus]) {
|
||||
let operator = self.previous().clone();
|
||||
let right = self.unary(symbol_table)?;
|
||||
Ok(Expression::Unary {
|
||||
|
|
@ -439,14 +439,14 @@ impl AstCompiler {
|
|||
fn get(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let expr = self.primary(symbol_table)?;
|
||||
|
||||
if self.match_token(vec![LeftParen]) {
|
||||
if self.match_token(&[LeftParen]) {
|
||||
let name = self.peek().clone();
|
||||
self.advance();
|
||||
self.function_call(name, symbol_table)
|
||||
} else if self.match_token(vec![LeftBracket]) {
|
||||
} else if self.match_token(&[LeftBracket]) {
|
||||
let index = self.expression(symbol_table)?;
|
||||
self.index(expr, index)
|
||||
} else if self.match_token(vec![Dot]) {
|
||||
} else if self.match_token(&[Dot]) {
|
||||
let name = self.peek().clone();
|
||||
self.advance();
|
||||
self.field_or_method(expr, name, symbol_table)
|
||||
|
|
@ -480,7 +480,7 @@ impl AstCompiler {
|
|||
},
|
||||
_ => return Err(self.raise(CompilerError::IllegalTypeToIndex("Unknown".to_string()))),
|
||||
};
|
||||
self.consume(RightBracket, Expected("']' after index."))?;
|
||||
self.consume(&RightBracket, Expected("']' after index."))?;
|
||||
Ok(get)
|
||||
}
|
||||
|
||||
|
|
@ -491,7 +491,7 @@ impl AstCompiler {
|
|||
op: Token,
|
||||
symbol_table: &mut SymbolTable,
|
||||
) -> Expr {
|
||||
if self.match_token(vec![LeftParen]) {
|
||||
if self.match_token(&[LeftParen]) {
|
||||
let arguments = self.arguments(symbol_table)?;
|
||||
Ok(MethodCall {
|
||||
receiver: Box::new(receiver.clone()),
|
||||
|
|
@ -509,23 +509,23 @@ impl AstCompiler {
|
|||
}
|
||||
|
||||
fn primary(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
Ok(if self.match_token(vec![LeftBracket]) {
|
||||
Ok(if self.match_token(&[LeftBracket]) {
|
||||
self.list(symbol_table)?
|
||||
} else if self.match_token(vec![LeftBrace]) {
|
||||
} else if self.match_token(&[LeftBrace]) {
|
||||
self.map(symbol_table)?
|
||||
} else if self.match_token(vec![False]) {
|
||||
} else if self.match_token(&[False]) {
|
||||
Expression::Literal {
|
||||
line: self.peek().line,
|
||||
literaltype: Bool,
|
||||
value: Value::Bool(false),
|
||||
}
|
||||
} else if self.match_token(vec![True]) {
|
||||
} else if self.match_token(&[True]) {
|
||||
Expression::Literal {
|
||||
line: self.peek().line,
|
||||
literaltype: Bool,
|
||||
value: Value::Bool(true),
|
||||
} //, FloatingPoint, Text
|
||||
} else if self.match_token(vec![Integer]) {
|
||||
} else if self.match_token(&[Integer]) {
|
||||
Expression::Literal {
|
||||
line: self.peek().line,
|
||||
literaltype: Integer,
|
||||
|
|
@ -536,7 +536,7 @@ impl AstCompiler {
|
|||
.map_err(|e| self.raise(ParseError(format!("{:?}", e))))?,
|
||||
),
|
||||
}
|
||||
} else if self.match_token(vec![U32]) {
|
||||
} else if self.match_token(&[U32]) {
|
||||
Expression::Literal {
|
||||
line: self.peek().line,
|
||||
literaltype: Integer,
|
||||
|
|
@ -545,7 +545,7 @@ impl AstCompiler {
|
|||
.map_err(|e| self.raise(ParseError(format!("{:?}", e))))?,
|
||||
),
|
||||
}
|
||||
} else if self.match_token(vec![U64]) {
|
||||
} else if self.match_token(&[U64]) {
|
||||
Expression::Literal {
|
||||
line: self.peek().line,
|
||||
literaltype: Integer,
|
||||
|
|
@ -554,7 +554,7 @@ impl AstCompiler {
|
|||
.map_err(|e| self.raise(ParseError(format!("{:?}", e))))?,
|
||||
),
|
||||
}
|
||||
} else if self.match_token(vec![FloatingPoint]) {
|
||||
} else if self.match_token(&[FloatingPoint]) {
|
||||
Expression::Literal {
|
||||
line: self.peek().line,
|
||||
literaltype: FloatingPoint,
|
||||
|
|
@ -565,19 +565,19 @@ impl AstCompiler {
|
|||
.map_err(|e| self.raise(ParseError(format!("{:?}", e))))?,
|
||||
),
|
||||
}
|
||||
} else if self.match_token(vec![StringType]) {
|
||||
} else if self.match_token(&[StringType]) {
|
||||
Expression::Literal {
|
||||
line: self.peek().line,
|
||||
literaltype: StringType,
|
||||
value: Value::String(self.previous().lexeme.clone()),
|
||||
}
|
||||
} else if self.match_token(vec![Char]) {
|
||||
} else if self.match_token(&[Char]) {
|
||||
Expression::Literal {
|
||||
line: self.peek().line,
|
||||
literaltype: Char,
|
||||
value: Value::Char(self.previous().lexeme.chars().next().unwrap()),
|
||||
}
|
||||
} else if self.match_token(vec![DateTime]) {
|
||||
} else if self.match_token(&[DateTime]) {
|
||||
Expression::Literal {
|
||||
line: self.peek().line,
|
||||
literaltype: DateTime,
|
||||
|
|
@ -590,9 +590,9 @@ impl AstCompiler {
|
|||
.into(),
|
||||
)),
|
||||
}
|
||||
} else if self.match_token(vec![LeftParen]) {
|
||||
} else if self.match_token(&[LeftParen]) {
|
||||
let expr = self.expression(symbol_table)?;
|
||||
self.consume(RightParen, Expected("')' after expression."))?;
|
||||
self.consume(&RightParen, Expected("')' after expression."))?;
|
||||
Expression::Grouping {
|
||||
line: self.peek().line,
|
||||
expression: Box::new(expr),
|
||||
|
|
@ -600,9 +600,9 @@ impl AstCompiler {
|
|||
} else {
|
||||
let token = self.advance().clone();
|
||||
debug!("{:?}", token);
|
||||
if self.match_token(vec![LeftParen]) {
|
||||
if self.match_token(&[LeftParen]) {
|
||||
self.function_call(token.clone(), symbol_table)?
|
||||
} else if self.match_token(vec![Colon]) {
|
||||
} else if self.match_token(&[Colon]) {
|
||||
self.named_parameter(&token, symbol_table)?
|
||||
} else {
|
||||
self.variable_lookup(&token, symbol_table)?
|
||||
|
|
@ -622,12 +622,12 @@ impl AstCompiler {
|
|||
|
||||
fn list(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let mut list = vec![];
|
||||
while !self.match_token(vec![RightBracket]) {
|
||||
while !self.match_token(&[RightBracket]) {
|
||||
list.push(self.expression(symbol_table)?);
|
||||
if self.peek().token_type == TokenType::Comma {
|
||||
self.advance();
|
||||
} else {
|
||||
self.consume(RightBracket, Expected("']' at the end of the list."))?;
|
||||
self.consume(&RightBracket, Expected("']' at the end of the list."))?;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -640,15 +640,15 @@ impl AstCompiler {
|
|||
|
||||
fn map(&mut self, symbol_table: &mut SymbolTable) -> Expr {
|
||||
let mut entries = vec![];
|
||||
while !self.match_token(vec![RightBrace]) {
|
||||
while !self.match_token(&[RightBrace]) {
|
||||
let key = self.expression(symbol_table)?;
|
||||
self.consume(Colon, Expected("':' after map key."))?;
|
||||
self.consume(&Colon, Expected("':' after map key."))?;
|
||||
let value = self.expression(symbol_table)?;
|
||||
entries.push((key, value));
|
||||
if self.peek().token_type == TokenType::Comma {
|
||||
self.advance();
|
||||
} else {
|
||||
self.consume(RightBrace, Expected("'}' after map."))?;
|
||||
self.consume(&RightBrace, Expected("'}' after map."))?;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -687,7 +687,7 @@ impl AstCompiler {
|
|||
symbol_table: &mut SymbolTable,
|
||||
) -> Result<Vec<Expression>, CompilerErrorAtLine> {
|
||||
let mut arguments = vec![];
|
||||
while !self.match_token(vec![RightParen]) {
|
||||
while !self.match_token(&[RightParen]) {
|
||||
if arguments.len() >= 25 {
|
||||
return Err(self.raise(TooManyParameters));
|
||||
}
|
||||
|
|
@ -696,7 +696,7 @@ impl AstCompiler {
|
|||
if self.peek().token_type == TokenType::Comma {
|
||||
self.advance();
|
||||
} else {
|
||||
self.consume(RightParen, Expected("')' after arguments."))?;
|
||||
self.consume(&RightParen, Expected("')' after arguments."))?;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -705,7 +705,7 @@ impl AstCompiler {
|
|||
|
||||
fn consume(
|
||||
&mut self,
|
||||
token_type: TokenType,
|
||||
token_type: &TokenType,
|
||||
message: CompilerError,
|
||||
) -> Result<Token, CompilerErrorAtLine> {
|
||||
if self.check(token_type) {
|
||||
|
|
@ -717,7 +717,7 @@ impl AstCompiler {
|
|||
Ok(self.previous().clone())
|
||||
}
|
||||
|
||||
fn match_token(&mut self, tokens: Vec<TokenType>) -> bool {
|
||||
fn match_token(&mut self, tokens: &[TokenType]) -> bool {
|
||||
for tt in tokens {
|
||||
if self.check(tt) {
|
||||
self.advance();
|
||||
|
|
@ -727,11 +727,11 @@ impl AstCompiler {
|
|||
false
|
||||
}
|
||||
|
||||
fn check(&self, token_type: TokenType) -> bool {
|
||||
fn check(&self, token_type: &TokenType) -> bool {
|
||||
if self.is_at_end() {
|
||||
false
|
||||
} else {
|
||||
self.peek().token_type == token_type
|
||||
&self.peek().token_type == token_type
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use crate::ast_compiler::Expression::NamedParameter;
|
||||
use crate::ast_compiler::{Expression, Function, Parameter, Statement};
|
||||
use crate::chunk::Chunk;
|
||||
use crate::errors::CompilerError::{IncompatibleTypes, UndeclaredVariable};
|
||||
use crate::errors::{CompilerError, CompilerErrorAtLine};
|
||||
use crate::symbol_builder::{Symbol, calculate_type, infer_type};
|
||||
use crate::tokens::TokenType;
|
||||
|
|
@ -116,19 +117,19 @@ impl Compiler {
|
|||
let calculated_type =
|
||||
calculate_type(var_type, &inferred_type).map_err(|e| self.raise(e))?;
|
||||
if var_type != &Unknown && var_type != &calculated_type {
|
||||
return Err(self.raise(CompilerError::IncompatibleTypes(
|
||||
var_type.clone(),
|
||||
calculated_type,
|
||||
)));
|
||||
return Err(
|
||||
self.raise(IncompatibleTypes(var_type.clone(), calculated_type))
|
||||
);
|
||||
}
|
||||
let name_index = self.chunk.add_var(var_type, name);
|
||||
self.vars.insert(name.to_string(), name_index);
|
||||
self.compile_expression(namespace, initializer, symbols, registry)?;
|
||||
self.emit_bytes(OP_ASSIGN, name_index as u16);
|
||||
} else {
|
||||
return Err(self.raise(CompilerError::UndeclaredVariable(name.to_string())));
|
||||
return Err(self.raise(UndeclaredVariable(name.to_string())));
|
||||
}
|
||||
}
|
||||
// replace with function
|
||||
Statement::PrintStmt { value } => {
|
||||
self.compile_expression(namespace, value, symbols, registry)?;
|
||||
self.emit_byte(OP_PRINT);
|
||||
|
|
@ -217,7 +218,7 @@ impl Compiler {
|
|||
self.emit_byte(type_index as u16);
|
||||
self.emit_byte(arguments.len() as u16);
|
||||
}
|
||||
Expression::Variable { name, line, .. } => {
|
||||
Expression::Variable { name, .. } => {
|
||||
let name_index = self.vars.get(name);
|
||||
if let Some(name_index) = name_index {
|
||||
self.emit_bytes(OP_GET, *name_index as u16);
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ pub fn compile_sourcedir(source_dir: &str) -> Result<HashMap<String, Chunk>, Cru
|
|||
let source = fs::read_to_string(path).map_err(map_underlying())?;
|
||||
let tokens = scan(&source)?;
|
||||
let mut symbol_table = HashMap::new();
|
||||
match ast_compiler::compile(Some(&path), tokens, &mut symbol_table) {
|
||||
match ast_compiler::compile(Some(path), tokens, &mut symbol_table) {
|
||||
Ok(statements) => {
|
||||
let path = path.strip_prefix(source_dir).unwrap().replace(".crud", "");
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ pub enum Symbol {
|
|||
}
|
||||
|
||||
fn make_qname(path: &str, name: &Token) -> String {
|
||||
if path == "" {
|
||||
if path.is_empty() {
|
||||
name.lexeme.to_string()
|
||||
} else {
|
||||
format!("{}.{}", path, name.lexeme)
|
||||
|
|
@ -40,16 +40,10 @@ pub fn build(path: &str, ast: &[Statement], symbols: &mut HashMap<String, Symbol
|
|||
match statement {
|
||||
Statement::VarStmt { name, var_type, .. } => {
|
||||
let key = make_qname(path, name);
|
||||
if !symbols.contains_key(&key) {
|
||||
// surely there's a better way to do this?
|
||||
symbols.insert(
|
||||
key,
|
||||
Symbol::Variable {
|
||||
symbols.entry(key).or_insert_with(|| Symbol::Variable {
|
||||
name: name.lexeme.to_string(),
|
||||
var_type: var_type.clone(),
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
Statement::FunctionStmt { function } => {
|
||||
symbols.insert(
|
||||
|
|
@ -154,7 +148,7 @@ pub fn infer_type(expr: &Expression, symbols: &HashMap<String, Symbol>) -> Token
|
|||
} => {
|
||||
let left_type = infer_type(left, symbols);
|
||||
let right_type = infer_type(right, symbols);
|
||||
if vec![Greater, Less, GreaterEqual, LessEqual].contains(&operator.token_type) {
|
||||
if [Greater, Less, GreaterEqual, LessEqual].contains(&operator.token_type) {
|
||||
Bool
|
||||
} else if left_type == right_type {
|
||||
// map to determined numeric type if yet undetermined (32 or 64 bits)
|
||||
|
|
@ -225,10 +219,7 @@ pub fn infer_type(expr: &Expression, symbols: &HashMap<String, Symbol>) -> Token
|
|||
_ => Unknown,
|
||||
}
|
||||
}
|
||||
Expression::MethodCall {
|
||||
receiver,
|
||||
..
|
||||
} => infer_type(receiver, symbols),
|
||||
Expression::MethodCall { receiver, .. } => infer_type(receiver, symbols),
|
||||
Expression::Stop { .. } => TokenType::Unknown,
|
||||
// Expression::PathMatch { .. } => TokenType::Unknown,
|
||||
Expression::NamedParameter { .. } => TokenType::Unknown,
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ pub enum TokenType {
|
|||
Unknown,
|
||||
Void,
|
||||
While,
|
||||
ObjectType(String)
|
||||
ObjectType(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for TokenType {
|
||||
|
|
@ -162,7 +162,6 @@ impl fmt::Display for TokenType {
|
|||
TokenType::While => write!(f, "while"),
|
||||
TokenType::SignedInteger => write!(f, "i32/64"),
|
||||
TokenType::UnsignedInteger => write!(f, "u32/64"),
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -171,7 +170,8 @@ impl Eq for TokenType {}
|
|||
|
||||
impl TokenType {
|
||||
pub(crate) fn is_type(&self) -> bool {
|
||||
match self {
|
||||
matches!(
|
||||
self,
|
||||
TokenType::I32
|
||||
| TokenType::I64
|
||||
| TokenType::U32
|
||||
|
|
@ -183,8 +183,7 @@ impl TokenType {
|
|||
| TokenType::Object
|
||||
| TokenType::ListType
|
||||
| TokenType::MapType
|
||||
| TokenType::Char => true,
|
||||
_ => false,
|
||||
}
|
||||
| TokenType::Char
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
32
src/value.rs
32
src/value.rs
|
|
@ -163,22 +163,22 @@ impl Into<Value> for DateTime<Utc> {
|
|||
impl Display for Value {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match &self {
|
||||
&Value::U32(v) => write!(f, "{}", v),
|
||||
&Value::U64(v) => write!(f, "{}", v),
|
||||
&Value::String(v) => write!(f, "{}", v),
|
||||
&Value::Bool(v) => write!(f, "{}", v),
|
||||
&Value::I32(v) => write!(f, "{}", v),
|
||||
&Value::I64(v) => write!(f, "{}", v),
|
||||
&Value::F32(v) => write!(f, "{}", v),
|
||||
&Value::F64(v) => write!(f, "{}", v),
|
||||
&Value::Char(v) => write!(f, "{}", v),
|
||||
&Value::DateTime(v) => write!(f, "{}", v),
|
||||
&Value::Enum => write!(f, "enum"),
|
||||
&Value::ObjectType(o) => write!(f, "{}: {:?}", o.definition, o.fields),
|
||||
&Value::List(v) => write!(f, "{:?}", v),
|
||||
&Value::Map(map) => to_string(f, map),
|
||||
&Value::Error(v) => write!(f, "{}", v),
|
||||
&Value::Void => write!(f, "()"),
|
||||
Value::U32(v) => write!(f, "{}", v),
|
||||
Value::U64(v) => write!(f, "{}", v),
|
||||
Value::String(v) => write!(f, "{}", v),
|
||||
Value::Bool(v) => write!(f, "{}", v),
|
||||
Value::I32(v) => write!(f, "{}", v),
|
||||
Value::I64(v) => write!(f, "{}", v),
|
||||
Value::F32(v) => write!(f, "{}", v),
|
||||
Value::F64(v) => write!(f, "{}", v),
|
||||
Value::Char(v) => write!(f, "{}", v),
|
||||
Value::DateTime(v) => write!(f, "{}", v),
|
||||
Value::Enum => write!(f, "enum"),
|
||||
Value::ObjectType(o) => write!(f, "{}: {:?}", o.definition, o.fields),
|
||||
Value::List(v) => write!(f, "{:?}", v),
|
||||
Value::Map(map) => to_string(f, map),
|
||||
Value::Error(v) => write!(f, "{}", v),
|
||||
Value::Void => write!(f, "()"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ pub async fn interpret_async(
|
|||
.insert("query".to_string(), Value::Map(value_map(query_params)));
|
||||
vm.local_vars
|
||||
.insert("headers".to_string(), Value::Map(value_map(headers)));
|
||||
vm.run(&get_context(function), &chunk)
|
||||
vm.run(&get_context(function), chunk)
|
||||
} else {
|
||||
Err(RuntimeError::FunctionNotFound(function.to_string()))
|
||||
}
|
||||
|
|
@ -89,7 +89,7 @@ impl Vm {
|
|||
for (_, name) in chunk.vars.iter() {
|
||||
self.local_vars.insert(name.clone(), args.remove(0));
|
||||
}
|
||||
self.run("", &chunk)
|
||||
self.run("", chunk)
|
||||
}
|
||||
|
||||
fn run(&mut self, context: &str, chunk: &Chunk) -> Result<Value, RuntimeError> {
|
||||
|
|
@ -251,7 +251,7 @@ impl Vm {
|
|||
let mut fields = vec![];
|
||||
params
|
||||
.iter()
|
||||
.zip(args.into_iter())
|
||||
.zip(args)
|
||||
.for_each(|(param, arg)| {
|
||||
fields.push((param.name.lexeme.clone(), arg))
|
||||
});
|
||||
|
|
@ -303,7 +303,7 @@ fn binary_op(vm: &mut Vm, op: impl Fn(&Value, &Value) -> Result<Value, ValueErro
|
|||
Ok(result) => vm.push(result),
|
||||
Err(e) => {
|
||||
vm.error_occurred = true;
|
||||
println!("Error: {} {:?} and {:?}", e.to_string(), a, b);
|
||||
println!("Error: {} {:?} and {:?}", e, a, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue