Added FileReader to stdlib
Added FileReader to stdlib to read in files and return it as a string
This commit is contained in:
parent
3be4ae1941
commit
4212bec61f
7 changed files with 50 additions and 6 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -30,4 +30,5 @@ bin/
|
||||||
|
|
||||||
.class
|
.class
|
||||||
/src/*.jar
|
/src/*.jar
|
||||||
/.idea
|
/.idea
|
||||||
|
hello_world.froj
|
|
@ -1,4 +1,3 @@
|
||||||
|
var string = fileReader("..eogm");
|
||||||
|
|
||||||
|
print string;
|
||||||
|
|
||||||
print "Hello world!";
|
|
||||||
|
|
|
@ -74,7 +74,7 @@ public class Froj {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void error(int line, String message) {
|
public static void error(int line, String message) {
|
||||||
report(line, "", message);
|
report(line, "", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,9 @@ import com.jsaasta.froj.Interpreter;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Returns current time in nanoSeconds / 1000;
|
||||||
|
*/
|
||||||
public class Clock implements FrojCallable {
|
public class Clock implements FrojCallable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -14,7 +17,7 @@ public class Clock implements FrojCallable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object call(Interpreter interpreter, List<Object> arguments) {
|
public Object call(Interpreter interpreter, List<Object> arguments) {
|
||||||
return (double) System.currentTimeMillis() / 1000.0;
|
return (double) System.nanoTime() / 1000.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
29
src/com/jsaasta/froj/stdlib/FileReader.java
Normal file
29
src/com/jsaasta/froj/stdlib/FileReader.java
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package com.jsaasta.froj.stdlib;
|
||||||
|
|
||||||
|
import com.jsaasta.froj.Froj;
|
||||||
|
import com.jsaasta.froj.FrojCallable;
|
||||||
|
import com.jsaasta.froj.Interpreter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class FileReader implements FrojCallable {
|
||||||
|
@Override
|
||||||
|
public int arity() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object call(Interpreter interpreter, List<Object> arguments) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
byte[] bytes = Files.readAllBytes(Paths.get((String) arguments.get(0)));
|
||||||
|
return new String(bytes, Charset.defaultCharset());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new StdlibRuntimeError("Couldn't find file: " + arguments.get(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ public class Stdlib {
|
||||||
classes = new HashMap<>();
|
classes = new HashMap<>();
|
||||||
classes.put("clock", new Clock());
|
classes.put("clock", new Clock());
|
||||||
classes.put("input", new Input());
|
classes.put("input", new Input());
|
||||||
|
classes.put("fileReader", new FileReader());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, Object> define(){
|
public static Map<String, Object> define(){
|
||||||
|
|
11
src/com/jsaasta/froj/stdlib/StdlibRuntimeError.java
Normal file
11
src/com/jsaasta/froj/stdlib/StdlibRuntimeError.java
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
package com.jsaasta.froj.stdlib;
|
||||||
|
|
||||||
|
import com.jsaasta.froj.Token;
|
||||||
|
|
||||||
|
public class StdlibRuntimeError extends RuntimeException{
|
||||||
|
|
||||||
|
|
||||||
|
public StdlibRuntimeError(String message) {
|
||||||
|
super(message, null, false, false);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue