-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtamgucalc.tmg
804 lines (748 loc) · 22.3 KB
/
tamgucalc.tmg
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/@
Project : Spreadsheet
filename : tamgucalc.tmg
Date : 2020/06/15
Purpose : spreadsheet for terminals with Lisp expressions
Programmer : Claude ROUX (claude.roux@naverlabs.com)
@/
//The Lisp interpreter
lisp lisp_interpreter;
string msgerr;
//Color definition to highlight parenthesis balancing
string colorparenth = _sys.colors(7,31,49, false);
string colorsel = _sys.colors(1, 31, 49, false);
string colornrm = _sys.colors(0,0,0,false);
int inputsection = 6;
//Initialisation: coords contains your terminal dimensions
ivector coords = _sys.screensize();
//columnsize is the size of columns. You can modify this value
int columnsize = 10;
//We initialise the number of lines and columns according to your terminal size and columnsize
//We keep 'inputsection' lines below the last line to display messages
int x_viewsize = coords[0]-inputsection;
//The number of columns depends on the number of columns on the screen / the chosen columnsize
int y_viewsize = coords[1]/columnsize;
//We keep these values for comparison when the size is modified
int x_maxview = x_viewsize;
int y_maxview = y_viewsize;
//These two values define the actual matrix size, which is different from viewsize
//If the view port is larger than 101 and 51, then the matrix is equal to the viewport
int x_max = max(101,x_maxview);
int y_max = max(51,y_maxview);
//codeline is used to position messages
//'inputsection' lines down after the last line of the matrix
int codeline = x_viewsize+inputsection;
//Actual boundaries of the table
//These bounderies define the square in which values have been entered
int x_bound=0;
int y_bound=0;
//v_matrix is the visualisation matrix
vector v_matrix;
//formulas records the different Lisp expressions
mapss formulas;
//A few predefined methods
\(defun put (i j k) (key (key v_matrix i) j (string k)))
\(defun average (x) (/ (sum x) (size x)))
//mat is the actual matrix in which computing is done...
fmatrix mat(x_max,y_max);
//inputs is used to handle modifications on: filenames and size.
//If you want to enrich tamgucalc, you might add a new key
mapss inputs;
string inputkey;
string inputvalue;
inputs["Size"] = ""+(x_viewsize-1)+":"+(y_viewsize-1);
inputs["File"] = _args[0];
inputs["Go(r,c)"] = "1,1";
if (_args[0] != "")
inputs["Data(raw csv)"] = _args[0]+".csv";
else
inputs["Data(raw csv)"] = "";
//initialisation of v_matrix
function initialisation() {
int i,j;
for (i in <x_max>) {
v_matrix[i] = [];
for (j in <y_max>) {
if (!i) {
v_matrix[i].push(string(j));
}
elif (!j) {
v_matrix[i].push(string(i));
}
else
v_matrix[i].push("0");
}
}
}
//Loading a file
function readtable(string f) {
int i,j;
for (i in <1,x_max>) {
for (j in <1,y_max>) {
v_matrix[i][j] = "0";
}
}
try {
svector sv;
svector cut;
int nb;
string s,k,e;
sv.read(f);
bool beg = true;
i=0;
for (s in sv) {
cut=s.split("\t");
if (beg == true) {
//we have stored some parameters in the three first cells
x_viewsize = cut[0];
y_viewsize = cut[1];
columnsize = cut[2];
x_max = cut[3];
y_max = cut[4];
x_bound = cut[5];
y_bound = cut[6];
inputs["Size"] = ""+(x_viewsize-1)+":"+(y_viewsize-1);
codeline = x_viewsize+inputsection;
mat.dimension(x_max,y_max);
beg=false;
//we need to reset them
cut[0] = '0';
cut[1] = '1';
cut[2] = '2';
cut[3] = '3';
cut[4] = '4';
cut[5] = '5';
cut[6] = '6';
}
j = 0;
for (e in cut) {
e=e.trim();
if (e[0] == "(") {
k = i+"_"+j;
formulas[k] = e;
v_matrix[i][j] = "0";
}
else
v_matrix[i][j] = e;
j++;
}
i++;
}
evaluation(0,0);
}
catch {
initialisation();
displayall(0,0);
}
}
//Writing the visual matrix content to a file
function writetable(string f) {
if (f=="") {
displaymessage("Abort");
return;
}
string k;
int x_bound = x_bound;
int y_bound=max(y_bound,7);
displaymessage("Writting: "+f);
file sv(f,"w");
int i,j;
string w;
v_matrix[0][0] = string(x_viewsize);
v_matrix[0][1] = string(y_viewsize);
v_matrix[0][2] = string(columnsize);
v_matrix[0][3] = string(x_max);
v_matrix[0][4] = string(y_max);
v_matrix[0][5] = string(x_bound);
v_matrix[0][6] = string(y_bound);
for (i in <x_bound>) {
for (j in <y_bound>) {
if (j)
sv.write("\t");
k = i+"_"+j;
if (formulas.test(k))
sv.write(formulas[k]);
else {
w = v_matrix[i][j];
sv.write(w.trim());
}
}
sv.write("\n");
}
v_matrix[0][0] = "0";
v_matrix[0][1] = "1";
v_matrix[0][2] = "2";
v_matrix[0][3] = "3";
v_matrix[0][4] = "4";
v_matrix[0][5] = "5";
v_matrix[0][6] = "6";
}
//Writing only data to a CSV file. The result of formulas is stored
function writecsv(string f) {
if (f=="") {
displaymessage("Abort");
return;
}
if ('.csv' not in f)
f+=".csv";
displaymessage("Writting (csv): "+f);
file sv(f,"w");
int i,j;
string k;
string dsp;
for (i in <1,x_bound>) {
for (j in <1,y_bound>) {
if (j!=1)
sv.write("\t");
dsp = v_matrix[i][j];
dsp = dsp.trim();
if (dsp[-1] == '!')
dsp=dsp[:-1];
if (!dsp[0].isdigit())
sv.write(dsp.json());
else
sv.write(dsp);
}
sv.write("\n");
}
}
//------------------- The Code ---------
//Formulas evaluation
function evaluation(int off_x, int off_y) {
mat.clear();
string ky;
int i,j;
for (i in <1,x_bound>) {
for (j in <1,y_bound>) {
ky = i+"_"+j;
if (formulas.test(ky)) {
mat[i:j] = 0;
}
else {
mat[i:j] = float(v_matrix[i][j]);
}
}
}
if (formulas.size()==0) {
displayall(off_x,off_y);
return;
}
msgerr="";
string msg;
float val;
int o;
bool stop=false;
//We record function beforehand
for (ky in formulas) {
i = ky[:"_"][:-1];
j = ky["_":][1:];
if ("defun " in formulas[ky]) {
try {
lisp_interpreter.eval(formulas[ky]);
v_matrix[i][j] = "#"+formulas[ky][" ":"("][:-1].trim();
}
catch;
}
}
//Not very efficient, we apply until nothing is modified again
for (o in <formulas.size()>) {
stop=true;
for (ky in formulas) {
//functions are skipped
if ("defun " in formulas[ky])
continue;
i = ky[:"_"][:-1];
j = short(ky["_":][1:]);
try {
val = lisp_interpreter.eval(formulas[ky]);
if (val != mat[i:j])
stop=false;
mat[i:j] = val;
v_matrix[i][j] = string(val);
v_matrix[i][j]+="!";
}
catch(msg) {
msgerr=msg;
v_matrix[i][j] = "#ERR";
}
}
if (stop)
break;
}
displayall(off_x, off_y);
displayerr(msgerr);
}
//Display methods
//Display one element, we check its size
function dispelement(int i,int j, int off_x, int off_y) {
string u = v_matrix[i+off_x][j+off_y];
if (u.size() >= columnsize-1)
u=u[:columnsize-2]+"_";
print(u);
}
//Displaying all elements on screen
function displayall(int off_x, int off_y) {
int i,j;
_sys.cls();
_sys.colors(7,96,40);
for (i in <x_viewsize>) {
_sys.row_column(i+1,0);
dispelement(i,0, off_x, 0);
}
for (j in <y_viewsize>) {
_sys.row_column(0,columnsize*j);
dispelement(0,j, 0, off_y);
}
_sys.colors(0,0,0);
for (i in <1,x_viewsize>) {
for (j in <1,y_viewsize>) {
_sys.row_column(i+1,columnsize*j);
dispelement(i,j, off_x, off_y);
}
}
}
//Error message is displayed on line 4
function displayerr(string s) {
_sys.row_column(codeline-4,0);
_sys.eraseline(2);
print(s);
}
//Messages are displayed on line 3
function displaymessage(string s) {
_sys.row_column(codeline-3,0);
_sys.eraseline(2);
print(s);
}
//Highlight the current element
function showelement(int i, int j, int off_x, int off_y) {
string dsp;
int I = i+off_x;
int J = j+off_y;
string ky = I+"_"+J;
//We check if we are in a formula mode
if (inputkey != "")
dsp = inputkey+": "+inputvalue;
elif (formulas.test(ky)) {
dsp = I+","+J+": "+formulas[ky];
}
else {//When we have a 0, we do not display it
dsp = v_matrix[I][J];
if (dsp.trim()=="")
v_matrix[I][J]="0";
if (v_matrix[I][J] == '0')
dsp = I+","+J+": ";
else {
dsp = I+","+J+": "+v_matrix[I][J];
}
}
//we use spaces to clean the cell first
string space;
space.padding(columnsize," ");
_sys.row_column(i+1,columnsize*j);
print(space);
//Then we position our cursor again to display our value
_sys.row_column(i+1,columnsize*j);
//We use a specific color to show that it has been selected
_sys.colors(7,31,49);
dispelement(i,j, off_x, off_y);
//Colors are reset, we then display on the last line the current value
_sys.colors(0,0,0);
_sys.row_column(codeline-1,0);
_sys.eraseline(2);
print(dsp);
}
//----------------------------------------------------------------------------------------------------
//MAIN LOOP
//the line 0 and the column 0 are used to display coordinates...
int i = 1;
int j = 1;
int I, J;
int off_x = 0;
int off_y = 0;
function handlingmovement() {
bool redisplay=false;
if (i < 1) {
i=1;
if (off_x > 0) {
off_x--;
redisplay=true;
}
}
else
if (i > x_viewsize-1) {
i = x_viewsize-1;
if ((off_x+i) < x_max-1) {
off_x++;
redisplay=true;
}
}
if (j < 1) {
j = 1;
if (off_y > 0) {
off_y--;
redisplay=true;
}
}
else
if (j > y_viewsize-1) {
j = y_viewsize-1;
if ((off_y+j) < y_max-1) {
off_y++;
redisplay=true;
}
}
if (redisplay)
displayall(off_x, off_y);
showelement(i,j, off_x, off_y);
}
//If a filename is provided, we try to read the file, otherwise we initialise our structure
initialisation();
if (inputs["File"]=="")
displayall(0,0);
else
readtable(inputs["File"]);
string msgform="%1Arrows%2 to select cell. %1Enter%2 to record. %1..%2 to define a range.";
msgform=msgform.format(colorsel,colornrm);
string helpmsg = "%1'('%2:Formula %1Ctrl-s%2:Save %1Ctrl-w%2:Save as %1Crld-d%2:Save Data %1Ctrl-r%2:Resize %1Ctrl-g%2:Goto %1Ctrl-q%2:Quit";
helpmsg = helpmsg.format(colorsel,colornrm);
string msgbase = "%1Ctrl-b%2: Help";
msgbase = msgbase.format(colorsel,colornrm);
displaymessage(msgbase);
showelement(i,j, off_x, off_y);
string s=_sys.getchar();
bool selection = false;
string forme, ky;
int ci=-1,cj=-1;
int lasti, lastj;
//17 is ctrl-q
while (s[0].ord() != 17) {
I = i+off_x;
J = j+off_y;
//We use this position to display displaymessages
displaymessage(msgbase);
ky = I+"_"+J;
if (s[0].ord() == 2) {
displaymessage(helpmsg);
showelement(i,j, off_x, off_y);
s=_sys.getchar();
continue;
}
//Using arrows
if (s[0].ord() == 27) {
if (s.size() == 1) {
//We abort an inputkey...
if (inputkey != "") {
inputkey="";
inputvalue="";
}
showelement(i,j, off_x, off_y);
s=_sys.getchar();
continue;
}
_sys.row_column(i+1,columnsize*j);
dispelement(i,j, off_x, off_y);
if (s == _sys_keydel) {
if (not selection) {
v_matrix[I][J] = "0";
if (formulas.test(ky))
formulas.pop(ky);
}
elif (I == ci and J == cj) {
v_matrix[I][J] = "0";
formulas.pop(ky);
selection = false;
}
showelement(i,j, off_x, off_y);
s = _sys.getchar();
continue;
}
elif (s == _sys_keyright)
j++;
elif (s == _sys_keyleft)
j--;
elif (s == _sys_keyup)
i--;
elif (s == _sys_keydown)
i++;
handlingmovement();
s = _sys.getchar();
continue;
}
//character deletion with the delete key
if (s.ord() == 127) {
if (inputkey!="") {
inputvalue=inputvalue[:-1];
}
else {
//We are either modifying a formula or a cell
if (formulas.test(ky)) {
formulas[ky] = formulas[ky][:-1];
if (formulas[ky] == "") {
//The formula has been deleted
formulas.pop(ky);
v_matrix[I][J] = "0";
selection=false;
ci =-1;
cj =-1;
_sys.row_column(i+1,columnsize*j);
//We display the "0" instead
dispelement(i,j, off_x, off_y);
}
else {
selection=true;
ci = I;
cj = J;
}
}
else {
if (!selection) {
if (v_matrix[I][J].size() > 1)
v_matrix[I][J] = v_matrix[I][J][:-1];
else
v_matrix[I][J] = "0";
}
}
}
showelement(i,j, off_x, off_y);
s=_sys.getchar();
continue;
}
//When introducing a formula, selection is true and the key is the formula position
if (selection) {
displaymessage(msgform);
ky = ci+"_"+cj;
}
//When hitting the Carriage Return key
if (s == "\n") {
if (inputkey=="Go(r,c)") {
int pi = inputvalue[:","][:-1];
int pj = inputvalue[",":][1:];
pi = max(1,pi);
pj = max(1,pj);
pi = min(x_max-1,pi);
pj = min(y_max-1,pj);
inputkey="";
inputvalue="";
off_x = max(off_x+pi-I,0);
off_y = max(off_y+pj-J,0);
I = pi;
J = pj;
i = I-off_x;
j = J-off_y;
displayall(off_x,off_y);
}
elif (inputkey=="Size") {
ivector iv = inputvalue.split(":");
if (iv == 2) {
int x = iv[0];
int y = iv[1];
x++;
y++;
if (x > x_maxview) {
x = x_maxview;
}
else {
if (x <= 1) {
x = 2;
}
}
if (y > y_maxview) {
y = y_maxview;
}
else {
if (y <= 1) {
y = 2;
}
}
x_viewsize = x;
y_viewsize = y;
inputs[inputkey] = ""+(x_viewsize-1)+":"+(y_viewsize-1);
codeline = x_viewsize+inputsection;
i = 1;
I = off_x+1;
j = 1;
J = off_y+1;
displayall(off_x, off_y);
}
}
elif (inputkey == "Data(raw csv)") {
inputs[inputkey] = inputvalue;
writecsv(inputs[inputkey]);
}
elif (inputkey=="File") {
inputs[inputkey] = inputvalue;
writetable(inputs[inputkey]);
}
else {
//If we are in selection mode (formula) then we enable the cell selection with the arrows
if (selection) {
string subkey;
//We test if a ".." has been introduced, in that case, we compute the interval between the structures
if (lasti != -1 && formulas[ky][-2:] == '..') {
if (lasti > I)
[lasti,I] = [I,lasti];
if (lastj > J)
[lastj,J] = [J,lastj];
string frm;
int n;
bool beg=true;
formulas[ky] = formulas[ky][:-2];
//a single line
if (lasti == I) {
frm = "mat["+I+":]["+lastj+":"+(J+1)+"]";
formulas[ky][-"mat":] = frm;
}
elif (lastj == J) { //a single column
frm = "mat[:"+J+"]["+lasti+":"+(I+1)+"]";
formulas[ky][-"mat":] = frm;
}
else { //a square
n = lasti;
//&&& is the merge operator
frm = "(&&& ";
//we will add several lines together
while (n <= I) {
if (!beg)
frm += " ";
else
beg=false;
frm += "mat["+n+":]["+lastj+":"+(J+1)+"]";
n++;
}
frm += ")";
formulas[ky][-"mat":] = frm;
}
}
else
//One element at a time
formulas[ky] += "mat["+I+":"+J+"] ";
lasti=I;
lastj=J;
i = ci-off_x;
j = cj-off_y;
}
else {
displaymessage("eval");
evaluation(off_x, off_y);
_sys.row_column(i+1,columnsize*j);
//You can add new values with the CR key
//We showelement it to remove the selection color
dispelement(i,j, off_x, off_y);
//We move to the next cell below
i++;
handlingmovement();
}
}
inputkey="";
inputvalue="";
showelement(i,j, off_x, off_y);
s=_sys.getchar();
continue;
}
if (s[0].ord() < 27) {
if (s[0].ord() == 4) {
//ctrl-d
inputkey="Data(raw csv)";
inputvalue=inputs[inputkey];
print(inputkey+": ",inputvalue);
}
elif (s[0].ord() == 7) {
inputkey = "Go(r,c)";
inputvalue = "";
print(inputkey+": ",inputvalue);
}
elif (s[0].ord() == 19 and inputs["File"] != "") //ctrl-s
writetable(inputs["File"]);
elif (s[0].ord() == 19 or s[0].ord() == 23) { //ctrl-w/s
_sys.row_column(codeline-1,0);
inputkey="File";
inputvalue=inputs[inputkey];
print(inputkey+": ",inputvalue);
}
elif (s[0].ord() == 18) { //ctrl-r
inputkey="Size";
inputvalue=inputs[inputkey];
print(inputkey+": ",inputvalue);
}
showelement(i,j, off_x, off_y);
s=_sys.getchar();
continue;
}
if (inputkey == "" and formulas.test(ky) and ci == I and cj == J) {
formulas[ky]+=s;
formulas[ky] = formulas[ky].replace(colorparenth,"");
formulas[ky] = formulas[ky].replace(colornrm,"");
//When the "(...)" balances, we exit the selection mode
if (formulas[ky].count('(') > formulas[ky].count(")")) {
if (s == ')') {
int e = formulas[ky].size()-2;
while (e>0 and formulas[ky][e:].count("(") != formulas[ky][e:].count(")")) e--;
formulas[ky] = formulas[ky][:e]+colorparenth+formulas[ky][e]+colornrm+formulas[ky][e+1:];
}
selection = true;
displaymessage(msgform);
}
else {
selection=false;
displaymessage('Ready to compute!!!');
}
showelement(i,j, off_x, off_y);
s=_sys.getchar();
continue;
}
//We detect a '(', we enter the formula mode
if (s=='(' and !selection) {
ci = I;
cj = J;
lasti=-1;
lastj=-1;
formulas[ky] = s;
x_bound = max(I+1,x_bound);
y_bound = max(J+1,y_bound);
selection = true;
displaymessage(msgform);
showelement(i,j, off_x, off_y);
s=_sys.getchar();
continue;
}
//In case of pasting code
if (s.size() > 2 and s[0] == '(') {
s=s.trim();
string u,k;
//We try to detect from which place it was copied
for (k in formulas) {
if (formulas[k].trim() == s) {
u=k;
break;
}
}
if (u != "") {
u["_"]=":";
string l= u[:":"];
string c= u[":":];
string rl = I+":";
string rc = ":"+J;
s=s.replace(l,rl);
s=s.replace(c,rc);
formulas[ky]=s;
showelement(i,j, off_x, off_y);
s=_sys.getchar();
continue;
}
}
if (inputkey != "")
inputvalue += s;
else {
if (not selection) {
if (v_matrix[I][J] == '0')
v_matrix[I][J]=s;
else
v_matrix[I][J]+=s;
x_bound = max(I+1,x_bound);
y_bound = max(J+1,y_bound);
}
}
showelement(i,j, off_x, off_y);
s=_sys.getchar();
continue;
}