Skip to content

Commit

Permalink
add fibonacci
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkesy committed Mar 13, 2024
1 parent 62c86fe commit aed94ba
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
12 changes: 12 additions & 0 deletions examples/fibonacci.mmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
void main() {
int fib = fibbonacci_single_return(15);
println(str(fib));
}

int fibbonacci_single_return(int n) {
int ret = n;
if n > 1 {
ret = fibbonacci_single_return(n - 1) + fibbonacci_single_return(n - 2);
}
return ret;
}
1 change: 1 addition & 0 deletions src/Interpreter/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interpretStatement (InterpretState state _) (ExpressionStatement expression) = d
interpretStatement (InterpretState state _) (FunctionDefinitionStatement _) = do
return (InterpretState state Nothing)
interpretStatement (InterpretState state _) (ReturnStatement expression) = do
-- TODO: return should cancel the current function
ret <- interpretExpression state expression
return (InterpretState state (Just ret))
interpretStatement (InterpretState state _) (ControlStatement control) = do
Expand Down
6 changes: 4 additions & 2 deletions src/Parser/Name.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Text.Parsec.String

parseName :: Parser Name
parseName = do
fistChar <- letter
rest <- many (digit <|> letter)
fistChar <- startChar
rest <- many (digit <|> startChar)
return (fistChar : rest)
where
startChar = letter <|> char '_'

0 comments on commit aed94ba

Please sign in to comment.