Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tahadostifam committed Feb 28, 2025
1 parent 2185321 commit a516c24
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 91 deletions.
4 changes: 1 addition & 3 deletions examples/main.cyr
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
for #i = 0; i < 10; i++ {

}
print(0 + 1, 5, 3, sample())
127 changes: 60 additions & 67 deletions parser/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<'a> Parser<'a> {
match self.parse_infix_expression(left.clone(), left_start) {
Some(infix) => {
left = infix?;

if let Expression::Infix(b) = left.clone() {
left_start = b.span.start;
}
Expand All @@ -43,7 +43,9 @@ impl<'a> Parser<'a> {
// REVIEW
if !(self.current_token_is(TokenKind::Semicolon)
|| self.current_token_is(TokenKind::LeftBrace)
|| self.current_token_is(TokenKind::RightBracket))
|| self.current_token_is(TokenKind::RightBracket)
|| self.current_token_is(TokenKind::Comma)
)
{
self.next_token();
}
Expand Down Expand Up @@ -86,11 +88,6 @@ impl<'a> Parser<'a> {
Expression::ArrayIndex(array_index)
} else if self.current_token_is(TokenKind::LeftBrace) {
return self.parse_struct_init(from_package);
} else if self.current_token_is(TokenKind::LeftParen) {
return self.parse_field_access_or_method_call(
ast::ast::Expression::FromPackage(from_package.clone()),
from_package.span.start,
);
} else {
return Ok(Expression::FromPackage(from_package));
}
Expand Down Expand Up @@ -241,8 +238,7 @@ impl<'a> Parser<'a> {
}
TokenKind::LeftParen => {
self.next_token(); // consume the identifier token
let expr = self.parse_field_access_or_method_call(left, left_start);
Some(expr)
Some(self.parse_field_access_or_method_call(left, left_start))
}
_ => None,
}
Expand Down Expand Up @@ -290,20 +286,19 @@ impl<'a> Parser<'a> {
}
self.next_token(); // consume left brace

let first_argument = self.parse_expression(Precedence::Lowest)?.0;
series.push(first_argument);

while self.current_token_is(TokenKind::Comma) {
self.next_token();
loop {
let expr = self.parse_expression(Precedence::Lowest)?.0;
series.push(expr);

if self.current_token_is(TokenKind::RightBracket) {
if !self.current_token_is(TokenKind::RightParen) {
dbg!(self.current_token.kind.clone());
self.expect_current(TokenKind::Comma)?;
} else {
self.next_token(); // consume right paren
break;
}

let expr = self.parse_expression(Precedence::Lowest)?.0;
series.push(expr);
}

if !(self.current_token_is(end.clone()) || self.current_token_is(TokenKind::EOF)) {
return Err(CompileTimeError {
location: self.current_location(),
Expand Down Expand Up @@ -372,69 +367,65 @@ impl<'a> Parser<'a> {
field_access: None,
}];

while self.current_token_is(TokenKind::Dot) {
self.next_token(); // consume dot

let identifier = match self.current_token.kind.clone() {
TokenKind::Identifier { name } => Identifier {
name,
span: self.current_token.span.clone(),
loc: self.current_location(),
},
_ => {
return Err(CompileTimeError {
location: self.current_location(),
etype: ParserErrorType::ExpectedIdentifier,
file_name: Some(self.lexer.file_name.clone()),
code_raw: Some(self.lexer.select(left_start..self.current_token.span.end)),
verbose: None,
caret: true,
});
}
};
self.next_token();

if self.current_token_is(TokenKind::LeftParen) {
chains.push(FieldAccessOrMethodCall {
method_call: Some(self.parse_func_call(ast::ast::Expression::Identifier(identifier), left_start)?),
field_access: None,
});
} else {
chains.push(FieldAccessOrMethodCall {
method_call: None,
field_access: Some(FieldAccess {
identifier,
span: self.current_token.span.clone(),
loc: self.current_location(),
}),
});
}
}
// while self.current_token_is(TokenKind::Dot) {
// self.next_token(); // consume dot

// let identifier = match self.current_token.kind.clone() {
// TokenKind::Identifier { name } => Identifier {
// name,
// span: self.current_token.span.clone(),
// loc: self.current_location(),
// },
// _ => {
// return Err(CompileTimeError {
// location: self.current_location(),
// etype: ParserErrorType::ExpectedIdentifier,
// file_name: Some(self.lexer.file_name.clone()),
// code_raw: Some(self.lexer.select(left_start..self.current_token.span.end)),
// verbose: None,
// caret: true,
// });
// }
// };
// self.next_token();

// if self.current_token_is(TokenKind::LeftParen) {
// chains.push(FieldAccessOrMethodCall {
// method_call: Some(self.parse_func_call(ast::ast::Expression::Identifier(identifier), left_start)?),
// field_access: None,
// });
// } else {
// chains.push(FieldAccessOrMethodCall {
// method_call: None,
// field_access: Some(FieldAccess {
// identifier,
// span: self.current_token.span.clone(),
// loc: self.current_location(),
// }),
// });
// }
// }

Ok(Expression::FieldAccessOrMethodCall(chains))
}

pub fn parse_func_call(&mut self, left: Expression, left_start: usize) -> Result<FuncCall, ParseError> {
let arguments = self.parse_expression_series(TokenKind::RightParen)?;
let start = self.current_token.span.start;
match left {
let expr = match left {
Expression::FromPackage(from_package) => {
self.next_token();

Ok(FuncCall {
FuncCall {
func_name: from_package,
arguments: arguments.0,
span: Span {
start: left_start,
end: self.current_token.span.end,
},
loc: self.current_location(),
})
}
}
Expression::Identifier(identifier) => {
self.next_token();

Ok(FuncCall {
FuncCall {
func_name: FromPackage {
sub_packages: vec![],
identifier,
Expand All @@ -447,7 +438,7 @@ impl<'a> Parser<'a> {
end: self.current_token.span.end,
},
loc: self.current_location(),
})
}
}
_ => {
return Err(CompileTimeError {
Expand All @@ -459,7 +450,9 @@ impl<'a> Parser<'a> {
caret: true,
});
}
}
};

Ok(expr)
}

pub fn parse_struct_member(&mut self, object_expr: Box<Expression>) -> Result<Expression, ParseError> {
Expand Down
1 change: 0 additions & 1 deletion parser/src/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,6 @@ impl<'a> Parser<'a> {
Ok(body)
}

// ANCHOR
pub fn parse_for_loop(&mut self) -> Result<Statement, ParseError> {
let start = self.current_token.span.start;

Expand Down
31 changes: 11 additions & 20 deletions parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ mod tests {
fn test_func_call_expresion() {
// FIXME
assert_parse("foo_bar();");
assert_parse("print(1);");
// assert_parse("print(1);");
assert_parse("foo_bar(1, 2);");
// assert_parse("print(1 + 2);");
assert_parse("print(1 as double);");
assert_parse("print(1 + 2)");
// assert_parse("print(1 as double);");
// assert_parse("print(nested());");

// assert_parse("print(!true);");
// assert_parse("print(!false);");
// assert_parse("print(\"Cyrus Lang =)\");");
Expand Down Expand Up @@ -168,34 +169,24 @@ mod tests {

#[test]
fn test_parse_for_statement() {
assert_parse(
"
for #i = 0; i < 10; i++ {
}
",
);
assert_parse("for #i = 0; i < 10; i++ { }");
assert_parse("for #i = 0; i < 10; { }");
assert_parse("for #i = 0; { }");
assert_parse("for { }");
}

#[test]
fn test_arrays() {
assert_parse("#a: array = [1, 2, 3]");
assert_parse("#a: array = [\"Cyrus\", \"Lang\"]");
assert_parse("#a: i32[3] = [1, 2, 3]");
assert_parse("#a: string[2] = [\"Cyrus\", \"Lang\"]");
assert_parse("arr[0][1]");
}

#[test]
fn test_array_index_assign() {
assert_parse("my_var[1] = 555;");
}

#[test]
fn test_func_return_user_defined_type() {
assert_parse("fn sample(): MyStruct {}");
}

#[test]
fn test_ptr_identifier() {
assert_parse("*my_var");
assert_parse("&**my_var");
}
}

0 comments on commit a516c24

Please sign in to comment.