From df83d221745b563eb7631e8e32a9df871da97a11 Mon Sep 17 00:00:00 2001 From: olpc user Date: Sun, 5 Jan 2020 15:27:52 -0800 Subject: working on showing own steps, bug so far --- starts/meaning-vm/level2.cpp | 372 ++++++++++++++++++++++++------------------- 1 file changed, 205 insertions(+), 167 deletions(-) diff --git a/starts/meaning-vm/level2.cpp b/starts/meaning-vm/level2.cpp index ae779f2..7c80ea1 100644 --- a/starts/meaning-vm/level2.cpp +++ b/starts/meaning-vm/level2.cpp @@ -424,174 +424,192 @@ void parse(ref stream) string lookupstr; ss >> lookupstr; ref lookup = lookupstr; - string cmd; - ss >> cmd; - if (cmd == "/*") { - // could parse comments into file info - } else if (cmd == "when") { - ref args = makeconcept(); - string name; - ss >> name; - std::map labels; - std::set values; - values.insert("context"); - values.insert("self"); - while (true) { - string arg; - ss >> arg; - if (arg == "[") { break; } - args.link("information-order", arg); - values.insert(arg); - } - ref result = ref("set-steps")(name, args); - ref laststep = result; - labels["return"] = nothing; - // when dump group [ - // = is-in-set in-set group - // ? is-in-set if true return. - // period-at-end: goto. - // comma-or-colon-at-end: label - // output-name group - // output-name ":" // quotes mean always-global - // ] - // - // proposing expression-based now. - // haven't resolved inherited name-contexts with literal strings fully. - // we'll need a function that turns a symbol into a ref, and takes - // an inherited context. - // we'll also change write-name to output-text, and get the name attribute - // what opens an inherited context? when are symbols added to it? - // atm we have a list of steps has 1 context. - // we also have labels to refer to. - // put labels in the context, treat them as normal names. - // that sounds nice, for vm to be able to pass step references to functions - // would just be a literal, though, a constant - // or we could not do subblocks, expression as steps - // what if we were to write this using the steps, with a local context - // we would have to track labels, and put them in the surrounding local context. maybe also a local condition. - // let's make a context object, link labels and surrounding condition to it. - // working on conditions. - // propose if tracks last step - // when if ends, adds last step to condition's set of last steps - // then next step after condition can wire to all steps in the set. - // can use 1-element set for normal steps. - // change step-creation to not automatically wire. - // and reconsider condition-step to not use its next-step attribute. - // instead its conditions decide what the next step is. - // looks good for conditions. fix names and update whole thing. - // inside a [], each step enters a set, to be wired to the next step inbetween. - // for jump-labels, we'll need to pass a reference to names of them to the - // function that builds the [] step list. - // this reference to names is basically a name-context. a lookup map for names. name-context is a concept related to the name link that inherits via outer-context links. - // it shows what to get for the name link - // to move towards name-contexts, let's at least call it name-context. - // maybe make a function to do the lookup. - // label-name-context. - // it's roughly okay to branch anywhere within the funtion, so it doesn't ned to actually inherit. - while (true) { - string label, action, result; - ss >> action; - if (action == "]") { break; } - if (action[action.size()-1] == ':' || action[action.size()-1] == ',') { - label = action; - label.resize(label.size() - 1); - if (label == "return") { throw makeconcept().link(is, "return-label-used"); } - ss >> action; + while (true) { + string cmd; + ss >> cmd; + if (!ss) { break; } + if (cmd == "/*") { + // could parse comments into file info + } else if (cmd == "information") { + ref args = makeconcept(); + string name; + ss >> name; + string linerest; + std::getline(ss, linerest); + stringstream ss2(linerest); + while (true) { + string arg; + ss2 >> arg; + if (!ss2) { break; } + args.link("information-order", arg); } - if (action == "=" || action == "set") { - ss >> result; - ss >> action; - values.insert(result); + ref("set-steps")(name, args); + } else if (cmd == "when") { + string name; + ss >> name; + string tok; + ss >> tok; + if (tok != "[") { throw makeconcept().link(is, "missing-[-after-when"); } + std::map labels; + std::set values; + values.insert("context"); + values.insert("self"); + ref order = makehabitinformationorder(name); + for (ref arg: order.getAll("information-order")) { + values.insert(arg.name()); } - if (action[action.size()-1] == '.') { - // is goto - action.resize(action.size() - 1); - if (!labels.count(action)) { - labels.emplace(action, makeconcept()); - } - labels[action].link("label", action); - if (laststep.linked("next-step")) { throw makeconcept().link(is, "jump-from-nowhere", "label", action); } - laststep.link("next-step", labels[action]); - laststep = nothing; - continue; - } - if (action == "if") { - ref cond = lookup(parsevalue(stream)); + conceptunmake(order); + // need to seed values with argument names + ref laststep = name; + labels["return"] = nothing; + // when dump group [ + // = is-in-set in-set group + // ? is-in-set if true return. + // period-at-end: goto. + // comma-or-colon-at-end: label + // output-name group + // output-name ":" // quotes mean always-global + // ] + // + // proposing expression-based now. + // haven't resolved inherited name-contexts with literal strings fully. + // we'll need a function that turns a symbol into a ref, and takes + // an inherited context. + // we'll also change write-name to output-text, and get the name attribute + // what opens an inherited context? when are symbols added to it? + // atm we have a list of steps has 1 context. + // we also have labels to refer to. + // put labels in the context, treat them as normal names. + // that sounds nice, for vm to be able to pass step references to functions + // would just be a literal, though, a constant + // or we could not do subblocks, expression as steps + // what if we were to write this using the steps, with a local context + // we would have to track labels, and put them in the surrounding local context. maybe also a local condition. + // let's make a context object, link labels and surrounding condition to it. + // working on conditions. + // propose if tracks last step + // when if ends, adds last step to condition's set of last steps + // then next step after condition can wire to all steps in the set. + // can use 1-element set for normal steps. + // change step-creation to not automatically wire. + // and reconsider condition-step to not use its next-step attribute. + // instead its conditions decide what the next step is. + // looks good for conditions. fix names and update whole thing. + // inside a [], each step enters a set, to be wired to the next step inbetween. + // for jump-labels, we'll need to pass a reference to names of them to the + // function that builds the [] step list. + // this reference to names is basically a name-context. a lookup map for names. name-context is a concept related to the name link that inherits via outer-context links. + // it shows what to get for the name link + // to move towards name-contexts, let's at least call it name-context. + // maybe make a function to do the lookup. + // label-name-context. + // it's roughly okay to branch anywhere within the funtion, so it doesn't ned to actually inherit. + while (true) { + string label, action, result; ss >> action; - if (action[action.size()-1] != '.') { - throw makeconcept().link(is, "condition-is-not-label", "action", action, "cond", cond); - } - if (!laststep.isa("condition-step")) { - throw makeconcept().link(is, "if-not-following-condition", "cond", cond, "action", action); + if (action == "]") { break; } + if (action[action.size()-1] == ':' || action[action.size()-1] == ',') { + label = action; + label.resize(label.size() - 1); + if (label == "return") { throw makeconcept().link(is, "return-label-used"); } + ss >> action; } - if (label.size()) { - throw makeconcept().link(is, "if-case-has-label", "cond", cond, "action", action, "label", label); + if (action == "=" || action == "set") { + ss >> result; + ss >> action; + values.insert(result); } - action.resize(action.size()-1); - if (!labels.count(action)) { - labels.emplace(action, makeconcept()); + if (action[action.size()-1] == '.') { + // is goto + action.resize(action.size() - 1); + if (!labels.count(action)) { + labels.emplace(action, makeconcept()); + } labels[action].link("label", action); + if (laststep.linked("next-step")) { throw makeconcept().link(is, "jump-from-nowhere", "label", action); } + laststep.link("next-step", labels[action]); + laststep = nothing; + continue; } - ref("condition-step-set")(laststep, cond, labels[action]); - // if this improves from being jump, remember to - // update laststep to end of any 'anything' branch - continue; - } - if (laststep == nothing && label.size() == 0) { throw makeconcept().link(is, "no-path-to-code"); } - if (label.size() && !labels.count(label)) { - labels[label] = makeconcept(); - labels[label].link("label", label); - } - ref nextstep = label.size() ? labels[label] : makeconcept(); - if (action == "?" || action == "pick") { - string cond; - ss >> cond; - if (!values.count(cond)) { - throw makeconcept().link(is, "condition-must-be-in-context", condition, cond); + if (action == "if") { + ref cond = lookup(parsevalue(stream)); + ss >> action; + if (action[action.size()-1] != '.') { + throw makeconcept().link(is, "condition-is-not-label", "action", action, "cond", cond); + } + if (!laststep.isa("condition-step")) { + throw makeconcept().link(is, "if-not-following-condition", "cond", cond, "action", action); + } + if (label.size()) { + throw makeconcept().link(is, "if-case-has-label", "cond", cond, "action", action, "label", label); + } + action.resize(action.size()-1); + if (!labels.count(action)) { + labels.emplace(action, makeconcept()); + labels[action].link("label", action); + } + ref("condition-step-set")(laststep, cond, labels[action]); + // if this improves from being jump, remember to + // update laststep to end of any 'anything' branch + continue; } - laststep = ref("set-condition-step")(nextstep, laststep, cond, makeconcept().link("anything", "nothing")); - } else { - // otherwise, action is an action, and we have to read the right number of args - if (laststep.isa("condition-step")) { - if (ref("condition-step-get")(laststep, "anything") != "nothing") { - if (label.size() == 0) { - throw makeconcept().link(is, "condition-already-has-anything-branch-and-steps-follow", condition, laststep); + if (laststep == nothing && label.size() == 0) { throw makeconcept().link(is, "no-path-to-code"); } + if (label.size() && !labels.count(label)) { + labels[label] = makeconcept(); + labels[label].link("label", label); + } + ref nextstep = label.size() ? labels[label] : makeconcept(); + if (action == "?" || action == "pick") { + string cond; + ss >> cond; + if (!values.count(cond)) { + throw makeconcept().link(is, "condition-must-be-in-context", condition, cond); + } + laststep = ref("set-condition-step")(nextstep, laststep, cond, makeconcept().link("anything", "nothing")); + } else { + // otherwise, action is an action, and we have to read the right number of args + if (laststep.isa("condition-step")) { + if (ref("condition-step-get")(laststep, "anything") != "nothing") { + if (label.size() == 0) { + throw makeconcept().link(is, "condition-already-has-anything-branch-and-steps-follow", condition, laststep); + } + } else { + ref("condition-step-set")(laststep, "anything", nextstep); } - } else { - ref("condition-step-set")(laststep, "anything", nextstep); + } else if (laststep != nothing) { + laststep.link("next-step", nextstep); } - } else if (laststep != nothing) { - laststep.link("next-step", nextstep); - } - ref habit = values.count(action) ? action : lookup(action); - ref order = makehabitinformationorder(habit); - ref neededmap = makeconcept(); - ref knownmap = makeconcept(); - string linerest; - std::getline(ss, linerest); - stringstream ss2(linerest); - ref stream2 = alloc(intellect::level0::concepts::allocations(), (istream*)&ss2); - for (ref arg : order.getAll("information-order")) { - ref argname = parsevalue(stream2); - if (!ss2) { break; } - // depending on whether argname is in localcontext, pass to neededmap or knownmap. also parse literal strings. - if (values.count(argname.name())) { - neededmap.link(arg, argname); - } else { - knownmap.link(arg, lookup(argname)); + ref habit = values.count(action) ? action : lookup(action); + ref order = makehabitinformationorder(habit); + ref neededmap = makeconcept(); + ref knownmap = makeconcept(); + string linerest; + std::getline(ss, linerest); + stringstream ss2(linerest); + ref stream2 = alloc(intellect::level0::concepts::allocations(), (istream*)&ss2); + for (ref arg : order.getAll("information-order")) { + ref argname = parsevalue(stream2); + if (!ss2) { break; } + // depending on whether argname is in localcontext, pass to neededmap or knownmap. also parse literal strings. + if (values.count(argname.name())) { + neededmap.link(arg, argname); + } else { + knownmap.link(arg, lookup(argname)); + } } + conceptunmake(order); + dealloc(stream2, intellect::level0::concepts::allocations()); + ref mademap = makeconcept(); + if (result.size()) { + mademap.link("result", values.count(result) ? result : lookup(result)); + } + ref("set-context-step")(nextstep, "nothing", knownmap, neededmap, mademap, habit); + laststep = nextstep; } - dealloc(stream2, intellect::level0::concepts::allocations()); - ref mademap = makeconcept(); - if (result.size()) { - mademap.link("result", values.count(result) ? result : lookup(result)); - } - ref("set-context-step")(nextstep, "nothing", knownmap, neededmap, mademap, habit); - laststep = nextstep; - } - } - } else { - throw ref("parse-error").link("stream", stream, "unexpected-word", cmd); + } + } else { + throw ref("parse-error").link("stream", stream, "unexpected-word", cmd); + } } } @@ -647,7 +665,20 @@ int main() // dump changes to expand from a different node string script = "simpleparser bootstrap-lookup \ -when dump group [\n\ +information dump group linkset\n\ +information dump-expand group linkset\n\ +when dump-expand [\n\ + set link-entry make-concept\n\ + first-link-entry link-entry group\n\ + loop:\n\ + = has-target linked link-entry 'target'\n\ + ? has-target if 'false' return.\n\ + = link-target get link-entry 'target'\n\ + dump link-target linkset\n\ + next-link-entry link-entry\n\ + loop.\n\ +]\n\ +when dump [\n\ = is-in-set in-set group\n\ ? is-in-set if true return.\n\ put-in-set group\n\ @@ -674,13 +705,15 @@ when dump group [\n\ set has-target linked link-entry 'target'\n\ pick has-target if false done2.\n\ set link-type get link-entry 'type'\n\ - pick link-type\n\ - if 'responsibility' continue2.\n\ - if anything next2.\n\ - continue2:\n\ set link-target get link-entry 'target'\n\ + set basic-follow linked linkset 'follow' linktype\n\ + pick basic-follow if 'false' next2.\n\ 'dump' link-target\n\ next2:\n\ + set expand linked linkset 'expand' linktype\n\ + pick expand if 'false' next2b.\n\ + dump-expand link-target linkset\n\ + next2b:\n\ next-link-entry link-entry\n\ loop2.\n\ done2:\n\ @@ -753,11 +786,16 @@ when dump group [\n\ ref memoryfile("memory-000.txt"); - decls(responsibility, interest); - link(responsibility-of-interest, responsibility, dump); - for (ref a = dump; a.linked("next-step"); a = a.get("next-step")) { - (responsibility-of-interest).link(responsibility, dump); - } + ref linksofinterest = makeconcept(); + linksofinterest.link( + "follow", "next-step", + "follow", "needed-map", + "follow", "made-map", + "follow", "known", + "follow", "translation", + "follow", "next-steps", + "expand", "next-steps"); + // structure of steps // [action] [parameter->value ...] repeat // [active memory too small to resolve concern around shape of literals in context] @@ -933,7 +971,7 @@ when dump group [\n\ */ try { std::cerr << intellect::level1::dump(dump, makeconcept()) << std::endl; - dump(responsibility-of-interest); + dump(dump, linksofinterest); #undef ref } catch(intellect::level1::ref r) { std::cerr << intellect::level1::ref(r.ptr()).dump(makeconcept()) << std::endl; -- cgit v1.2.3