From 4eb986aa7ab8e3a16c5eadcca5bde31d7f505753 Mon Sep 17 00:00:00 2001 From: bringert Date: Thu, 21 Dec 2006 00:01:45 +0000 Subject: [PATCH] Generate JavaScript type annotator. Use lindef in JavaScript. --- javascript/gflib.js | 150 +++++++++++++++++++++++++++++--------------- 1 file changed, 98 insertions(+), 52 deletions(-) diff --git a/javascript/gflib.js b/javascript/gflib.js index f64fa5f12..fb8016ab2 100644 --- a/javascript/gflib.js +++ b/javascript/gflib.js @@ -5,15 +5,27 @@ function Fun(name) { } Fun.prototype.print = function () { return this.show(0); } ; Fun.prototype.show = function (prec) { - var s = this.name; - var cs = this.getChildren(); - for (var i in cs) { - s += " " + cs[i].show(1); + if (this.name == '?') { + if (isUndefined(this.type)) { + return '?'; + } 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) { return this['arg'+i]; @@ -33,44 +45,6 @@ Fun.prototype.getChildren = function () { 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 */ @@ -104,17 +78,89 @@ function Int(value) { this.value = value; } Int.prototype.print = 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 */ -function Linearizer() { +function Concrete(abstr) { + this.abstr = abstr; this.rules = new Array(); } -Linearizer.prototype.rule = function (name, cs) { return this.rules[name](cs); }; -Linearizer.prototype.addRule = function (name, f) { this.rules[name] = f; }; -Linearizer.prototype.linearize = function (tree) { return this.linearizeToTerm(tree).print(); }; -Linearizer.prototype.linearizeToTerm = function (tree) { +Concrete.prototype.rule = function (name, cs) { return this.rules[name](cs); }; +Concrete.prototype.addRule = function (name, f) { this.rules[name] = f; }; +Concrete.prototype.lindef = function (cat, v) { return this.rules["_d"+cat]([new Str(v)]); } ; +Concrete.prototype.linearize = function (tree) { return this.linearizeToTerm(tree).print(); }; +Concrete.prototype.linearizeToTerm = function (tree) { if (tree.name == '?') { - return new Meta(); + if (isUndefined(tree.type)) { + return new Meta(); + } else { + return this.lindef(tree.type, tree.name); + } } else { var cs = tree.getChildren(); for (var i = 0; i < cs.length; i++) {