-
Notifications
You must be signed in to change notification settings - Fork 53
/
index.html
153 lines (131 loc) · 5.67 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
<!DOCTYPE html>
<html>
<body>
<h2 style="font-family:arial">The Umka playground</h2>
<a href="https://github.com/vtereshkov/umka-lang" style="text-decoration: none; font-family:arial">Umka on GitHub</a>
<br><br>
<button id="runButton">Run</button>
<br><br>
<textarea id="sourceTextArea" rows="20" style="width:100%; resize:vertical; background-color:LightYellow"></textarea>
<br><br>
<textarea id="outputTextArea" rows="10" style="width:100%; resize:vertical; background-color:WhiteSmoke" readonly></textarea>
<script>
const tabWidth = 4
var runButton = document.getElementById("runButton")
var sourceTextArea = document.getElementById("sourceTextArea")
var outputTextArea = document.getElementById("outputTextArea")
sourceTextArea.value =
`fn main() {
printf("Hello, World!\\n")
}`
sourceTextArea.onkeydown = onKey
runButton.onclick = onRun
var Module = {'print': onPrint, 'printErr': onPrintErr}
function onKey(e)
{
var start = this.selectionStart
var end = this.selectionEnd
if (e.key == 'Tab')
{
e.preventDefault()
this.value = this.value.substring(0, start) + ' '.repeat(tabWidth) + this.value.substring(end)
this.selectionStart = this.selectionEnd = start + tabWidth
}
else if (e.key == 'Enter')
{
e.preventDefault()
var indent = getLineIndent(start)
this.value = this.value.substring(0, start) + '\n' + ' '.repeat(indent) + this.value.substring(end)
this.selectionStart = this.selectionEnd = start + 1 + indent
}
else if (e.key == '(')
{
e.preventDefault()
this.value = this.value.substring(0, start) + '()' + this.value.substring(end)
this.selectionStart = this.selectionEnd = start + 1
}
else if (e.key == '[')
{
e.preventDefault()
this.value = this.value.substring(0, start) + '[]' + this.value.substring(end)
this.selectionStart = this.selectionEnd = start + 1
}
else if (e.key == '{')
{
e.preventDefault()
this.value = this.value.substring(0, start) + '{}' + this.value.substring(end)
this.selectionStart = this.selectionEnd = start + 1
}
else if (e.key == '"')
{
e.preventDefault()
this.value = this.value.substring(0, start) + '""' + this.value.substring(end)
this.selectionStart = this.selectionEnd = start + 1
}
else if (e.key == "'")
{
e.preventDefault()
this.value = this.value.substring(0, start) + "''" + this.value.substring(end)
this.selectionStart = this.selectionEnd = start + 1
}
}
function onPrint(text)
{
outputTextArea.value += text + '\n'
}
function onPrintErr(text)
{
onPrint(text)
var words = text.split(' ')
if (words[0] != "Error" || typeof(words[2]) == "undefined")
return
var lineNum = words[2].replace(',', '').replace('(', '')
selectLine(lineNum)
}
function onRun()
{
runPlayground = Module.cwrap('runPlayground', 'number', ['string', 'string'])
source = sourceTextArea.value
outputTextArea.value = ""
runPlayground("playground.um", source)
}
function selectLine(lineNum)
{
lineNum--
var lines = sourceTextArea.value.split("\n")
var startPos = 0, endPos = sourceTextArea.value.length
for (var i = 0; i < lines.length; i++)
{
if (i == lineNum)
break;
startPos += lines[i].length + 1
}
var endPos = lines[lineNum].length + startPos
if (typeof(sourceTextArea.selectionStart) != "undefined")
{
sourceTextArea.focus()
sourceTextArea.selectionStart = startPos
sourceTextArea.selectionEnd = endPos
return true
}
return false
}
function getLineIndent(curPos)
{
var lineStart = sourceTextArea.value.substring(0, curPos).lastIndexOf('\n') + 1
var indent = 0
for (var i = lineStart; i < curPos; i++)
{
if (sourceTextArea.value[i] == ' ')
indent++
else if (sourceTextArea.value[i] == '\t')
indent += tabWidth
else
break
}
return indent
}
</script>
<script src="playground/umka.js"></script>
</body>
</html>