-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.html
More file actions
175 lines (154 loc) · 5.27 KB
/
index.html
File metadata and controls
175 lines (154 loc) · 5.27 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Calc Editor</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h2>Calc Editor</h2>
<div>
<button id="run">
Run
</button>
</div>
<div id="modal">
</div>
<div id="container" style="width:800px;height:600px;border:1px solid grey"></div>
<div class="output">
<p id="output-value"> >> </p>
</div>
<script src="node_modules/monaco-editor/min/vs/loader.js"></script>
<script src="js/main.js"></script>
<script>
require.config({ paths: { 'vs': 'node_modules/monaco-editor/min/vs' }});
let editor;
let result=[];
let variables=[];
require(['vs/editor/editor.main'], function() {
monaco.languages.register({ id: 'calc' });
monaco.languages.setTokensProvider('calc', new CalcTokensProvider.CalcTokensProvider());
let literalFg = '3b8737';
let idFg = '344482';
let symbolsFg = '000000';
let keywordFg = '7132a8';
let errorFg = 'ff0000';
monaco.editor.defineTheme('myCoolTheme', {
base: 'vs',
inherit: false,
rules: [
{ token: 'number_lit.calc', foreground: literalFg },
{ token: 'id.calc', foreground: idFg, fontStyle: 'italic' },
{ token: 'lparen.calc', foreground: symbolsFg },
{ token: 'rparen.calc', foreground: symbolsFg },
{ token: 'equal.calc', foreground: symbolsFg },
{ token: 'minus.calc', foreground: symbolsFg },
{ token: 'plus.calc', foreground: symbolsFg },
{ token: 'div.calc', foreground: symbolsFg },
{ token: 'mul.calc', foreground: symbolsFg },
{ token: 'input_kw.calc', foreground: keywordFg, fontStyle: 'bold' },
{ token: 'output_kw.calc', foreground: keywordFg, fontStyle: 'bold' },
{ token: 'unrecognized.calc', foreground: errorFg }
]
});
editor = monaco.editor.create(document.getElementById('container'), {
value: [
'input salary',
'input nEmployees',
'input revenues',
'input otherExpenses',
'input taxRate',
'',
'totalExpenses = salary * nEmployees + otherExpenses',
'grossProfit = revenues - totalExpenses',
'totalTaxes = grossProfit * (taxRate / 100)',
'profit = profit - totalTaxes',
'',
'output totalTaxes',
'output profit',
''
].join('\n'),
language: 'calc',
theme: 'myCoolTheme'
});
editor.onDidChangeModelContent(function (e) {
let code = editor.getValue()
let syntaxErrors = ParserFacade.validate(code);
let monacoErrors = [];
for (let e of syntaxErrors) {
monacoErrors.push({
startLineNumber: e.startLine,
startColumn: e.startCol,
endLineNumber: e.endLine,
endColumn: e.endCol,
message: e.message,
severity: monaco.MarkerSeverity.Error
});
};
window.syntaxErrors = syntaxErrors;
let model = monaco.editor.getModels()[0];
monaco.editor.setModelMarkers(model, "owner", monacoErrors);
});
});
document.getElementById("run").addEventListener("click", ()=>{
const code = editor.getValue();
// get the inputs only and ask the user for the values
variables = ParserFacade.getInputs(code);
const modals = document.getElementById("modal")
if(variables.length>0){
let html=`<div id="modal">
<div class="modal-content">`
for (let i = 0; i < variables.length; i++) {
html+=`
<input type="text"" id="textInput-${variables[i].variable}" placeholder="Enter the ${variables[i].variable} value">
`
}
html+=`<button id="saveButton">Save</button></div></div>`
modals.innerHTML=html;
document.getElementById("modal").style.display = "block";
}
else {
// run the visitor giving it the input values to the class constructor
result = ParserFacade.calculateExpression(code,variables);
writeOutputToConsole(result);
}
const saveButton = document.getElementById('saveButton');
saveButton.addEventListener('click', () => {
// Get text from modal
for (let i = 0; i < variables.length; i++) {
const textInput = document.getElementById(`textInput-${variables[i].variable}`);
const text = textInput.value;
// Check if the inserted value is a number to prevent misbehavior
if(isNaN(text)){
writeErrorToConsole("Error: Assigning "+text+" to "+ variables[i].variable +" : is not a number");
return;
}
variables[i].value=text
}
document.getElementById("modal").style.display = "none";
// Call visitor to make calculations
result = ParserFacade.calculateExpression(code,variables);
writeOutputToConsole(result);
});
});
function writeOutputToConsole(result){
for (let i = 0; i <result.output.length ; i++) {
let output = document.getElementById("output-value");
output.innerHTML += "<b>"+result.output[i].variable+"</b>" + " = " + result.output[i].value+"<br>";
output.innerHTML += ">> ";
}
for (let i = 0; i <result.errors.length ; i++) {
let output = document.getElementById("output-value");
output.innerHTML += '<span style="color:red">'+result.errors[i].message+"</span>"+"<br>";
output.innerHTML += ">> ";
}
}
function writeErrorToConsole(messsage){
let output = document.getElementById("output-value");
output.innerHTML += '<span style="color:red">'+messsage+"</span>"+"<br>";
output.innerHTML += ">> ";
}
</script>
</body>
</html>