Skip to content

Commit

Permalink
ADD: Add palindrome
Browse files Browse the repository at this point in the history
- `task util`: sum, product, reverse, palindrome
- `sys.func`: function, swap, apply, applicative
  • Loading branch information
bajdcc committed May 9, 2017
1 parent 97f5b10 commit 0c27ea6
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/priv/bajdcc/LALR1/grammar/runtime/RuntimeMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public RuntimeMachine() throws Exception {
ModuleBase.getInstance(),
ModuleMath.getInstance(),
ModuleList.getInstance(),
ModuleFunction.getInstance(),
ModuleString.getInstance(),
ModuleProc.getInstance(),
ModuleUI.getInstance(),
ModuleTask.getInstance(),
ModuleRemote.getInstance(),
ModuleNet.getInstance(),
ModuleFunction.getInstance(),
};
}

Expand Down
22 changes: 0 additions & 22 deletions src/priv/bajdcc/LALR1/interpret/module/ModuleBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,6 @@ public String getModuleName() {
public RuntimeCodePage getCodePage() throws Exception {
String base = "var g_author = func [\"标准库的作者:bajdcc\"] ~() -> \"bajdcc\";\n"
+ "export \"g_author\";\n"
+ "var g_max = func ~(a, b) -> a > b ? a : b;\n"
+ "export \"g_max\";\n"
+ "var g_min = func ~(a, b) -> a < b ? a : b;\n"
+ "export \"g_min\";\n"
+ "var g_lt = func ~(a, b) -> a < b;\n"
+ "export \"g_lt\";\n"
+ "var g_gt = func ~(a, b) -> a > b;\n"
+ "export \"g_gt\";\n"
+ "var g_eq = func ~(a, b) -> a == b;\n"
+ "export \"g_eq\";\n"
+ "var g_mod = func ~(a, b) -> a % b == 0;\n"
+ "export \"g_mod\";\n"
+ "var g_curry_1 = func ~(a, b) {\n"
+ " var curry = func ~(c) -> call a(b, c);\n"
+ " return curry;"
+ "};\n"
+ "export \"g_curry_1\";\n"
+ "var g_swap = func ~(a) {\n"
+ " var swap = func ~(b, c) -> call a(c, b);\n"
+ " return swap;"
+ "};\n"
+ "export \"g_swap\";\n"
+ "var g_println = func ~() {\n"
+ " call g_print(g_endl);\n"
+ "};\n"
Expand Down
36 changes: 24 additions & 12 deletions src/priv/bajdcc/LALR1/interpret/module/ModuleFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,45 @@ public RuntimeCodePage getCodePage() throws Exception {
"export \"g_func_sub\";\n" +
"export \"g_func_mul\";\n" +
"export \"g_func_div\";\n" +
"var curry = func ~(a, b) {\n" +
" var f = func ~(c) -> call a(b, c);\n" +
" return f;\n" +
"var g_func_curry = func ~(a, b) {\n" +
" var _curry = func ~(c) -> call a(b, c);\n" +
" return _curry;\n" +
"};\n" +
"var swap = func ~(a) {\n" +
" var f = func ~(b, c) -> call a(c, b);\n" +
" return f;\n" +
"var g_func_swap = func ~(a) {\n" +
" var _swap = func ~(b, c) -> call a(c, b);\n" +
" return _swap;\n" +
"};\n" +
"export \"g_func_curry\";\n" +
"export \"g_func_swap\";\n" +
"var g_func_1 = func ~(a) -> a;\n" +
"export \"g_func_1\";\n" +
"\n" +
"var xs = func [\"数组遍历闭包\"] ~(l) {\n" +
" var len = call g_array_size(l);\n" +
" var idx = 0;\n" +
" var f = func ~() {\n" +
" var _xs = func ~() {\n" +
" if (idx == len) { return g__; }\n" +
" var d = call g_array_get(l, idx);\n" +
" idx++;\n" +
" var f2 = func ~() -> d;\n" +
" return f2;\n" +
" var _xs_ = func ~() -> d;\n" +
" return _xs_;\n" +
" };\n" +
" return f;\n" +
" return _xs;\n" +
"};\n" +
"\n" +
"var g_func_apply = func ~(name, list) {\n" +
" return call g_func_apply_arg(name, list, \"g_func_1\");\n" +
"};\n" +
"export \"g_func_apply\";\n" +
"var g_func_apply_arg = func ~(name, list, arg) {\n" +
" var len = call g_array_size(list);\n" +
" if (len == 0) { return g__; }\n" +
" if (len == 1) { return call g_array_get(list, 0); }\n" +
" var x = call xs(list);\n" +
" var val = call x();\n" +
" let val = call val();\n" +
" var n = \"g_func_\" + name;\n" +
" var n = name;\n" +
" let n = call arg(n);\n" +
" for (;;) {\n" +
" var v2 = call x();\n" +
" if (call g_is_null(v2)) { break; }\n" +
Expand All @@ -89,7 +98,10 @@ public RuntimeCodePage getCodePage() throws Exception {
" }\n" +
" return val;\n" +
"};\n" +
"export \"g_func_apply\";\n";
"export \"g_func_apply_arg\";\n" +
"var g_func_applicative = func ~(f, a, b) -> call f(a, call b(a));\n" +
"export \"g_func_applicative\";\n" +
"\n";

Grammar grammar = new Grammar(base);
RuntimeCodePage page = grammar.getCodePage();
Expand Down
6 changes: 5 additions & 1 deletion src/priv/bajdcc/LALR1/interpret/module/ModuleString.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ public String getModuleName() {
@Override
public RuntimeCodePage getCodePage() throws Exception {
String base = "import \"sys.base\";\n" +
"import \"sys.func\";\n" +
"var g_range_string = yield ~(a) {\n" +
" var size = call g_string_length(a);\n" +
" for (var i = 0; i < size; i++) {\n" +
" yield call g_string_get(a, i);\n" +
" }\n" +
"};\n" +
"export \"g_range_string\";\n";
"export \"g_range_string\";\n" +
"var g_string_reverse = func ~(str) -> call g_func_apply_arg(\"g_func_add\", call g_string_split(str, \"\"), \"g_func_swap\");\n" +
"export \"g_string_reverse\";\n" +
"\n";

Grammar grammar = new Grammar(base);
RuntimeCodePage page = grammar.getCodePage();
Expand Down
25 changes: 23 additions & 2 deletions src/priv/bajdcc/LALR1/interpret/os/task/TKUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public String getCode() {
" call g_array_add(arr, num);\n" +
" }\n" +
" }\n" +
" var val = call g_func_apply(\"add\", arr);\n" +
" var val = call g_func_apply(\"g_func_add\", arr);\n" +
" call g_map_put(msg, \"val\", val);\n" +
" } else if (id == \"product\") {\n" +
" var arg = call g_map_get(msg, \"arg\");\n" +
Expand All @@ -68,7 +68,28 @@ public String getCode() {
" call g_array_add(arr, num);\n" +
" }\n" +
" }\n" +
" var val = call g_func_apply(\"mul\", arr);\n" +
" var val = call g_func_apply(\"g_func_mul\", arr);\n" +
" call g_map_put(msg, \"val\", val);\n" +
" } else if (id == \"reverse\") {\n" +
" var arg = call g_map_get(msg, \"arg\");\n" +
" var arr = [];\n" +
" var len = call g_array_size(arg);\n" +
" foreach (var i : call g_range(2, len - 1)) {\n" +
" call g_array_add(arr, call g_array_get(arg, i));\n" +
" }\n" +
" var str = call g_string_join_array(arr, \" \");\n" +
" var val = call g_string_reverse(str);\n" +
" call g_map_put(msg, \"val\", val);\n" +
" } else if (id == \"palindrome\") {\n" +
" var arg = call g_map_get(msg, \"arg\");\n" +
" var arr = [];\n" +
" var len = call g_array_size(arg);\n" +
" foreach (var i : call g_range(2, len - 1)) {\n" +
" call g_array_add(arr, call g_array_get(arg, i));\n" +
" }\n" +
" var str = call g_string_join_array(arr, \" \");\n" +
" var reverse = func ~(str) -> call g_string_reverse(str);\n" +
" var val = call g_func_applicative(\"g_func_eq\", str, reverse);\n" +
" call g_map_put(msg, \"val\", val);\n" +
" }\n" +
"};\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public String getCode() {
"\n" +
"var error = call g_map_get(msg, \"error\");\n" +
"var val = call g_map_get(msg, \"val\");\n" +
"let val = call g_to_string(val);\n" +
"if (error == 1) {\n" +
" call g_write_pipe(out, \"Error: \" + val);\n" +
" call g_write_pipe(out, '\\n');\n" +
Expand Down
10 changes: 8 additions & 2 deletions src/priv/bajdcc/LALR1/interpret/test/TestInterpret11.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ public static void main(String[] args) {
"import \"sys.base\";\n" +
"import \"sys.func\";\n" +
"import \"sys.list\";\n" +
"import \"sys.string\";\n" +
"\n" +
"var a = call g_array_range(1, 10);\n" +
"var b = call g_func_apply(\"add\", a);\n" +
"var b = call g_func_apply(\"g_func_add\", a);\n" +
"call g_printn(b);\n" +
"var c = call g_func_apply(\"mul\", a);\n" +
"var c = call g_func_apply(\"g_func_add\", a);\n" +
"call g_printn(c);\n" +
"var d = call g_func_apply_arg(\"g_func_add\", call g_string_split(\"12345\", \"\"), \"g_func_swap\");\n" +
"call g_printn(d);\n" +
"var reverse = func ~(str) -> call g_string_reverse(str);\n" +
"var e = call g_func_applicative(\"g_func_eq\", \"12321\", reverse);\n" +
"call g_printn(e);\n" +
""
};

Expand Down

0 comments on commit 0c27ea6

Please sign in to comment.