forked from Ahnfelt/funk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
118 lines (110 loc) · 3.04 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
<html>
<head>
<meta charset="UTF-8" />
<script src="parser.js"></script>
<script src="emitter.js"></script>
<script src="prelude.js"></script>
<style>
#controls {
position: absolute;
top: 0;
bottom: 30px;
left: 0;
right: 50%;
padding-top: 2px;
padding-left: 5px;
box-sizing: border-box;
font-family: verdana;
}
#controls-run {
font-weight: bold;
float: right;
}
#editor {
position: absolute;
top: 30px;
bottom: 0;
left: 0;
right: 50%;
}
#editor textarea {
width: 100%;
height: 100%;
}
#result {
position: absolute;
top: 0;
bottom: 0;
left: 50%;
right: 0;
padding: 10px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div id="controls">
<button id="controls-run" onclick="runFunkProgram()">Run</button>
<strong>Try Funk</strong>
</div>
<div id="editor">
<textarea id="editor-textarea" placeholder="Write some Funk code here, then press Run.">
:point {|x y| {
|X| x
|Y| y
|Add p| point(x + p.X, y + p.Y)
|Show| "(" + x + ", " + y + ")"
}}
:p1 point(5, 7)
:p2 point(1, 2)
:p3 p1.Add(p2)
system.SetText(p1.Show + " + " + p2.Show + " = " + p3.Show)
; Operators are implemented exactly like methods are.
; You can use "+" instead of Add, eg:
; |"+" p| point(x + p.X, y + p.Y)
; If you do that, then you can write "p1 + p2" instead of "p1.Add(p2)"
; Define your own control structures, eg: if(x > y) {"foo"} {"bar"}
:if {
|True t _| t()
|False _ e| e()
}
:when {
|True t| t()
|False _|
}
; Loops via recursion: while {x > y} {"blah"}
:while {|c b|
when(c()) {
b()
while(c, b)
}
}
:x new(10)
while {*x > 0} {
system.Log(*x)
x -= 1
}
</textarea>
</div>
<div id="result">
</div>
<script>
function runFunkProgram() {
var resultElement = document.getElementById('result');
resultElement.innerHTML = "";
var program = document.getElementById('editor-textarea').value;
try {
var parsed = parseFunk(program);
var emitted = emitFunk.emitProgram(parsed);console.log(emitted);
resultElement.style.color = "#000000";
resultElement.textContent = "";
eval(emitted);
} catch(e) {
resultElement.style.color = "#ff0000";
resultElement.textContent = e.toString();
throw e;
}
}
</script>
</body>
</html>