-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
533 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fun fib(n) { | ||
if (n <= 1) return n; | ||
return fib(n - 2) + fib(n - 1); | ||
} | ||
|
||
for (var i = 0; i < 20; i = i + 1) { | ||
print fib(i); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.craftinginterpreters.lox; | ||
|
||
import java.util.List; | ||
|
||
interface LoxCallable { | ||
int arity(); | ||
|
||
Object call(Interpreter interpreter, List<Object> arguments); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/craftinginterpreters/lox/LoxFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.craftinginterpreters.lox; | ||
|
||
import java.util.List; | ||
|
||
public class LoxFunction implements LoxCallable { | ||
private final Stmt.Function declaration; | ||
private final Environment closure; | ||
|
||
LoxFunction(Stmt.Function declaration, Environment closure) { | ||
this.closure = closure; | ||
this.declaration = declaration; | ||
} | ||
|
||
@Override | ||
public int arity() { | ||
return declaration.params.size(); | ||
} | ||
|
||
@Override | ||
public Object call(Interpreter interpreter, List<Object> arguments) { | ||
Environment environment = new Environment(closure); | ||
for (int i = 0; i < declaration.params.size(); i++) { | ||
environment.define(declaration.params.get(i).lexeme, arguments.get(i)); | ||
} | ||
|
||
try { | ||
interpreter.executeBlock(declaration.body, environment); | ||
} catch (Return returnValue) { | ||
return returnValue.value; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "<fn " + declaration.name.lexeme + ">"; | ||
} | ||
} |
Oops, something went wrong.