Added Stdlib
Updated Interpreter to take in FrojCallable as lists. Added Clock() and Input() as functions
This commit is contained in:
parent
25e5a411a8
commit
105d868d0f
4 changed files with 86 additions and 17 deletions
|
@ -1,5 +1,9 @@
|
|||
package com.jsaasta.froj;
|
||||
|
||||
import com.jsaasta.froj.stdlib.Clock;
|
||||
import com.jsaasta.froj.stdlib.Input;
|
||||
import com.jsaasta.froj.stdlib.Stdlib;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -12,23 +16,7 @@ public class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
|
|||
private Environment environment = globals;
|
||||
|
||||
public Interpreter() {
|
||||
globals.define("clock", new FrojCallable() {
|
||||
@Override
|
||||
public int arity() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object call(Interpreter interpreter, List<Object> arguments) {
|
||||
return (double) System.currentTimeMillis() / 1000.0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<native fn>";
|
||||
}
|
||||
|
||||
});
|
||||
Stdlib.define().forEach(globals::define);
|
||||
}
|
||||
|
||||
public void interpret(List<Stmt> statements) {
|
||||
|
|
25
src/com/jsaasta/froj/stdlib/Clock.java
Normal file
25
src/com/jsaasta/froj/stdlib/Clock.java
Normal file
|
@ -0,0 +1,25 @@
|
|||
package com.jsaasta.froj.stdlib;
|
||||
|
||||
import com.jsaasta.froj.FrojCallable;
|
||||
import com.jsaasta.froj.Interpreter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Clock implements FrojCallable {
|
||||
|
||||
@Override
|
||||
public int arity() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object call(Interpreter interpreter, List<Object> arguments) {
|
||||
return (double) System.currentTimeMillis() / 1000.0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<native fn Clock()>";
|
||||
}
|
||||
|
||||
}
|
37
src/com/jsaasta/froj/stdlib/Input.java
Normal file
37
src/com/jsaasta/froj/stdlib/Input.java
Normal file
|
@ -0,0 +1,37 @@
|
|||
package com.jsaasta.froj.stdlib;
|
||||
|
||||
import com.jsaasta.froj.FrojCallable;
|
||||
import com.jsaasta.froj.Interpreter;
|
||||
import com.jsaasta.froj.RuntimeError;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.List;
|
||||
|
||||
public class Input implements FrojCallable {
|
||||
|
||||
@Override
|
||||
public int arity() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object call(Interpreter interpreter, List<Object> arguments) {
|
||||
InputStreamReader input = new InputStreamReader(System.in);
|
||||
BufferedReader reader = new BufferedReader(input);
|
||||
System.out.print("> ");
|
||||
try {
|
||||
String line = reader.readLine();
|
||||
return line;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<native fn Clock()>";
|
||||
}
|
||||
|
||||
}
|
19
src/com/jsaasta/froj/stdlib/Stdlib.java
Normal file
19
src/com/jsaasta/froj/stdlib/Stdlib.java
Normal file
|
@ -0,0 +1,19 @@
|
|||
package com.jsaasta.froj.stdlib;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Stdlib {
|
||||
|
||||
private static final Map<String, Object> classes;
|
||||
|
||||
static {
|
||||
classes = new HashMap<>();
|
||||
classes.put("clock", new Clock());
|
||||
classes.put("input", new Input());
|
||||
}
|
||||
|
||||
public static Map<String, Object> define(){
|
||||
return classes;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue