This repository has been archived by the owner on Jul 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsource.html
150 lines (125 loc) · 3.61 KB
/
source.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>miniForth playGround</title>
<style>
*{
box-sizing: border-box;
padding: 0;
margin: 0;
}
.parent {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(2, 1fr);
grid-column-gap: 0px;
grid-row-gap: 0px;
width: 100vw;
height: 100vh;
}
.parent > * {
padding: 10px;
}
#miniForth {
min-width: 50vw;
grid-area: 1 / 1 / 3 / 2;
}
#asm { grid-area: 1 / 2 / 2 / 3; }
#result { grid-area: 2 / 2 / 3 / 3; }
</style>
</head>
<body>
<div class="parent">
<textarea id='miniForth'></textarea>
<div id='asm'></div>
<div id='result'></div>
</div>
</body>
<!-- <footer>
<a href="./fulfill.html"><button>Fulfill the predicate</button></a>
</footer> -->
<script type="text/javascript">
var myInput = document.getElementById("miniForth");
if(myInput.addEventListener ) {
myInput.addEventListener('keydown',this.keyHandler,false);
} else if(myInput.attachEvent ) {
myInput.attachEvent('onkeydown',this.keyHandler); /* damn IE hack */
}
function keyHandler(e) {
var TABKEY = 9;
if(e.keyCode == TABKEY) {
var start = myInput.selectionStart;
var end = myInput.selectionEnd;
var sel = myInput.value.substring(start, end);
var finText = myInput.value.substring(0, start) + ' ' + myInput.value.substring(end);
myInput.value = finText;
myInput.focus();
myInput.selectionEnd= end + 4;
if(e.preventDefault) {
e.preventDefault();
}
return false;
}
}
</script>
<script src="./modules/bsv.bundle.js"></script>
<script>
XXXPARSERXXX
var W;
var H;
var M = document.getElementById('miniForth');
var A = document.getElementById('asm')
A.style.backgroundColor = '#F1F1F1';
M.value = String.raw`XXXREADMEXXX`
// // window size
// W = window.innerWidth;
// H = window.innerHeight;
// M.style.height = H / 2 - 25 + 'px';
// M.style.width = W - 20 + 'px';
// A.style.height = H / 2 - 25 + 'px';
// A.style.width = W - 20 + 'px';
var i0;
var i1;
var o;
window.setInterval(() => {
// parse result
i1 = M.value;
if(i1 != i0) {
try {
o = peg$parse(i1)
evalScript(o)
} catch (err) {
o = err
}
// console.log(o)
A.innerText = o;
}
i0 = i1;
}, 1000)
var r = document.getElementById('result')
function evalScript(opcodes) {
console.log(opcodes)
let s = bsvjs.Script.fromAsmString(opcodes);
let i = new bsvjs.Interp();
// i.flags = bsvjs.Script.Interpreter.SCRIPT_ENABLE_MAGNETIC_OPCODES | bsv.Script.Interpreter.SCRIPT_ENABLE_MONOLITH_OPCODES;
i.script = s;
i.verify();
console.log(i)
i = i.toJSON()
console.log(i)
r.innerText = 'stack: '
+ JSON.stringify(fixDouble(i.stack))
+ '\naltstack: '
+ JSON.stringify(fixDouble(i.altStack))
+ (i.errStr != '' ? `\nerror: ${i.errStr}` : '')
}
// function lineLimit(str) {
// return str.match(/.{1,30}/g).join('\n')
// }
function fixDouble(str) {
return str.slice(str.length/2)
}
</script>
</html>