-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.html
157 lines (130 loc) · 3.98 KB
/
index.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
<html>
<head>
<title>JSPython dev</title>
<script src="./node_modules/ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="./dist/jspython-interpreter.js"></script>
<script src="./dist/assets/mode-jspython.js"></script>
<style>
.container {
height: 100%;
min-width: 200px;
width: 90%;
margin: 10px auto;
}
#editor,
#resultEditor {
height: 40%;
display: block;
min-height: 20%;
width: 100%;
margin-top: 10px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<h4>JSPython development console</h4>
<div id="editor">
a = 33
b = 33
if b > a:
return print("b is greater than a")
elif a == b:
return print("a and b are equal")
</div>
<button onclick="tokenize()">Tokenize</button>
<button onclick="parse()">Parse</button>
<button onclick="runInterpreter()">Run</button>
<div id="resultEditor"> </div>
<!-- <textarea id="result"></textarea> -->
</div>
<script>
const editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/python");
const resultEditor = ace.edit("resultEditor");
resultEditor.setTheme("ace/theme/monokai");
resultEditor.session.setMode("ace/mode/json");
const interpreter = jspython.jsPython();
console.log({ interpreter });
function tokenize() {
tokenizer = (s) => console.log(`tokens => ${s}`, interpreter().tokenize(s))
const scripts = editor.getValue();
try {
const result = interpreter
.tokenize(scripts)
.map((t, i) => `${t[1]} : '${t[0]}'`)
.join('\n');
const data = typeof result === 'object' ? JSON.stringify(result, null, '\t') : result;
resultEditor.getSession().setValue(data)
} catch (err) {
console.error(err);
resultEditor.getSession().setValue(String(err.message))
}
}
function parse() {
const scripts = editor.getValue();
try {
const result = interpreter
.parse(scripts);
const data = typeof result === 'object' ? JSON.stringify(result, null, '\t') : result;
resultEditor.getSession().setValue(data)
} catch (err) {
console.error(err);
resultEditor.getSession().setValue(String(err.message))
}
}
async function runInterpreter() {
const scripts = editor.getValue();
try {
interpreter.registerModuleLoader((path => {
return Promise.resolve(`
{"x": "test22", "n": 55}
`);
}));
interpreter.registerPackagesLoader(path =>
(
path === 'service' ? {
add: (x, y) => x + y,
remove: (x, y) => x - y,
times: (x, y) => x * y,
}
: null
)
);
const scope = {
Math,
errorFunc: p => {
if (p === 6) {
throw new Error(`Don't like '6'`);
}
console.log('allGood');
},
sqrPromise: (fn) => new Promise((s, f) => setTimeout(() => s(fn * fn), 5)),
print: (...args) => { console.log(...args); return args.length > 0 ? args[0] : null; },
x: 10,
o: {
f1: (x, y) => { return { x, y }; },
v1: 55,
sub1: {
subValue: 88
}
}
};
const result = await interpreter
.evaluate(scripts, scope, undefined, 'index1.jspy');
// const result = jsPython()
// .eval(scripts, scope);
// const result = await jsPython()
// .evalAsync(scripts, scope);
const data = typeof result === 'object' ? JSON.stringify(result, null, '\t') : String(result);
resultEditor.getSession().setValue(data)
} catch (err) {
console.log('error', err);
resultEditor.getSession().setValue(String(err.message))
}
}
</script>
</body>
</html>