-
Notifications
You must be signed in to change notification settings - Fork 9
/
jlox_diff.patch
101 lines (99 loc) · 2.91 KB
/
jlox_diff.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
diff --git a/java/com/craftinginterpreters/lox/Interpreter.java b/java/com/craftinginterpreters/lox/Interpreter.java
index 823503f4..8c638059 100644
--- a/java/com/craftinginterpreters/lox/Interpreter.java
+++ b/java/com/craftinginterpreters/lox/Interpreter.java
@@ -2,6 +2,10 @@
package com.craftinginterpreters.lox;
//> Statements and State import-list
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+
//> Functions import-array-list
import java.util.ArrayList;
//< Functions import-array-list
@@ -35,6 +39,9 @@ class Interpreter implements Expr.Visitor<Object>,
//< Statements and State environment-field
//> Functions interpreter-constructor
+
+ private InputStreamReader getcStream = new InputStreamReader(System.in, StandardCharsets.UTF_8);
+
Interpreter() {
globals.define("clock", new LoxCallable() {
@Override
@@ -49,8 +56,74 @@ class Interpreter implements Expr.Visitor<Object>,
@Override
public String toString() { return "<native fn>"; }
});
+
+ globals.define("getc", new LoxCallable() {
+ @Override
+ public int arity() { return 0; }
+
+ @Override
+ public Object call(Interpreter interpreter,
+ List<Object> arguments) {
+ try {
+ int c = getcStream.read();
+ if (c < 0) {
+ return (double)-1;
+ }
+ return (double)c;
+ } catch (IOException error) {
+ return (double)-1;
+ }
+ }
+
+ @Override
+ public String toString() { return "<native fn>"; }
+ });
+
+ globals.define("chr", new LoxCallable() {
+ @Override
+ public int arity() { return 1; }
+
+ @Override
+ public Object call(Interpreter interpreter,
+ List<Object> arguments) {
+ return Character.toString((char)(double)arguments.get(0));
+ }
+
+ @Override
+ public String toString() { return "<native fn>"; }
+ });
+
+ globals.define("exit", new LoxCallable() {
+ @Override
+ public int arity() { return 1; }
+
+ @Override
+ public Object call(Interpreter interpreter,
+ List<Object> arguments) {
+ System.exit((int)(double)arguments.get(0));
+ return null;
+ }
+
+ @Override
+ public String toString() { return "<native fn>"; }
+ });
+
+ globals.define("print_error", new LoxCallable() {
+ @Override
+ public int arity() { return 1; }
+
+ @Override
+ public Object call(Interpreter interpreter,
+ List<Object> arguments) {
+ System.err.println((String)arguments.get(0));
+ return null;
+ }
+
+ @Override
+ public String toString() { return "<native fn>"; }
+ });
}
-
+
//< Functions interpreter-constructor
/* Evaluating Expressions interpret < Statements and State interpret
void interpret(Expr expression) { // [void]