Skip to content

Commit

Permalink
fix: issue with return values
Browse files Browse the repository at this point in the history
  • Loading branch information
megatank58 committed Apr 26, 2023
1 parent 592d1da commit 359cca6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
1 change: 0 additions & 1 deletion examples/main.oxi

This file was deleted.

6 changes: 4 additions & 2 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,18 @@ impl<'a> Interpreter<'a> {
}

let mut stream = function.statements.into_iter().peekable();

loop {
if stream.peek().is_none() {
break;
}

self.match_node(stream.next().unwrap());

if self.returned.is_some() {
self.returned = None;
break;
}

self.match_node(stream.next().unwrap());
}
}
}
Expand Down
34 changes: 28 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{iter::Peekable, vec::IntoIter, ops::Range};
use std::{iter::Peekable, ops::Range, vec::IntoIter};

use crate::{
ast::{AstNode, Expression},
Expand All @@ -22,7 +22,10 @@ impl<'a> Parser<'a> {
Some(ast)
}

pub fn match_tokens(&'a self, tokens: Vec<(Token, usize)>) -> Option<Vec<(AstNode, Range<usize>)>> {
pub fn match_tokens(
&'a self,
tokens: Vec<(Token, usize)>,
) -> Option<Vec<(AstNode, Range<usize>)>> {
let mut pos = 0;
let mut nodes: Vec<(AstNode, Range<usize>)> = vec![];

Expand Down Expand Up @@ -205,7 +208,10 @@ impl<'a> Parser<'a> {

let (expression, _) = self.pratt_parser(tokens.into_iter().peekable(), 0);

(AstNode::Assignment(ident.to_string(), expression), token.1..t.1)
(
AstNode::Assignment(ident.to_string(), expression),
token.1..t.1,
)
} else {
error(
self.name,
Expand Down Expand Up @@ -343,17 +349,30 @@ impl<'a> Parser<'a> {
let mut expression = vec![];

let mut end = 0;
let mut depth = 1;

for token in tokens {
if token.0 == Token::LParen {
depth += 1;
}
if token.0 == Token::RParen {
depth -= 1;
if !expression.is_empty() {
if depth > 0 {
expression.push(token);
}
let (data, _) =
self.pratt_parser(expression.clone().into_iter().peekable(), 0);

params.push(data);

expression.clear();
continue;
}
if depth == 0 {
end = token.1;
break;
}
end = token.1;
break;
}

if token.0 == Token::Comma {
Expand All @@ -368,7 +387,10 @@ impl<'a> Parser<'a> {
expression.push(token);
}

(AstNode::FunctionCall(ident.to_string(), params), token.1..end)
(
AstNode::FunctionCall(ident.to_string(), params),
token.1..end,
)
} else if token.0 == Token::Fn {
let t = &stream.next()?;
if let Token::FunctionName(name) = &t.0 {
Expand Down

0 comments on commit 359cca6

Please sign in to comment.