From: Robert D. <rob...@gm...> - 2022-08-06 16:48:23
|
Hi Eduardo, this is all very interesting. Some comments that might be helpful. About GraphViz, on searching the web for pointers, it looks like it doesn't have an output format for ASCII art, although it does cover many other formats. There are a few other programs that output ASCII art, you could take a look. Here are some links I found: https://metacpan.org/pod/Graph::Easy https://github.com/ggerganov/dot-to-ascii https://stackoverflow.com/questions/6886426/tool-to-create-ascii-graph-from-a-set-of-vertices-and-edges https://graphviz.org/ But maybe that's all not so important since you have a working program in Lua. I guess the Maxima program is to convert a Maxima expression to a Lua associative table. When I have to do stuff like that, I always try to work as much as possible with expressions, and avoid string operations if at all possible. In that spirit, here's my attempt. (%i1) lua_table_from(e) := if atom(e) then string(e) else funmake (lua_table, cons ([0] = op(e), map (lua_table_from, args(e)))); (%i2) lua_table_from (x/y + (z - 4)/3); (%o2) lua_table([0] = +, lua_table([0] = /, lua_table([0] = +, z, -4), 3), lua_table([0] = /, x, y)) That has the right structure, but it needs curly braces. Now curly braces already indicate a set, for which simplification can reorder the elements, so protect the setified expression from simplification while we print it. (%i3) block ([simp:false], grind (subst (lua_table = set, %o2))); {[0] = "+",{[0] = "/",{[0] = "+","z","-4"},"3"},{[0] = "/","x","y"}}$ grind outputs a trailing dollar sign, which is a bother here ... How about this instead: (%i8) block ([simp: false, display2d: false], print (subst (lua_table=set, %o2))); {[0] = "+",{[0] = "/",{[0] = "+","z","-4"},"3"},{[0] = "/","x","y"}} You can capture the output of print (or grind) via with_stdout, which see. Now a further simplification-related issue is that stuff like 1 + 1 gets simplified to 2, so you need to protect the input as well as the output. The simplest way to do that is to put the protected expression into a lambda. E.g. lambda([], 1 + 1) doesn't simplify 1 + 1. So here's an end to end solution: print_lua_table (e) := block ([simp: false, display2d: false], print (subst (lua_table = set, lua_table_from (e ())))); Note that e() returns whatever's in the lambda since e is a lambda expression. Some examples. (%i13) print_lua_table (lambda ([], 2/3 + 3*5 - 7)); {[0] = "+",{[0] = "/","2","3"},{[0] = "*","3","5"},{[0] = "-","7"}} (%i15) print_lua_table (lambda ([], 2*3 + 4/5 + 6)); {[0] = "+",{[0] = "*","2","3"},{[0] = "/","4","5"},"6"} (%i16) print_lua_table (lambda ([], foo*bar + baz/quux + 1)); {[0] = "+",{[0] = "*","foo","bar"},{[0] = "/","baz","quux"},"1"} I've tried to separate the different aspects: just generate something with the right structure, think about how to get that into curly braces, figure out how to protect the input and output from simplification. Hope this helps, it's a lot of fun. Robert |