1
0
forked from GitHub/gf-core

Syntax editor: jump to next hole in tree

This commit is contained in:
john.j.camilleri
2012-11-15 15:38:17 +00:00
parent 3216a5def6
commit 48dbabcbbe
2 changed files with 46 additions and 6 deletions

View File

@@ -5,7 +5,8 @@ function Tree(value) {
var createNode = function(value, children) {
var node = {
value: value,
children: []
children: [],
hasChildren: function(){ return this.children.length > 0; }
};
if (children != undefined)
for (c in children)
@@ -106,6 +107,43 @@ function AST(fun, cat) {
this.tree.find(this.current).children = [];
}
// generic HOF for traversing tree
this.traverse = function(f) {
function visit(id, node) {
f(node);
for (i in node.children) {
var newid = new NodeID(id);
newid.add(parseInt(i));
visit(newid, node.children[i]);
}
}
visit(new NodeID(), this.tree.root);
}
// Move current ID to next hole
this.toNextHole = function() {
var id = new NodeID(this.current);
// loop until we're at top
while (id.get().length > 0) {
var node = this.tree.find(id);
// first check children
for (i in node.children) {
var child = node.children[i];
if (!child.value.fun) {
var newid = new NodeID(id);
newid.add(i);
this.current = newid;
return;
}
}
// otherwise go up to parent
id.get().pop();
}
}
// Move current id to child number i
this.toChild = function(i) {
this.current.add(i);

View File

@@ -137,14 +137,16 @@ Editor.prototype.complete_refinement=function(data) {
for (ci in fun_args) {
ast.add(null, fun_args[ci]);
}
// Select next hole & get its refinements
ast.toChild(0);
update_current_node();
get_refinements();
}
// Update vis
redraw_tree();
update_linearisation();
// Select next hole & get its refinements
ast.toNextHole();
update_current_node();
get_refinements();
}
}