forked from GitHub/gf-rgl
Generate JavaScript type annotator. Use lindef in JavaScript.
This commit is contained in:
@@ -5,15 +5,27 @@ function Fun(name) {
|
|||||||
}
|
}
|
||||||
Fun.prototype.print = function () { return this.show(0); } ;
|
Fun.prototype.print = function () { return this.show(0); } ;
|
||||||
Fun.prototype.show = function (prec) {
|
Fun.prototype.show = function (prec) {
|
||||||
var s = this.name;
|
if (this.name == '?') {
|
||||||
var cs = this.getChildren();
|
if (isUndefined(this.type)) {
|
||||||
for (var i in cs) {
|
return '?';
|
||||||
s += " " + cs[i].show(1);
|
} else {
|
||||||
|
var s = '?:' + this.type;
|
||||||
|
if (prec > 0) {
|
||||||
|
s = "(" + s + ")" ;
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var s = this.name;
|
||||||
|
var cs = this.getChildren();
|
||||||
|
for (var i in cs) {
|
||||||
|
s += " " + cs[i].show(1);
|
||||||
|
}
|
||||||
|
if (prec > 0 && cs.length > 0) {
|
||||||
|
s = "(" + s + ")" ;
|
||||||
|
}
|
||||||
|
return s;
|
||||||
}
|
}
|
||||||
if (prec > 0 && cs.length > 0) {
|
|
||||||
s = "(" + s + ")" ;
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
};
|
};
|
||||||
Fun.prototype.getChild = function (i) {
|
Fun.prototype.getChild = function (i) {
|
||||||
return this['arg'+i];
|
return this['arg'+i];
|
||||||
@@ -33,44 +45,6 @@ Fun.prototype.getChildren = function () {
|
|||||||
return a;
|
return a;
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
/* Hack to get around the fact that our SISR doesn't build real Fun objects. */
|
|
||||||
function copyTree(x) {
|
|
||||||
if (typeof x == 'string' && x == '?') {
|
|
||||||
return new Fun('?');
|
|
||||||
} else {
|
|
||||||
var t = new Fun(x.name);
|
|
||||||
for (var i = 0; ; i++) {
|
|
||||||
var c = x['arg' + i];
|
|
||||||
if (isUndefined(c)) { break; }
|
|
||||||
t.setChild(i, copyTree(c));
|
|
||||||
}
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseTree(str) {
|
|
||||||
return parseTree_(str.match(new RegExp("[\\w\\']+|\\(|\\)","g")), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseTree_(tokens, prec) {
|
|
||||||
if (tokens.length == 0 || tokens[0] == ")") { return null; }
|
|
||||||
var t = tokens.shift();
|
|
||||||
if (t == "(") {
|
|
||||||
var tree = parseTree_(tokens, 0);
|
|
||||||
tokens.shift();
|
|
||||||
return tree;
|
|
||||||
} else {
|
|
||||||
var tree = new Fun(t);
|
|
||||||
if (prec == 0) {
|
|
||||||
var c;
|
|
||||||
var i;
|
|
||||||
for (i = 0; (c = parseTree_(tokens, 1)) !== null; i++) {
|
|
||||||
tree.setChild(i,c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tree;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Concrete syntax terms */
|
/* Concrete syntax terms */
|
||||||
|
|
||||||
@@ -104,17 +78,89 @@ function Int(value) { this.value = value; }
|
|||||||
Int.prototype.print = function() { return this.value; };
|
Int.prototype.print = function() { return this.value; };
|
||||||
Int.prototype.toIndex = function() { return this.value; };
|
Int.prototype.toIndex = function() { return this.value; };
|
||||||
|
|
||||||
|
/* Type annotation */
|
||||||
|
|
||||||
|
function Abstract() {
|
||||||
|
this.types = new Array();
|
||||||
|
}
|
||||||
|
Abstract.prototype.addType = function(fun, args, cat) {
|
||||||
|
this.types[fun] = new Type(args, cat);
|
||||||
|
} ;
|
||||||
|
Abstract.prototype.annotate = function(tree, type) {
|
||||||
|
if (tree.name == '?') {
|
||||||
|
tree.type = type;
|
||||||
|
} else {
|
||||||
|
var typ = this.types[tree.name];
|
||||||
|
var cs = tree.getChildren();
|
||||||
|
for (var i = 0; i < cs.length; i++) {
|
||||||
|
this.annotate(cs[i], typ.args[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tree;
|
||||||
|
} ;
|
||||||
|
/* Hack to get around the fact that our SISR doesn't build real Fun objects. */
|
||||||
|
Abstract.prototype.copyTree = function(x, type) {
|
||||||
|
return this.annotate(this.copyTree_(x), type);
|
||||||
|
};
|
||||||
|
Abstract.prototype.copyTree_ = function(x) {
|
||||||
|
if (typeof x == 'string' && x == '?') {
|
||||||
|
return new Fun('?');
|
||||||
|
} else {
|
||||||
|
var t = new Fun(x.name);
|
||||||
|
for (var i = 0; ; i++) {
|
||||||
|
var c = x['arg' + i];
|
||||||
|
if (isUndefined(c)) { break; }
|
||||||
|
t.setChild(i, this.copyTree(c));
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
} ;
|
||||||
|
Abstract.prototype.parseTree = function(str, type) {
|
||||||
|
return this.annotate(this.parseTree_(str.match(/[\w\']+|\(|\)|\?/g), 0), type);
|
||||||
|
} ;
|
||||||
|
Abstract.prototype.parseTree_ = function(tokens, prec) {
|
||||||
|
if (tokens.length == 0 || tokens[0] == ")") { return null; }
|
||||||
|
var t = tokens.shift();
|
||||||
|
if (t == "(") {
|
||||||
|
var tree = this.parseTree_(tokens, 0);
|
||||||
|
tokens.shift();
|
||||||
|
return tree;
|
||||||
|
} else if (t == '?') {
|
||||||
|
return new Fun('?');
|
||||||
|
} else {
|
||||||
|
var tree = new Fun(t);
|
||||||
|
if (prec == 0) {
|
||||||
|
var c, i;
|
||||||
|
for (i = 0; (c = this.parseTree_(tokens, 1)) !== null; i++) {
|
||||||
|
tree.setChild(i,c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tree;
|
||||||
|
}
|
||||||
|
} ;
|
||||||
|
|
||||||
|
function Type(args, cat) {
|
||||||
|
this.args = args;
|
||||||
|
this.cat = cat;
|
||||||
|
}
|
||||||
|
|
||||||
/* Linearization */
|
/* Linearization */
|
||||||
|
|
||||||
function Linearizer() {
|
function Concrete(abstr) {
|
||||||
|
this.abstr = abstr;
|
||||||
this.rules = new Array();
|
this.rules = new Array();
|
||||||
}
|
}
|
||||||
Linearizer.prototype.rule = function (name, cs) { return this.rules[name](cs); };
|
Concrete.prototype.rule = function (name, cs) { return this.rules[name](cs); };
|
||||||
Linearizer.prototype.addRule = function (name, f) { this.rules[name] = f; };
|
Concrete.prototype.addRule = function (name, f) { this.rules[name] = f; };
|
||||||
Linearizer.prototype.linearize = function (tree) { return this.linearizeToTerm(tree).print(); };
|
Concrete.prototype.lindef = function (cat, v) { return this.rules["_d"+cat]([new Str(v)]); } ;
|
||||||
Linearizer.prototype.linearizeToTerm = function (tree) {
|
Concrete.prototype.linearize = function (tree) { return this.linearizeToTerm(tree).print(); };
|
||||||
|
Concrete.prototype.linearizeToTerm = function (tree) {
|
||||||
if (tree.name == '?') {
|
if (tree.name == '?') {
|
||||||
return new Meta();
|
if (isUndefined(tree.type)) {
|
||||||
|
return new Meta();
|
||||||
|
} else {
|
||||||
|
return this.lindef(tree.type, tree.name);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
var cs = tree.getChildren();
|
var cs = tree.getChildren();
|
||||||
for (var i = 0; i < cs.length; i++) {
|
for (var i = 0; i < cs.length; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user