summaryrefslogtreecommitdiff
path: root/src/www/syntax-editor/ast.js
diff options
context:
space:
mode:
authorjohn.j.camilleri <john.j.camilleri@chalmers.se>2012-12-03 14:02:47 +0000
committerjohn.j.camilleri <john.j.camilleri@chalmers.se>2012-12-03 14:02:47 +0000
commit314052f8d3b380c9b1562aafdbaf4af445e7fd82 (patch)
treeaf8d1daa4a4d31e4b1c5bccaed6dbbb89ed2c701 /src/www/syntax-editor/ast.js
parente174f37940d4c9480d83e57bf7bf453dd2b3c9de (diff)
Syntax editor: add wrap feature
Diffstat (limited to 'src/www/syntax-editor/ast.js')
-rw-r--r--src/www/syntax-editor/ast.js25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/www/syntax-editor/ast.js b/src/www/syntax-editor/ast.js
index 20e0c7019..5aa55d230 100644
--- a/src/www/syntax-editor/ast.js
+++ b/src/www/syntax-editor/ast.js
@@ -35,6 +35,10 @@ function NodeID(x) {
return new NodeID( this );
}
+ // Return NodeID as string
+ this.toString = function() {
+ return this.id.toString();
+ }
}
/* --- Abstract Syntax Tree (with state)------------------------------------- */
@@ -42,7 +46,7 @@ function NodeID(x) {
function ASTNode(data) {
for (var d in data) this[d]=data[d];
this.children = [];
- for (var c in data.children) {
+ if (data) for (var c in data.children) {
this.children.push( new ASTNode(data.children[c]) );
}
this.hasChildren = function(){
@@ -103,6 +107,10 @@ function AST(fun, cat) {
this.currentNode.cat = c;
}
+ this.hasParent = function() {
+ return this.currentID.get().length > 1;
+ }
+
// Add a single type dependency at current node
this.addDep = function(k, type) {
// Add unassigned type variable to current
@@ -115,7 +123,7 @@ function AST(fun, cat) {
return node;
}
- // Add a single fun at current node
+ // Add a node as child of current node
this.add = function(fun, cat) {
var node = newNode(fun,cat);
this._add(this.currentID, node);
@@ -128,6 +136,19 @@ function AST(fun, cat) {
x.children.push(node);
}
+ // Wrap the current node inside another node
+ this.wrap = function(typeobj, childid) {
+ var subtree = new ASTNode(this.currentNode);
+ this.currentNode.fun = typeobj.name.join(" ");
+ this.currentNode.cat = typeobj.ret;
+ this.currentNode.children = [];
+ for (var i in typeobj.args) {
+ this.add(null, typeobj.args[i]);
+ }
+ this.currentNode.children[i] = subtree;
+ return subtree;
+ }
+
// Determine if current node is writable (empty/no children)
this.is_writable=function() {
var cn = this.currentNode;