-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjipda.mjs
170 lines (138 loc) · 3.6 KB
/
jipda.mjs
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
import fs from 'fs';
import argvf from 'minimist';
import Repl from 'repl';
import {decycle} from './lib/cycle';
import concLattice from "./conc-lattice.mjs";
import typeLattice from "./type-lattice";
import createSemantics from "./js-semantics.mjs";
import concAlloc from "./conc-alloc.mjs";
import concKalloc from "./conc-kalloc.mjs";
import {computeInitialCeskState, explore} from "./abstract-machine.mjs";
import {JsContext} from "./js-context.mjs";
import readline from 'readline';
import {Browser} from './browser.mjs';
import tagAlloc from "./tag-alloc";
import aacKalloc from "./aac-kalloc";
import {StateRegistry} from "./abstract-machine";
import {statesToDot} from "./export/dot-graph";
import {FileResource, StringResource} from "./ast";
/*
b browser
r JIPDA repl
R node repl
g graph
*/
const read = name => fs.readFileSync(name).toString();
const argv = argvf(process.argv.slice(2));
//console.log("::" + JSON.stringify(argv));
const nodeRepl = argv.R;
const json = argv.j;
const graph = argv.g;
const browser = argv.b;
const inputFileName = argv._[0];
const repl = !inputFileName || argv.r;
console.log("input file: " + inputFileName);
console.log("browser: " + browser);
console.log("JSON: " + json);
console.log("graph: " + graph);
console.log("JIPDA repl: " + repl);
console.log("node repl: " + nodeRepl);
let lattice;
let alloc;
let kalloc;
if (argv.lattice === 'type')
{
lattice = typeLattice;
alloc = tagAlloc;
kalloc = aacKalloc;
}
else
{
lattice = concLattice;
alloc = concAlloc;
kalloc = concKalloc;
}
console.log("lattice: " + lattice);
let jsContext = null;
const ast0src = new FileResource("prelude.js");
const ast1src = browser ? new FileResource("web-prelude.js") : new StringResource("");
const jsSemantics = createSemantics(lattice, {errors: true});
const {store:store0, kont:kont0} = computeInitialCeskState(jsSemantics, concAlloc, concKalloc, ast0src, ast1src);
function Explorer()
{
this.stateRegistry = new StateRegistry();
}
Explorer.prototype.explore =
function (initialStates, onEndState)
{
return explore(initialStates, onEndState, undefined, undefined, this.stateRegistry);
}
jsContext = new JsContext(jsSemantics, new Explorer(), alloc, kalloc, store0, kont0);
if (inputFileName)
{
// const src = read(inputFileName);
const resource = new FileResource(inputFileName);
if (browser)
{
const browser = new Browser(jsContext);
browser.parse(resource);
//console.log(resultofparsing.toString());
}
else
{
evalPrint(resource);
}
}
function evalPrint(resource)
{
const jsValue = jsContext.evaluateScript(resource);
const value = jsValue.d;
console.log(value.toString());
}
function finalize()
{
if (json)
{
const stringified = JSON.stringify(decycle(jsContext.explorer.stateRegistry.states));
const outputFileName = (inputFileName || "jipda") + ".json";
fs.writeFileSync(outputFileName, stringified);
console.log("wrote " + outputFileName);
}
if (graph)
{
const g = statesToDot(jsContext.explorer.stateRegistry.states);
const outputFileName = (inputFileName || "jipda") + ".dot";
fs.writeFileSync(outputFileName, g);
console.log("wrote " + outputFileName);
}
if (nodeRepl)
{
Repl.start("node> ").context.jsContext = jsContext;
}
}
if (repl)
{
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function r()
{
rl.question('> ', src =>
{
if (src === ":q")
{
rl.close();
finalize();
return;
}
evalPrint(new StringResource(src))
r();
})
}
r();
}
else
{
finalize();
}