-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
204 lines (183 loc) · 5.32 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Compiler</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/prism.min.js"></script>
</head>
<style>
.token.primary
{
color: #0072b5;
}
.token.keywords {
color: rgb(167, 11, 120);
}
.token.string
{
color: #a35114;
}
.token.operators {
color: #c10101;
}
.token.secondary
{
color: #075063;
}
.token.array
{
color: rgb(34, 144, 24);
}
.token.assign{
color: #63258d;
}
#highlighting {
overflow: auto;
position: absolute;
width: 100%;
height: 370px;
background: #dcdcdc;
padding: 10px;
margin: 0;
}
#editing {
resize: none;
overflow: auto;
position: absolute;
white-space: nowrap;
color: transparent;
background: transparent;
caret-color: rgb(85, 85, 85);
z-index: 1;
tab-size: 2;
border: 0;
width: 100%;
height: 370px;
padding: 10px;
margin: 0;
font-size: 15px;
line-height: 20px;
word-spacing: 5px;
font-weight: bold
}
#highlighting-content {
border-radius: 2px;
color: rgb(0, 0, 0);
position: absolute;
tab-size: 2;
left: 10px;
font-size: 15px;
line-height: 20px;
word-spacing: 5px;
font-weight: bold
}
/* End of prism.js syntax highlighting*/
</style>
<body style="font-family: sans-serif;">
<h1>Simple Compiler</h1>
<p>A simple and fast condition / expression compiler and executer <br>
Fully JavaScript based and no dependencies <br>
Priority: Bracket >> operators (left to right execution , no BODMAS rule) <br>
Atleast one space is must between all operators/ variables / constants</p>
<!-- //////////////// -->
<div style="height: 200px; position: fixed; margin-top: 50px; width: 90%;">
<textarea id="editing" spellcheck="false" oninput="update(this.value); sync_scroll(this);" onscroll="sync_scroll(this);" onkeydown="check_tab(this, event);"></textarea>
<pre id="highlighting" aria-hidden="true">
<code class="language-html" id="highlighting-content"></code>
</pre>
</div>
<!-- /////////////////// -->
<div style="display: flex;grid-gap: 5px;">
<div>Output : </div>
<div id="value" style="font-weight: bold;" >6</div>
</div>
<script src="simpleCompiler.js"></script>
<script>
let engine = new SimpleCompiler();
customLanguage();
const txt =
`( 1 + 2 ) > 2 ? ( 1 + 1 ) : ( 1 + 2 )`;
document.getElementById('editing').value = txt;
update(txt);
function showResult(text) {
const expression = text;
const{output , fail} = engine.compileAndExecute(expression);
///////////////////////////////////////////
document.getElementById("value").innerHTML = String(output);
if(!fail){
document.getElementById("value").style.color = "black";
}else{
document.getElementById("value").style.color = "red";
}
};
function customLanguage(){
Prism.languages.html = {
'keywords':{
pattern: /(^| |||\n)(LIKE|UNLIKE|BREAK|IN|false|true)(^| |||\n)/,
greedy: true
},
'operators': {
pattern: /[.=?:><!()]/,
greedy: true
},
'primary': {
pattern: /(^| ?|\n)[A-Za-z]*(?=\.)/,
greedy: true
},
'secondary': {
pattern: /(?<=[.])[A-Za-z]*(?=\s)/g,
greedy: true
},
'string': {
pattern: /(?<= ([=><!][=]) )'[A-Za-z .,_0-9-]*'/,
greedy: true
},
'assign':{
pattern: /(?<= (=) )'[A-Za-z .,_0-9-]*'/,
greedy: true
},
'array': {
pattern: /(?<= (IN|LIKE|UNLIKE) )'[A-Za-z .,_0-9-]*'/,
greedy: true
},
};
}
function update(text) {
let result_element = document.querySelector("#highlighting-content");
// Handle final newlines (see article)
if(text[text.length-1] == "\n") {
text += " ";
}
// Update code
result_element.innerHTML = text.replace(new RegExp("&", "g"), "&").replace(new RegExp("<", "g"), "<"); /* Global RegExp */
// Syntax Highlight
Prism.highlightElement(result_element);
showResult(text);
}
function sync_scroll(element) {
/* Scroll result to scroll coords of event - sync with textarea */
let result_element = document.querySelector("#highlighting");
// Get and set x and y
result_element.scrollTop = element.scrollTop;
result_element.scrollLeft = element.scrollLeft;
}
function check_tab(element, event) {
let code = element.value;
if(event.key == "Tab") {
/* Tab key pressed */
event.preventDefault(); // stop normal
let before_tab = code.slice(0, element.selectionStart); // text before tab
let after_tab = code.slice(element.selectionEnd, element.value.length); // text after tab
let cursor_pos = element.selectionEnd + 1; // where cursor moves after tab - moving forward by 1 char to after tab
element.value = before_tab + "\t" + after_tab; // add tab char
// move cursor
element.selectionStart = cursor_pos;
element.selectionEnd = cursor_pos;
update(element.value); // Update text to include indent
}
}
</script>
</body>
</html>