-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaqua.cpp
2722 lines (2404 loc) · 46.1 KB
/
aqua.cpp
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************
aqua.cpp
Created at: Jan 7, 2023, 7:41 AM GMT+9
Copyright (C) 2023 e6nlaq
This file is part of aqua.
Licensed under the MIT License.
*******************************************************/
// All comments are written in Japanese.
// Please understand.
// インクルード
#include "./lib/all.h"
// マクロ
#define co(x) std::cout << (x) << "\n"
#define cou(x) std::cout << (x)
#define err(x) errorlog(lines, code_line, (x));
#define all(x) x.begin(), x.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define sn(i) now(setting, (i));
#define jnx() js_next(line, linenum)
// optionとかの変数
bool op_funcskip = false;
bool op_stylereset = true;
bool op_end_anykey = false;
bool op_over_var = false;
bool st_using_yes = false;
bool st_style = true;
bool us_shell = false;
bool us_net = false;
bool us_clip = false;
bool us_sound = false;
bool tooljs = false;
bool toolpy = false;
// 変数
std::unordered_map<std::string, int> var_int;
std::unordered_map<std::string, std::string> var_string;
std::unordered_map<std::string, bool> var_bool;
std::unordered_map<std::string, double> var_double;
std::unordered_map<std::string, int64_t> var_int64;
std::unordered_map<std::string, ll> var_ll;
std::unordered_map<std::string, unsigned int> var_uint;
std::unordered_map<std::string, ll> label_list;
std::unordered_set<ll> nx_line;
std::unordered_set<std::string> const_var = {"true", "false", "True", "False"};
std::vector<std::string> lines;
std::vector<int> sett;
std::vector<ll> while_line;
std::vector<ll> until_line;
bool runcode = true;
bool comment = false;
bool iswin = true;
bool clirun = false;
int linenume;
int inc_now = 0;
int inc_code = 0;
int isnx = 0;
int isjnx = 0;
ll code_line = 0;
ll forever_line = -1;
ll if_count = 0;
ll while_count = 0;
ll until_count = 0;
const std::string version = "1.7.0 Preview 2";
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf32conv;
// 事前宣言
inline void errorlog(std::vector<std::string> line, int linenum, int errorcode);
inline std::string nx();
inline std::string to_js(std::string script, std::vector<std::string> line, int linenum);
inline void warn(std::string s)
{
if (st_style)
{
std::cout << "\033[33m\033[1mWarning\033[m: ";
}
else
{
std::cout << "Warning: ";
}
std::cout << s << "\n";
}
inline bool dup_varname(std::string name)
{
if (var_int.count(name) || var_string.count(name) || var_bool.count(name) || var_double.count(name) || var_int64.count(name) || var_ll.count(name))
{
return false;
}
return true;
}
inline int var_search(std::string name)
{
if (var_int.count(name))
{
return 1;
}
else if (var_string.count(name))
{
return 2;
}
else if (var_bool.count(name))
{
return 3;
}
else if (var_double.count(name))
{
return 4;
}
else if (var_int64.count(name))
{
return 5;
}
else if (var_ll.count(name))
{
return 6;
}
else if (var_uint.count(name))
{
return 7;
}
else
{
return 0;
}
}
inline bool m_numt(std::string name)
{
const int tmp = var_search(name);
return (tmp == 1 || tmp == 4 || tmp == 5 || tmp == 6);
}
inline std::string var_value(std::string name)
{
switch (var_search(name))
{
case 1:
return std::to_string(var_int[name]);
break;
case 2:
return var_string[name];
break;
case 3:
return std::to_string(var_bool[name]);
break;
case 4:
return std::to_string(var_double[name]);
break;
case 5:
return std::to_string(var_int64[name]);
break;
case 6:
return std::to_string(var_ll[name]);
break;
case 7:
return std::to_string(var_uint[name]);
break;
default:
errorlog(lines, linenume, 10);
break;
}
return "";
}
// エラー
inline void errorlog(std::vector<std::string> line, int linenum, int errorcode)
{
std::cout << "\n\nError: ";
switch (errorcode)
{
case 0:
co("Example Error"); // 未使用
break;
case 1:
co("Invalid function passed."); // 存在しない関数
break;
case 2:
co("Invalid argument."); // 無効な引数
break;
case 3:
co("Invalid option."); // 存在しないoption
break;
case 4:
co("User-defined exception thrown"); // throwエラー
break;
case 5:
co("Invalid error code"); // 無効なエラーコード
break;
case 6:
co("invalid type"); // 無効な型
break;
case 7:
co("Invalid Style"); // 無効なスタイル
break;
case 8:
co("Invalid name"); // 無効な変数名
break;
case 9:
co("Overlapping variables"); // 重複している変数
break;
case 10:
co("Non-existent variable"); // 存在しない変数
break;
case 11:
co("Division by zero is not allowed"); // ゼロ除算
break;
case 12:
co("There is no square root of a negative number"); // 負の数の平方根
break;
case 13:
co("Invalid Variable"); // 無効な変数
break;
case 14:
co("Missing argument"); // 引数が足りない
break;
case 15:
co("You can't use : here."); // 無効な場所の:
break;
case 16:
co("You can't break here."); // 無効な場所のbreak
break;
case 17:
co("goto must be greater than or equal to 1"); // 0以下の行数
break;
case 18:
co("This is only valid within the FOREVER"); // forever無いのにend forever
break;
case 19:
co("I can't find a sentence that fits the end"); // 対応するendがない
break;
case 20:
co("std::strings are invalid for this function."); // 引数に文字列
break;
case 21:
co("No access rights"); // アクセス拒否
break;
case 22:
co("Duplicate labels"); // ラベルの重複
break;
case 23:
co("Invalid label name"); // 無効なラベル名
break;
case 24:
co("nonexistent label"); // 存在しないラベル
break;
case 25:
co("Decimals not available"); // 利用できない小数
break;
case 26:
co("The value must be smaller on the left."); // A>Bのとき
break;
case 27:
co("Variables of invalid type"); // 無効な引数の変数の型
break;
case 28:
co("The length of the std::string must be 1."); // 文字列の長さが1(char)ではないとき
break;
case 29:
co("Numerical values are invalid."); // 数値が無効
break;
case 30:
co("Out of Range"); // 範囲外
break;
case 31:
co("There is no if corresponding to this end if."); // ifがないのにend if
break;
case 32:
co("There is no corresponding while for this end while."); // whileがないのにend while
break;
case 33:
co("There is no if corresponding to this else."); // ifがないのにelse
break;
case 34:
co("There is no corresponding until for this end until."); // untilがないのにend until
break;
case 35:
co("Aqua Tools does not exist"); // Aqua Toolsがない
break;
case 36:
co("Requires Aqua Tools to run."); // Aqua Toolsが必要
break;
case 37:
co("In JavaScript, you can use `http://... ` is not supported."); // Node.jsでhttpでの通信
break;
case 38:
co("Spaces are not allowed at the end of file or folder names."); // ファイルまたはフォルダ名の末尾に空白
break;
case 39:
co("Python is required for execution."); // Pythonが必要
break;
case 40:
co("Node.js is required for execution."); // Node.jsが必要
break;
case 41:
co("C++ runtime error"); // C++実行時エラー
break;
case 42:
co("Negative numbers are not allowed."); // 負の数は使用できません
break;
case 43:
co("Must be a std::string or array."); // 文字列又は配列である必要があります。
break;
case 44:
co("Argument must be a variable"); // 引数は変数である必要があります
break;
case 45:
co("Initial value of const not set"); // constの初期値未設定
break;
case 46:
co("Change of const.");
break;
default:
err(5);
return;
break;
}
#pragma region Error Messages
co("The program ended with code " + std::to_string(errorcode));
co("Line " + std::to_string(linenum + 1));
co("Error location\n");
// エラーの周りの行を表示するやつ
if (sett[0])
{
if (linenum > 1)
co(std::to_string(linenum - 1) + "| " + line[linenum - 2]);
if (linenum > 0)
co(std::to_string(linenum) + "| " + line[linenum - 1]);
co(std::to_string(linenum + 1) + "| \033[1m\033[31m\033[4m" + line[linenum] + "\033[m");
if (linenum != line.size() - 1)
co(std::to_string(linenum + 2) + "| " + line[linenum + 1]);
if (linenum + 2 < line.size())
co(std::to_string(linenum + 3) + "| " + line[linenum + 2]);
}
else
{
co(std::to_string(linenum + 1) + "| " + line[linenum]);
}
if (!clirun)
exit(1);
#pragma endregion
}
inline void usinglog(int id)
{
// usingのやつ
std::string ans = "";
if (!st_using_yes)
{
cscle();
co("-----------------------------------------------------------------------");
co("A request was received from this Aqua script.");
co("By accepting this request, the following will become available\n");
switch (id)
{
case 1:
co("Shell");
co("All operations on this OS and PC");
break;
case 2:
co("Network");
co("Connect to the Internet and retrieve information");
break;
case 3:
co("Clipboard");
co("Writing to and reading from the clipboard");
break;
case 4:
co("Sound");
co("Sound reproduction");
break;
}
co("\nAre you sure you want to do this? (Y/n)");
cou("> ");
std::cin >> ans;
transform(all(ans), ans.begin(), ::tolower); // StackOverflowから
}
if (st_using_yes || ans == "y" || ans == "yes")
{
switch (id)
{
case 1:
us_shell = true;
break;
case 2:
us_net = true;
break;
case 3:
us_clip = true;
break;
case 4:
us_sound = true;
break;
}
}
if (!st_using_yes)
cscle();
}
inline std::string f_math(int id, std::string s, std::string t)
{
// いろんな関数が使ってるやつ
ld a, b;
if (isstring(s) || isstring(t))
err(20);
if (isvarok(s))
a = stold(var_value(s));
else if (s == ":")
a = stold(nx());
else
a = stold(s);
if (isvarok(t))
b = stold(var_value(t));
else if (t == ":")
b = stold(nx());
else
b = stold(t);
switch (id)
{
case 1: // +
return std::to_string(a + b);
break;
case 2: // -
return std::to_string(a - b);
break;
case 3: // *
return std::to_string(a * b);
break;
case 4: // /
if (b != 0)
return std::to_string(a / b);
else
err(11);
break;
case 5: // %
if (b != 0)
return std::to_string(fmodl(a, b));
else
err(11);
break;
case 6: // ^
return std::to_string(pow(a, b));
break;
case 7: // >
return std::to_string(a > b);
break;
case 8: // <
return std::to_string(a < b);
break;
case 9: // <=
return std::to_string(a <= b);
break;
case 10: // >=
return std::to_string(a >= b);
break;
case 11: // gcd
return std::to_string(a_gcd((ll)a, (ll)b));
break;
case 12: // lcm
return std::to_string(a_lcm((ll)a, (ll)b));
break;
case 13: // and
return std::to_string((ll)a & ll(b));
break;
case 14: // or
return std::to_string((ll)a | ll(b));
break;
case 15: // xor
return std::to_string((ll)a ^ ll(b));
break;
case 16: // rand
if (a > b)
{
std::swap(a, b);
}
// rand_r使えって言われるけどVC++で使えないから却下
return std::to_string(rand() % ((ll)b - (ll)a + 1LL) + (ll)a);
break;
case 17: // lsh
return std::to_string((ll)a << (ll)b);
break;
case 18: // rsh
return std::to_string((ll)a >> (ll)b);
break;
case 19: // log
if (a < 0 || b < 0)
err(12);
return std::to_string(log_a(a, b));
break;
}
return "";
}
inline std::string f_trig(int id, std::string s)
{
ld a;
if (isvarok(s))
a = stold(var_value(s));
else if (s == ":")
a = stold(nx());
else
a = stold(s);
std::u32string k;
switch (id)
{
case 1: // abs
return std::to_string(abs(a));
break;
case 2: // sqrt
if (a < 0)
err(12);
return std::to_string(sqrtl(a));
break;
case 3: // sin
return std::to_string(sinl(a));
break;
case 4: // cos
return std::to_string(cosl(a));
break;
case 5: // tan
return std::to_string(tanl(a));
break;
case 6: // is_prime
return std::to_string(is_prime((ll)a));
break;
case 7: // not
return std::to_string(!a);
break;
case 8: // chr
k = {(char32_t)a};
#ifdef WINDOWS
return utf8_to_ansi(utf32conv.to_bytes(k));
#else
return utf32conv.to_bytes(k);
#endif
break;
case 9: // fact
if (a < 0)
err(12);
return std::to_string(fact((ll)a));
break;
}
return "";
}
inline bool to_bool(std::string s)
{
bool ret;
if (isvarok(s))
{
ret = stob(var_value(s));
}
else if (s == ":")
ret = stob(nx());
else
{
ret = stob(s);
}
return ret;
}
inline ld to_num(std::string s)
{
ld ret;
if (isvarok(s))
{
ret = stold(var_value(s));
}
else if (s == ":")
ret = stold(nx());
else
{
try
{
ret = stold(s);
}
catch (const std::exception &e)
{
err(20);
}
}
return ret;
}
inline std::string get_str(std::string s)
{
std::string ret;
if (isvarok(s))
{
ret = var_value(s);
}
else if (s == ":")
ret = nx();
else if (isstring(s))
{
ret = cutstr(s);
}
else
{
ret = s;
}
return ret;
}
inline std::string aqua(std::string script, std::vector<std::string> line, int linenum)
{
// スペースごとに変換
std::vector<std::string> code = scriptcut(script);
if (code[code.size() - 1].back() == ';')
{
code[code.size() - 1] = code[code.size() - 1].substr(0, code[code.size() - 1].size() - 1);
}
std::string func = code[0];
// funcを含まない引数の数
ll argn = code.size() - 1;
// 実行時エラー(忌まわしきSEGMENTATION FAULT)防止
for (int i = 0; i < 10; i++)
{
code.push_back("");
}
// 謎のエラー防止
try
{
// インシデントチェック
if (inc_code == inc_now && !comment)
{
// 以下、関数記述
if (func == "out")
{
// 全ての始まり。改行なし出力
if (isstring(code[1]))
out(cutstr(code[1]));
else if (code[1] == ":")
out(nx());
else if (isvarok(code[1]))
{
out(var_value(code[1]));
}
else
err(2);
}
else if (func == "option")
{
// オプション。色々。
if (code[1] == "function_skip")
{
op_funcskip = stob(code[2]);
}
else if (code[1] == "reset_style")
{
op_stylereset = stob(code[2]);
}
else if (code[1] == "end_anykey")
{
op_end_anykey = stob(code[2]);
}
else if (code[1] == "set_few")
{
std::cout << std::fixed << std::setprecision(stoi(code[2]));
}
else if (code[1] == "over_var")
{
op_over_var = stob(code[2]);
}
else
{
err(3);
}
}
else if (func == "outf")
{
// 改行付き出力。fついてるけどflushしない
if (isstring(code[1]))
outf(cutstr(code[1]));
else if (code[1] == ":")
{
outf(nx());
}
else if (isvarok(code[1]))
{
outf(var_value(code[1]));
}
else
err(2);
}
else if (func == "#" || func == "comment")
{
// 何もない。何もしない。
}
else if (func == "exit")
{
// 終了コード指定可exit
if (code[1] == "")
exit(0);
else
exit((ll)to_num(code[1]));
}
else if (func == "throw")
{
// 例外を投げる
if (code[1] == "")
{
err(4);
}
else
{
err((ll)to_num(code[1]));
}
}
else if (func == "var")
{
// 変数宣言
bool changed = false; // 初期値を自動的に変更したか否か
bool isconst = false; // 定数か否か
if (code[3] == "")
{
changed = true;
code[3] = "0";
}
if (isvarok(code[3]))
{
code[3] = var_value(code[3]);
}
if (code[2][0] == '$')
{
isconst = true;
code[2] = code[2].substr(1);
const_var.insert(code[2]);
if (changed) // なんで動くのか不明
err(45);
}
if (isvardecok(code[2]))
{
if (dup_varname(code[2]) || op_over_var)
{
if (code[1] == "int")
{
var_int[code[2]] = stoi(code[3]);
}
else if (code[1] == "string")
{
if (changed)
var_string[code[2]] = "";
else
var_string[code[2]] = cutstr(code[3]);
}
else if (code[1] == "bool")
{
if (changed)
var_bool[code[2]] = true;
else
var_bool[code[2]] = stob(code[3]);
}
else if (code[1] == "double")
{
var_double[code[2]] = stod(code[3]);
}
else if (code[1] == "int64_t")
{
var_int64[code[2]] = stoll(code[3]);
}
else if (code[1] == "longlong" || code[1] == "ll")
{
var_ll[code[2]] = stoll(code[3]);
}
else if (code[1] == "uint")
{
if (stoll(code[3]) < 0)
{
err(42);
}
else
{
var_uint[code[2]] = stoul(code[3]);
}
}
else
{
err(6); // 存在しない型の時
}
}
else
{
err(9); // 変数が重複しているとき。optionで制御可能
}
}
else
{
err(8); // やべえ名前の時
}
}
else if (func == "ln")
{
// 改行。エスケープシーケンス対応したから多分もう使わない。
if (code[1] == "")
{
std::cout << "\n";
}
else
{
for (ll i = 0; i < (ll)to_num(code[1]); i++) // rep使えよ
std::cout << "\n";
}
}
else if (func == "style")
{
// \033のやつ。意味わからん
if (sett[0])
{
if (code[1] == "text")
{
if (code[2] == "black")
{
cou("\033[30m");
}
else if (code[2] == "red")
{
cou("\033[31m");
}
else if (code[2] == "green")
{
cou("\033[32m");
}
else if (code[2] == "yellow")
{
cou("\033[33m");
}
else if (code[2] == "blue")
{
cou("\033[34m");
}
else if (code[2] == "magenta")
{
cou("\033[35m");
}
else if (code[2] == "cyan")
{
cou("\033[36m");
}
else if (code[2] == "white")
{
cou("\033[37m");
}
else if (code[2] == "bold")
{
cou("\033[1m");
}
else if (code[2] == "thinly")
{
cou("\033[2m");
}
else if (code[2] == "italic")
{
cou("\033[3m");
}
else if (code[2] == "underline")