-
Notifications
You must be signed in to change notification settings - Fork 1
/
False.scala
293 lines (274 loc) · 6.44 KB
/
False.scala
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import scala.collection.mutable.Stack
import scala.collection.mutable.HashMap
object False {
def main(args: Array[String]) {
println("Sort");
False START "6 9 5 8 7 49 1 7s:1i:[s;0>][1i:[s;i;>][$i;1+ø\\>~[i;`]?i;1+i:]#.\" \"s;1-s:]#"
println("Prime number from 1 to 100");
False START "[\\$@$@\\/*-0=]d: {Test if p divides q}[$2/[\\$@$@\\d;!~][1-]#1=\\%]p: {Is p prime?}[[$1=~][$p;![$.\" \"]?1-]#]f: {for all i from n to 2 do { if i is prime then print i} }99f;!";
println("16 Fibonacci");
False START "0i:1a:1b:[i;16=~][a;$.\", \"$b;$a:+b:i;1+i:]#\"...\"";
println("Quine");
False START "[\"'[,34,$!34,'],!\"]'[,34,$!34,'],!";
println("9!");
False START "[$1=$[\\%1\\]?~[$1-f;!*]?]f:9f;!.";
println("reverse input list");
False START "[][^$1_=~][[.!]]#%!";
//False START "ß[^$1_=~][,]#";
//False START "1a:[a;5\\>][a;1+a:]#a;
}
//The main prog
var prog = Array.ofDim[Char](0);
//The stack
val stack = new Stack[String];
//Map of the variables
var map = new HashMap[String, String];
def START(mainProg: String) = {
var muProg = mainProg;
prog = muProg.toCharArray;
var i = 0;
while(i < prog.length) {
val cur = prog(i);
if(cur.isDigit) {
val strNum:String = getInt(i);
stack.push(strNum);
i = i + strNum.length-1;
} else if(cur.isLetter) {
stack.push(""+cur);
}
cur match {
//Comment
case '{' => i = i+getComment(i+1)+1
//Arithmetic
case '_' => stack.push((-1 * stack.pop.toInt).toString)
case '+' => stack.push((stack.pop.toInt + stack.pop.toInt).toString)
case '-' => {
swap
stack.push((stack.pop.toInt - stack.pop.toInt).toString)
}
case '*' => stack.push((stack.pop.toInt * stack.pop.toInt).toString)
case '/' => {
swap
stack.push((stack.pop.toInt / stack.pop.toInt).toString)
}
//conditional
case '=' => {
if(stack.pop == stack.pop)
stack.push("-1");
else
stack.push("0");
}
case '~' => {
if(stack.pop == "0")
stack.push("-1");
else
stack.push("0");
}
case '>' => {
val a = stack.pop.toInt;
val b = stack.pop.toInt;
if(b > a)
stack.push("-1");
else
stack.push("0");
}
case '&' => {
val a = stack.pop.toInt;
val b = stack.pop.toInt;
if(a == 0 || b == 0)
stack.push("0");
else
stack.push("-1");
}
case '|' => {
val a = stack.pop.toInt;
val b = stack.pop.toInt;
if(a == -1 || b == -1)
stack.push("-1");
else
stack.push("0");
}
//stack operations
case '$' => stack.push(stack.top)
case '%' => stack.pop
case '\\' => swap
case '@' => {
val first = stack.pop;
val second = stack.pop;
val third = stack.pop;
stack.push(second);
stack.push(first);
stack.push(third);
}
case 'ø' => {
stack.pop;
val n = stack.pop;
val ar:Array[String] = stack.toArray;
stack.push(ar(n.toInt));
}
//Extension of ø rotates the nth element to the top instead of copies it
case '`' => {
var tempL:Stack[String] = new Stack[String];
val n = stack.pop.toInt;
for(i:Int <- 0 until n) {
tempL.push(stack.pop);
}
val res = stack.pop;
for(i:Int <- 0 until tempL.length) {
stack.push(tempL.pop);
}
stack.push(res);
}
case ''' => {
val char = prog(i+1);
stack.push(char.toInt.toString);
i = i+1;
}
//variable assignment
case ':' => {
val key = stack.pop;
val value = stack.pop;
map.put(key, value);
}
case ';' => {
val key = stack.pop;
stack.push(map.get(key).getOrElse("ERROR"));
}
//IO
case '.' => {
try {
print(stack.pop);
} catch {
case e:Exception => println("stack empty");
}
}
case ',' => {
val isANum = stack.pop;
try {
val char = isANum.toInt
print(char.toChar)
} catch {
case e:Exception => print(isANum);
}
}
case '"' => {
val s:String = getString(i+1);
print(s);
i = i+s.length+1;
}
case '^' => {
try {
val char = readChar;
stack.push(char.toString);
}
catch {
case e:Exception => stack.push("-1")
}
}
case 'ß' => {
Console.flush;
stack.pop();
}
//lambda
case '[' => {
val func:String = getFunc(i+1);
stack.push(func);
i = i+func.length+1;
}
case '!' => {
val func = stack.pop;
muProg = addToProg(i, muProg, func);
prog = muProg.toCharArray;
}
//control flow
case '?' => {
val func = stack.pop;
val cond = stack.pop;
if(cond == "-1") {
muProg = addToProg(i, muProg, func);
prog = muProg.toCharArray;
}
}
case '#' => {
val func = stack.pop;
val cond = stack.pop;
val loop = cond+"["+func+"["+cond+"]["+func+"]#]?";
muProg = addToProg(i, muProg, loop);
prog = muProg.toCharArray;
}
case _ => false
}
i = i+1;
//println(stack.toString+" "+stack.length);
}
println();
//println(stack.toString+" "+stack.length);
//println(map.toString);
}
def addToProg(i:Int, prog:String, func:String):String = {
val beg = prog.substring(0, i+1);
var end = "";
if(i+1 != prog.length) {
end = prog.substring(i+1, prog.length);
}
beg+func+end;
}
def swap() = {
val top:String = stack.pop;
val bot:String = stack.pop;
stack.push(top);
stack.push(bot);
}
def getInt(start:Int): String = {
var ret = new String;
for(i:Int <- start until prog.length) {
if(prog(i).isDigit) {
ret = ret + prog(i);
} else {
return ret;
}
}
ret
}
def getString(start:Int): String = {
var ret = new String;
for(i:Int <- start until prog.length) {
if(prog(i) != '"') {
ret = ret + prog(i);
} else {
return ret;
}
}
ret
}
def getComment(start:Int): Int = {
var length = 0;
for(i:Int <- start until prog.length) {
if(prog(i) != '}') {
length = length + 1;
} else {
return length;
}
}
length
}
def getFunc(start:Int): String = {
var ret = new String;
var bracket = new Stack[Char];
bracket.push('[');
for(i:Int <- start until prog.length) {
if(prog(i) == '[') {
bracket.push('[');
ret = ret + prog(i);
} else if(prog(i) == ']' && bracket.length == 1 && bracket.top == '[') {
return ret;
} else if(prog(i) == ']') {
bracket.pop;
ret = ret + prog(i);
} else {
ret = ret + prog(i);
}
}
ret
}
}