return expressions working
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
mod tree_node;
|
mod tree_node;
|
||||||
|
|
||||||
use crate::tokenizer::{
|
use crate::tokenizer::{
|
||||||
token::{Keyword, Symbol, Token, TokenType},
|
token::{self, Keyword, Symbol, Token, TokenType},
|
||||||
Tokenizer, TokenizerBuffer, TokenizerError,
|
Tokenizer, TokenizerBuffer, TokenizerError,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
@@ -518,11 +518,25 @@ impl Parser {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
while !self_matches_peek!(self, TokenType::Symbol(Symbol::RBrace)) {
|
while !self_matches_peek!(
|
||||||
|
self,
|
||||||
|
TokenType::Symbol(Symbol::RBrace) | TokenType::Keyword(Keyword::Return)
|
||||||
|
) {
|
||||||
let expression = self.parse()?.ok_or(ParseError::UnexpectedEOF)?;
|
let expression = self.parse()?.ok_or(ParseError::UnexpectedEOF)?;
|
||||||
expressions.push(expression);
|
expressions.push(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// print the current token for debugging
|
||||||
|
let current_token = token_from_option!(self.get_next()?);
|
||||||
|
|
||||||
|
if token_matches!(current_token, TokenType::Keyword(Keyword::Return)) {
|
||||||
|
self.assign_next()?;
|
||||||
|
let expression = self.expression()?.ok_or(ParseError::UnexpectedEOF)?;
|
||||||
|
let return_expr = Expression::ReturnExpression(Box::new(expression));
|
||||||
|
expressions.push(return_expr);
|
||||||
|
self.assign_next()?;
|
||||||
|
}
|
||||||
|
|
||||||
self.assign_next()?;
|
self.assign_next()?;
|
||||||
|
|
||||||
Ok(BlockExpression(expressions))
|
Ok(BlockExpression(expressions))
|
||||||
|
|||||||
@@ -138,32 +138,20 @@ impl std::fmt::Display for InvocationExpression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
|
||||||
pub struct PropertyAccessorExpression {
|
|
||||||
pub object: Box<Expression>,
|
|
||||||
pub property: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for PropertyAccessorExpression {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "{}.{}", self.object, self.property)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum Expression {
|
pub enum Expression {
|
||||||
Literal(Literal),
|
|
||||||
Variable(String),
|
|
||||||
Negation(Box<Expression>),
|
|
||||||
BinaryExpression(BinaryExpression),
|
|
||||||
LogicalExpression(LogicalExpression),
|
|
||||||
AssignmentExpression(AssignmentExpression),
|
AssignmentExpression(AssignmentExpression),
|
||||||
|
BinaryExpression(BinaryExpression),
|
||||||
|
BlockExpression(BlockExpression),
|
||||||
DeclarationExpression(String, Box<Expression>),
|
DeclarationExpression(String, Box<Expression>),
|
||||||
FunctionExpression(FunctionExpression),
|
FunctionExpression(FunctionExpression),
|
||||||
BlockExpression(BlockExpression),
|
|
||||||
InvocationExpression(InvocationExpression),
|
InvocationExpression(InvocationExpression),
|
||||||
|
Literal(Literal),
|
||||||
|
LogicalExpression(LogicalExpression),
|
||||||
|
Negation(Box<Expression>),
|
||||||
PriorityExpression(Box<Expression>),
|
PriorityExpression(Box<Expression>),
|
||||||
PropertyAccessorExpression(PropertyAccessorExpression)
|
ReturnExpression(Box<Expression>),
|
||||||
|
Variable(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Expression {
|
impl std::fmt::Display for Expression {
|
||||||
@@ -180,7 +168,7 @@ impl std::fmt::Display for Expression {
|
|||||||
Expression::InvocationExpression(e) => write!(f, "{}", e),
|
Expression::InvocationExpression(e) => write!(f, "{}", e),
|
||||||
Expression::Variable(id) => write!(f, "{}", id),
|
Expression::Variable(id) => write!(f, "{}", id),
|
||||||
Expression::PriorityExpression(e) => write!(f, "({})", e),
|
Expression::PriorityExpression(e) => write!(f, "({})", e),
|
||||||
Expression::PropertyAccessorExpression(e) => write!(f, "{}", e),
|
Expression::ReturnExpression(e) => write!(f, "(return {})", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,7 @@
|
|||||||
fn myPowerItem(arg) {
|
fn doStuff() {
|
||||||
|
let i = {
|
||||||
}
|
return 234.5;
|
||||||
|
};
|
||||||
|
|
||||||
fn doThings(x, y, z) {
|
return i;
|
||||||
let item = myPowerItem((12.45 + 5 * 123) ** 2);
|
}
|
||||||
|
|
||||||
let item2 = 5 + 2 - 5 + 123.24323 / 234 ** 21 - (15 / 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
doThings(1, 2, 3);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user