complete rewrite of gdt.lm using mini expr parser - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
path: root/expr-print.lua
diff options
context:
space:
mode:
authorFrancesco Abbate <francesco.bbt@gmail.com>2013年01月20日 19:21:09 +0100
committerFrancesco Abbate <francesco.bbt@gmail.com>2013年01月20日 19:21:09 +0100
commit623b7ac2a0b18c5df2ed89695df8e2849d681fdd (patch)
tree1025c8eef45ebde898c65f3841c76fec212380df /expr-print.lua
parent0a746cfd9cb89eb5ff6c6576e96e3c291c2ee6b1 (diff)
downloadgsl-shell-623b7ac2a0b18c5df2ed89695df8e2849d681fdd.tar.gz
complete rewrite of gdt.lm using mini expr parser
Diffstat (limited to 'expr-print.lua')
-rw-r--r--expr-print.lua 52
1 files changed, 52 insertions, 0 deletions
diff --git a/expr-print.lua b/expr-print.lua
new file mode 100644
index 00000000..27e8f75c
--- /dev/null
+++ b/expr-print.lua
@@ -0,0 +1,52 @@
+local format, concat = string.format, table.concat
+
+local oper_table = {['+'] = 0, ['-'] = 0, ['*'] = 1, ['/'] = 1, ['^'] = 2}
+
+local ex_print
+
+local function is_ident_simple(s)
+ return s:match('^[%l%u_][%w_]*$')
+end
+
+local function op_print(e, prio)
+ if #e == 1 then
+ local c, c_prio = ex_print(e[1])
+ if c_prio < prio then c = format('(%s)', c) end
+ return format("%s%s", e.operator, c)
+ else
+ local a, a_prio = ex_print(e[1])
+ local b, b_prio = ex_print(e[2])
+ if a_prio < prio then a = format('(%s)', a) end
+ if b_prio < prio then b = format('(%s)', b) end
+ local temp = (prio < 2 and "%s %s %s" or "%s%s%s")
+ return format(temp, a, e.operator, b)
+ end
+end
+
+local function exlist_print(e)
+ local t = {}
+ for k = 1, #e do t[k] = ex_print(e[k]) end
+ return concat(t, ', ')
+end
+
+ex_print = function(e)
+ if type(e) == 'number' then
+ return e, 3
+ elseif type(e) == 'string' then
+ local s = e
+ if not is_ident_simple(s) then s = format('[%s]', s) end
+ return s, 3
+ else
+ local prio = oper_table[e.operator]
+ local s = op_print(e, prio)
+ return s, prio
+ end
+end
+
+local function schema_print(e)
+ local ys = exlist_print(e.y)
+ local xs = exlist_print(e.x)
+ return format("%s ~ %s", ys, xs)
+end
+
+return {schema = schema_print, expr = ex_print, expr_list = exlist_print}
generated by cgit v1.2.3 (git 2.39.1) at 2025年09月12日 19:35:07 +0000

AltStyle によって変換されたページ (->オリジナル) /