aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornic-gaffney <gaffney_nic@protonmail.com>2023-04-11 18:16:31 -0500
committernic-gaffney <gaffney_nic@protonmail.com>2023-04-11 18:16:31 -0500
commit02f9abad0419e84cf62497a31657a632c1ace5eb (patch)
tree6ececbcfc3cdcb548d38a5145a72b7eb14d0b441
parent5a556ee16afe1c4065152adf089a33b4d9d2fba2 (diff)
downloadsloth-02f9abad0419e84cf62497a31657a632c1ace5eb.tar.gz
Functions work?
-rw-r--r--crates/sloth/src/parser/expr.rs10
-rw-r--r--crates/sloth/src/parser/stmt.rs13
2 files changed, 14 insertions, 9 deletions
diff --git a/crates/sloth/src/parser/expr.rs b/crates/sloth/src/parser/expr.rs
index a23359d..4095f98 100644
--- a/crates/sloth/src/parser/expr.rs
+++ b/crates/sloth/src/parser/expr.rs
@@ -73,9 +73,13 @@ impl<'a> AstParser<'a> {
TokenType::Character(literal) => Expr::Literal(Literal::Char(literal)),
TokenType::String(literal) => Expr::Literal(Literal::String(literal)),
TokenType::Regex(literal) => Expr::Literal(Literal::Regex(literal)),
- TokenType::Identifier(ident) => Expr::Variable(ident), /* TODO: make this shit check
- * if it is calling a
- * function dumbass */
+ TokenType::Identifier(ident) => {
+ if self.peek().tt.clone() != TokenType::OpeningParen {
+ Expr::Variable(ident)
+ } else {
+ Expr::Literal(Literal::String(ident))
+ }
+ }
TokenType::OpeningParen => {
let expr = self.expression();
self.consume(TokenType::ClosingParen, "Must end expression with ')'");
diff --git a/crates/sloth/src/parser/stmt.rs b/crates/sloth/src/parser/stmt.rs
index bd7b4b2..abb90fc 100644
--- a/crates/sloth/src/parser/stmt.rs
+++ b/crates/sloth/src/parser/stmt.rs
@@ -1,4 +1,4 @@
-use super::ast::{Expr, FuncArgs, Stmt};
+use super::ast::{FuncArgs, Stmt};
use super::AstParser;
use crate::lexer::TokenType;
@@ -204,6 +204,7 @@ impl<'a> AstParser<'a> {
fn return_statement(&mut self) -> Stmt {
let expr = self.expression();
+ self.consume(TokenType::SemiColon, "Expected ';' after return statement");
Stmt::Return { value: (expr) }
}
}
@@ -212,9 +213,9 @@ impl<'a> AstParser<'a> {
mod tests {
use itertools::Itertools;
- use super::{AstParser, Expr, Stmt};
+ use super::{AstParser, Stmt};
use crate::lexer::Lexer;
- use crate::parser::ast::{BinaryOp, FuncArgs, Literal};
+ use crate::parser::ast::{BinaryOp, Expr, FuncArgs, Literal};
#[test]
fn basic_statement_a() {
@@ -262,7 +263,7 @@ mod tests {
#[test]
fn basic_statement_c() {
- let lexer = Lexer::new("fn test_c (a, b, c) {\nreturn (b + a * c);\n}");
+ let lexer = Lexer::new("fn test_c (a, b, c) {\nreturn (a + b * c);\n}");
let tokens = lexer.collect_vec();
println!("{tokens:?}");
@@ -283,7 +284,7 @@ mod tests {
},
]),
body: (vec![Stmt::Return {
- value: (Expr::BinaryOp {
+ value: (Expr::Grouping(Box::new(Expr::BinaryOp {
op: BinaryOp::Add,
lhs: Box::new(Expr::Variable("a".to_string())),
rhs: Box::new(Expr::BinaryOp {
@@ -291,7 +292,7 @@ mod tests {
lhs: Box::new(Expr::Variable("b".to_string())),
rhs: Box::new(Expr::Variable("c".to_string())),
}),
- }),
+ }))),
}]),
return_type: (None),
};