-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmicroJ3.html
177 lines (167 loc) · 3.92 KB
/
microJ3.html
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" media="all"
href="https://maeyler.github.io/JS/sss/inspector.css">
<script src="https://maeyler.github.io/JS/sss/inspector.js"></script>
<script src="Lexical.js"></script>
<script src="Expression.js"></script>
<script src="microJ1.js"></script>
<script src="microJ3.js"></script>
<title>microJ3 Parser </title>
<style>
body { background: #fca; }
#outx {
resize: both;
background: #eee;
}
#inpx, #outx {
box-sizing: border-box;
border: 1px solid gray;
width: 400px;
height: 14em;
overflow-y: auto;
font: 12px monospace;
}
#file { width: 150px; }
</style>
</head>
<body>
<h2 id=title>microJ3 Parser  
<button onClick='doParse()'>Parse</button>
<input type=file id=file onChange='fileSelect(this)' />
</h2>
<p>
<textarea id=inpx rows=14>
M(k) { print k; } //Mark
R(n) { //Rule
if (n == 0) { return; }
R(n-1); M(n); R(n-1);
}
ruler(n) { //main
n = 5; //read n;
R(n); println;
}
</textarea>
</p>
<pre id=outx>
</pre>
<hr />
<script>
"use strict";
function doParse() {
let str = inpx.value
if (!str) return
tokens = Token.list(str)
let y = [...tokens] //clone
display(y); //console.log(y);
try {
tokens.reverse()
tok = tokens.pop()
let p = program();
match(EOF); //success();
console.log(p); display(p);
outx.innerText = p.toTree()
title.scrollIntoView()
return p
} catch(x) {
showError(inpx)
outx.innerText = x
reportError(x)
}
}
const reader = new FileReader();
function fileSelect(t) { //target is the button
let f = t.files[0];
if (f.name.toLowerCase().endsWith(".mj")) {
reader.onload = function(evt) { //text
inpx.value = evt.target.result;
console.log(f.name, f.size); doParse()
}
reader.readAsText(f);
}
}
//the simplest interpreter
var current //Statement
var SF //current StackFrame
class StackFrame {
//Method met; running method
//float[] values; current values
constructor(m, v) {
this.met = m; this.values = v;
}
toString() {
let s = this.values.join(' ');
return this.met+"["+ s +" ]";
}
}
class VM { //virtual machine for interpretation
static start(p) {
VM.stopped = false;
VM.returnValue = NaN;
try {
SF = null; printer = '';
VM.invokeMethod(p.main, []);
if (printer) console.log(printer)
if (SF != null) throw ("illegal Frame "+SF);
return "OK"
} catch(x) {
VM.error(x);
}
}
static error(msg) {
console.log(msg);
console.log("at "+SF.met+": "+current);
}
static invokeMethod(m, v) {
let old = SF;
SF = new StackFrame(m, v);
try {
VM.runBlock(m);
VM.returnValue = 0;
} catch(x) {
if (typeof x == "number")
VM.returnValue = x;
//console.log("return ", x);
else throw x;
}
SF = old;
}
static runBlock(b) {
let old = current;
for (let s of b.list) {
if (VM.stopped)
throw "Program stopped";
current = s; s.run();
}
current = old;
}
static assertEQ(x, y) {
if (x != y) throw ("Unexpected "+y);
}
static getValue(v) {
VM.assertEQ(v.met, SF.met);
return SF.values[v.ind];
}
static setValue(v, val) {
VM.assertEQ(v.met, SF.met);
SF.values[v.ind] = val;
}
}
function init() {
MENU = new Menu() //extends Menu
MENU.global = window
display(MENU)
}
try {
inspect(document.body, init)
//inp.value = "_.run()"
} catch(x) {
reportError(x)
}
inpx.focus(); doParse()
</script>
</body>
</html>