From 6b45dd8900c84531a5eba6de0837e0f84d619286 Mon Sep 17 00:00:00 2001 From: s-ol Date: Tue, 18 Sep 2018 06:01:05 +0200 Subject: tape --- app/html.client.moon | 5 ++- app/html.server.moon | 5 ++- app/realities.moon | 36 +++++++++------- duct_tape.moon | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ server.moon | 3 ++ 5 files changed, 144 insertions(+), 20 deletions(-) create mode 100644 duct_tape.moon diff --git a/app/html.client.moon b/app/html.client.moon index fdec714..77d6c51 100644 --- a/app/html.client.moon +++ b/app/html.client.moon @@ -28,8 +28,9 @@ element = (element) -> (...) -> elements = {} add = (e) -> elements[e] = element e -for e in *{'div', 'span', 'a', 'p', 'pre', 'button', 'ul', 'li', 'i', 'b', 'u', 'tt'} do add e -for e in *{'br', 'img', 'input', 'p'} do add e +for e in *{'div', 'form', 'span', 'a', 'p', 'button', 'ul', 'ol', 'li', 'i', 'b', 'u', 'tt'} do add e +for e in *{'article', 'section', 'header', 'footer', 'content'} do add e +for e in *{'br', 'hr', 'img', 'input', 'p', 'textarea'} do add e for i=1,8 do add "h" .. i elements diff --git a/app/html.server.moon b/app/html.server.moon index 6bd05e1..3a27b37 100644 --- a/app/html.server.moon +++ b/app/html.server.moon @@ -29,8 +29,9 @@ element = (element) -> (...) -> elements = {} add = (e) -> elements[e] = element e -for e in *{'div', 'span', 'a', 'p', 'pre', 'button', 'ul', 'li', 'i', 'b', 'u', 'tt'} do add e -for e in *{'br', 'img', 'input', 'p'} do add e +for e in *{'div', 'form', 'span', 'a', 'p', 'button', 'ul', 'ol', 'li', 'i', 'b', 'u', 'tt'} do add e +for e in *{'article', 'section', 'header', 'footer', 'content'} do add e +for e in *{'br', 'hr', 'img', 'input', 'p', 'textarea'} do add e for i=1,8 do add "h" .. i elements diff --git a/app/realities.moon b/app/realities.moon index b3c5020..244cd41 100644 --- a/app/realities.moon +++ b/app/realities.moon @@ -1,8 +1,11 @@ -import append, tohtml, h1, h2, p, a, i, div, ol, li, br, hr, span, button, section, article from require 'app.component' +import append, h1, h2, p, a, i, div, ol, li, br, hr, span, button, section, article from require 'app.component' local Diagram, o GRID_W = 50 GRID_H = 40 + +-- script https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.6/svg.min.js + if MODE == 'CLIENT' require 'svg.js' eval = js.global\eval @@ -189,32 +192,33 @@ append with article style: { margin: 'auto', 'max-width': '750px' } \append with sect "Terminology" \append p "We propose the following terms and definitions that will be used extensively for the remainder of the paper:" - for term, definition in pairs { - "layer of reality": "a closed system consisting of a world model and a set of rules or dynamics operating on and - constraining said model.", - "world model": "describes a world state containing objects, agents and/or concepts on an arbitrary abstraction level.", - break: 1, - "reality stack": "structure consisting of all layers of reality encoding an agent's interaction with his environment - in their world model at a given moment, as well as all layers supporting these respectively." - next: 2, - "physical reality": "layer of reality defined by physical matter and the physical laws acting upon it. + for definition in *{ + { "layer of reality": "a closed system consisting of a world model and a set of rules or dynamics operating on and + constraining said model." }, + { "world model": "describes a world state containing objects, agents and/or concepts on an arbitrary abstraction level." }, + '------', + { "reality stack": "structure consisting of all layers of reality encoding an agent's interaction with his environment + in their world model at a given moment, as well as all layers supporting these respectively." }, + '------', + { "physical reality": "layer of reality defined by physical matter and the physical laws acting upon it. While the emergent phenomena of micro- and macro physics as well as layers of social existence etc. may be seen - as separate layers, for the purpose of this paper we will group these together under the term of physical reality." - "mental reality": "layer of reality perceived and processed by the brain of a human agent.", - "digital reality": "layer of reality created and simulated by a digital system, e.g. a virtual reality game." - "phys, mind, digi": "abbreviations for physical, mental and digital reality respectively.", + as separate layers, for the purpose of this paper we will group these together under the term of physical reality." }, + { "mental reality": "layer of reality perceived and processed by the brain of a human agent." }, + { "digital reality": "layer of reality created and simulated by a digital system, e.g. a virtual reality game." }, + { "phys, mind, digi": "abbreviations for physical, mental and digital reality respectively." }, } - if 'number' == type definition + if 'string' == type definition \append hr! continue \append with div style: { 'margin-left': '2rem' } + term = next definition \append span term, style: { display: 'inline-block', 'margin-left': '-2rem', 'font-weight': 'bold', 'min-width': '140px' } - \append span definition + \append span definition[term] \append with sect "Introduction" \append p "We identify two different types of relationships between layers in multi-reality environments. diff --git a/duct_tape.moon b/duct_tape.moon new file mode 100644 index 0000000..f6e2a84 --- /dev/null +++ b/duct_tape.moon @@ -0,0 +1,115 @@ +package.noompath = './?.noom;./?/init.noom' + +local * + +compile = require "moonscript.compile" +parse = require "moonscript.parse" + +import smart_node, build from require "moonscript.types" +import dirsep from require "moonscript.base" +-- import split, get_options, unpack from require "moonscript.util" +import get_options, unpack from require "moonscript.util" + +line_tables = require "moonscript.line_tables" +lua = :loadstring, :load + +import p from require 'moon' + +matches = (chain) -> + return unless chain[1] == "chain" + ref, call = chain[2], chain[3] + return unless ref[1] == "ref" and call[1] == "call" + ref[2] == "COMPILE" + +transform_extract = (node) -> + { _, ref, call } = node + { _, call_args } = call + fndef = smart_node call_args[1] + + if #fndef.whitelist > 1 + return 'cannot COMPILE function with whitelist!' + + code, ltable, pos = compile.tree { fndef } + if not code + return compile.format_error(ltable, pos, text) + + node[1] = "string" + node[2] = "'" + node[3] = code + node[-1] = nil + nil + +transform_extracts = (node) -> + if matches node + transform_extract node + else + err = nil + for v in *node + err or= transform_extracts v if 'table' == type v + err + +to_lua = (text, options={}) -> + if "string" != type text + t = type text + return nil, "expecting string (got ".. t ..")" + + tree, err = parse.string text + if not tree + return nil, err + + err = transform_extracts tree + if err + return nil, err + + code, ltable, pos = compile.tree tree, options + if not code + return nil, compile.format_error(ltable, pos, text) + + code, ltable + +noom_loader = (name) -> + name_path = name\gsub "%.", dirsep + + local file, file_path + for path in package.noompath\gmatch "[^;]+" + file_path = path\gsub "?", name_path + file = io.open file_path + break if file + + if file + text = file\read "*a" + file\close! + res, err = loadstring text, "@#{file_path}" + if not res + error file_path .. ": " .. err + + return res + + return nil, "Could not find noom file" + +loadstring = (...) -> + options, str, chunk_name, mode, env = get_options ... + chunk_name or= "=(noom.loadstring)" + + code, ltable_or_err = to_lua str, options + unless code + return nil, ltable_or_err + + line_tables[chunk_name] = ltable_or_err if chunk_name + -- the unpack prevents us from passing nil + (lua.loadstring or lua.load) code, chunk_name, unpack { mode, env } + +insert_loader = (pos=2) -> + -- if not package.moonpath + -- package.moonpath = create_moonpath package.path + + loaders = package.loaders or package.searchers + for loader in *loaders + return false if loader == noom_loader + + table.insert loaders, pos, noom_loader + true + +{ + :insert_loader +} diff --git a/server.moon b/server.moon index 5a79039..0253faf 100644 --- a/server.moon +++ b/server.moon @@ -1,5 +1,8 @@ package.moonpath = './?.server.moon;' .. package.moonpath +import insert_loader from require 'duct_tape' +insert_loader! + export MODE, warn, append MODE = 'SERVER' warn = (...) -> -- cgit v1.2.3 From 2319b4d0729bcb5643c3cf8e645b15fedec11b2e Mon Sep 17 00:00:00 2001 From: s-ol Date: Tue, 18 Sep 2018 07:44:26 +0200 Subject: tape test --- duct_tape.moon | 92 +++++++++++++++++++++++++++++++++++----------------------- server.moon | 6 ++-- 2 files changed, 59 insertions(+), 39 deletions(-) diff --git a/duct_tape.moon b/duct_tape.moon index f6e2a84..35b6b5b 100644 --- a/duct_tape.moon +++ b/duct_tape.moon @@ -1,46 +1,60 @@ -package.noompath = './?.noom;./?/init.noom' +-- package.noompath = './?.noom;./?/init.noom' +package.noompath = './?.moon;./?/init.moon' -local * +export __FNDEF_NOOM_CACHE +__FNDEF_NOOM_CACHE = {} + +assert MODE == 'SERVER', "duct_tape only works on the server" -compile = require "moonscript.compile" -parse = require "moonscript.parse" +compile = require 'moonscript.compile' +parse = require 'moonscript.parse' -import smart_node, build from require "moonscript.types" -import dirsep from require "moonscript.base" --- import split, get_options, unpack from require "moonscript.util" -import get_options, unpack from require "moonscript.util" +import smart_node, build from require 'moonscript.types' +import dirsep from require 'moonscript.base' +import get_options, unpack from require 'moonscript.util' -line_tables = require "moonscript.line_tables" +line_tables = require 'moonscript.line_tables' lua = :loadstring, :load import p from require 'moon' -matches = (chain) -> - return unless chain[1] == "chain" - ref, call = chain[2], chain[3] - return unless ref[1] == "ref" and call[1] == "call" - ref[2] == "COMPILE" +local * +clone = (a) -> { k,v for k,v in pairs a } transform_extract = (node) -> - { _, ref, call } = node - { _, call_args } = call - fndef = smart_node call_args[1] + fndef = clone node - if #fndef.whitelist > 1 - return 'cannot COMPILE function with whitelist!' + -- if #fndef.whitelist > 1 + -- return "cannot COMPILE function with whitelist!" - code, ltable, pos = compile.tree { fndef } + -- compile the function to Lua separately + code, ltable, pos = compile.tree { clone fndef } if not code return compile.format_error(ltable, pos, text) - node[1] = "string" - node[2] = "'" - node[3] = code - node[-1] = nil + -- mutate the node in-place + node[1] = 'do' + for i=2,5 + node[i] = nil + + -- assign the lua code to __FNDEF_NOOM_CACHE[function] so it can be retrieved by `compile` + fn = { 'ref', 'fn' } + reference = build.chain { + base: '__FNDEF_NOOM_CACHE', + { 'index', fn } + } + + node[2] = { + { 'declare_with_shadows', { 'fn' } } + build.assign_one fn, fndef + build.assign_one reference, { 'string', '[[', code } + fn + } + nil transform_extracts = (node) -> - if matches node + if node[1] == "fndef" transform_extract node else err = nil @@ -99,17 +113,23 @@ loadstring = (...) -> -- the unpack prevents us from passing nil (lua.loadstring or lua.load) code, chunk_name, unpack { mode, env } -insert_loader = (pos=2) -> - -- if not package.moonpath - -- package.moonpath = create_moonpath package.path +do + local compile + insert_loader = (pos=2) -> + -- if not package.moonpath + -- package.moonpath = create_moonpath package.path + + loaders = package.loaders or package.searchers + for loader in *loaders + return false if loader == noom_loader - loaders = package.loaders or package.searchers - for loader in *loaders - return false if loader == noom_loader + table.insert loaders, pos, noom_loader + true - table.insert loaders, pos, noom_loader - true + compile = (fn) -> with code = __FNDEF_NOOM_CACHE[fn] + assert code, 'cannot compile function not loaded from noomscript source.' -{ - :insert_loader -} + { + :insert_loader + :compile + } diff --git a/server.moon b/server.moon index 0253faf..283672e 100644 --- a/server.moon +++ b/server.moon @@ -1,8 +1,5 @@ package.moonpath = './?.server.moon;' .. package.moonpath -import insert_loader from require 'duct_tape' -insert_loader! - export MODE, warn, append MODE = 'SERVER' warn = (...) -> @@ -17,6 +14,9 @@ flush = -> with x = buffer buffer = '' +import insert_loader from require 'duct_tape' +insert_loader! + error "please specify the module to build as an argumnet" unless arg[1] require "app.#{arg[1]}" -- cgit v1.2.3 From ac7e873595c93a996090632cfbdf2f47d5a4e2c4 Mon Sep 17 00:00:00 2001 From: s-ol Date: Mon, 15 Oct 2018 16:15:34 +1100 Subject: more tape --- app/realities.moon | 16 ++++++++++++++++ app/test.moon | 4 ++++ duct_tape.moon | 8 ++++++++ 3 files changed, 28 insertions(+) create mode 100644 app/test.moon diff --git a/app/realities.moon b/app/realities.moon index 244cd41..d83109c 100644 --- a/app/realities.moon +++ b/app/realities.moon @@ -1,11 +1,27 @@ import append, h1, h2, p, a, i, div, ol, li, br, hr, span, button, section, article from require 'app.component' +import compile from require 'duct_tape' local Diagram, o GRID_W = 50 GRID_H = 40 +on_client = (fn, ...) -> + switch MODE + when 'SERVER' + code = compile fn + warn code + append "" + when 'CLIENT' + fn ... + -- script https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.6/svg.min.js +on_client -> + js.global\alert 'hello world!' + if MODE == 'CLIENT' require 'svg.js' eval = js.global\eval diff --git a/app/test.moon b/app/test.moon new file mode 100644 index 0000000..799ec03 --- /dev/null +++ b/app/test.moon @@ -0,0 +1,4 @@ +y = { :print } +-> + test with y + print diff --git a/duct_tape.moon b/duct_tape.moon index 35b6b5b..d1efd0c 100644 --- a/duct_tape.moon +++ b/duct_tape.moon @@ -71,10 +71,14 @@ to_lua = (text, options={}) -> if not tree return nil, err + p tree + err = transform_extracts tree if err return nil, err + p tree + code, ltable, pos = compile.tree tree, options if not code return nil, compile.format_error(ltable, pos, text) @@ -110,6 +114,10 @@ loadstring = (...) -> return nil, ltable_or_err line_tables[chunk_name] = ltable_or_err if chunk_name + + print "#{chunk_name} compiled to:" + print code + -- the unpack prevents us from passing nil (lua.loadstring or lua.load) code, chunk_name, unpack { mode, env } -- cgit v1.2.3 From 1465ad3b786caf26cdff2ab02b953d3c73a71f17 Mon Sep 17 00:00:00 2001 From: s-ol Date: Thu, 18 Oct 2018 19:05:22 +1100 Subject: fix server/duct_tape --- app/test.moon | 4 ---- duct_tape.moon | 58 ++++++++++++++++++++++++++-------------------------------- server.moon | 1 + 3 files changed, 27 insertions(+), 36 deletions(-) delete mode 100644 app/test.moon diff --git a/app/test.moon b/app/test.moon deleted file mode 100644 index 799ec03..0000000 --- a/app/test.moon +++ /dev/null @@ -1,4 +0,0 @@ -y = { :print } --> - test with y - print diff --git a/duct_tape.moon b/duct_tape.moon index d1efd0c..0e62381 100644 --- a/duct_tape.moon +++ b/duct_tape.moon @@ -20,47 +20,47 @@ import p from require 'moon' local * -clone = (a) -> { k,v for k,v in pairs a } -transform_extract = (node) -> - fndef = clone node +escape_str = (str) -> + (str\gsub('\\', '\\\\')\gsub '"', '\\"'), nil - -- if #fndef.whitelist > 1 - -- return "cannot COMPILE function with whitelist!" +deep_clone = (value) -> + if 'table' == type value + { deep_clone(k), deep_clone(v) for k,v in pairs value } + else -- number, string, boolean, etc + value +transform_fndef = (fndef, parent, i) -> -- compile the function to Lua separately - code, ltable, pos = compile.tree { clone fndef } + code, ltable, pos = compile.tree { deep_clone fndef } if not code return compile.format_error(ltable, pos, text) - -- mutate the node in-place - node[1] = 'do' - for i=2,5 - node[i] = nil + code = "(function() #{code} end)()" -- assign the lua code to __FNDEF_NOOM_CACHE[function] so it can be retrieved by `compile` - fn = { 'ref', 'fn' } - reference = build.chain { + fn = -> { 'ref', 'fn' } + cache = build.chain { base: '__FNDEF_NOOM_CACHE', - { 'index', fn } + { 'index', fn! } } - node[2] = { + parent[i] = build.do { { 'declare_with_shadows', { 'fn' } } - build.assign_one fn, fndef - build.assign_one reference, { 'string', '[[', code } - fn + build.assign_one fn!, fndef + build.assign_one cache, { 'string', '"', escape_str code } + -- build.assign_one cache, { 'string', '[[', code } + fn! } nil -transform_extracts = (node) -> +find_fndefs = (node, p, i) -> if node[1] == "fndef" - transform_extract node + transform_fndef node, p, i else - err = nil - for v in *node - err or= transform_extracts v if 'table' == type v - err + for i = 1,#node + v = node[i] + find_fndefs v, node, i if 'table' == type v to_lua = (text, options={}) -> if "string" != type text @@ -71,13 +71,7 @@ to_lua = (text, options={}) -> if not tree return nil, err - p tree - - err = transform_extracts tree - if err - return nil, err - - p tree + find_fndefs tree code, ltable, pos = compile.tree tree, options if not code @@ -115,8 +109,8 @@ loadstring = (...) -> line_tables[chunk_name] = ltable_or_err if chunk_name - print "#{chunk_name} compiled to:" - print code + warn "#{chunk_name} compiled to:" + warn code -- the unpack prevents us from passing nil (lua.loadstring or lua.load) code, chunk_name, unpack { mode, env } diff --git a/server.moon b/server.moon index 283672e..fb33806 100644 --- a/server.moon +++ b/server.moon @@ -24,6 +24,7 @@ require "app.#{arg[1]}" print " + MMM: lunar low-gravity scripting playground -- cgit v1.2.3 From 7f24616e36eb012e968112cdb104fa5d3f1d1934 Mon Sep 17 00:00:00 2001 From: s-ol Date: Thu, 18 Oct 2018 19:07:26 +1100 Subject: fix realities --- app/realities.moon | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/app/realities.moon b/app/realities.moon index d83109c..d4713a0 100644 --- a/app/realities.moon +++ b/app/realities.moon @@ -1,27 +1,24 @@ import append, h1, h2, p, a, i, div, ol, li, br, hr, span, button, section, article from require 'app.component' -import compile from require 'duct_tape' local Diagram, o GRID_W = 50 GRID_H = 40 -on_client = (fn, ...) -> - switch MODE - when 'SERVER' +on_client = switch MODE + when 'SERVER' + import compile from require 'duct_tape' + (fn, ...) -> code = compile fn - warn code + -- warn code append "" - when 'CLIENT' - fn ... + when 'CLIENT' + (fn, ...) -> fn ... -- script https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.6/svg.min.js -on_client -> - js.global\alert 'hello world!' - if MODE == 'CLIENT' require 'svg.js' eval = js.global\eval @@ -124,7 +121,6 @@ figures = do 'justify-content': 'space-evenly' (...) -> div { :style, ... } - sources = do short = (id) => "#{id} #{@year}" long = => @names, " (#{@year}): ", (i @title), ", #{@published}" -- cgit v1.2.3