Skip to content

Commit

Permalink
changed js and scala
Browse files Browse the repository at this point in the history
  • Loading branch information
hsk committed Jul 25, 2015
1 parent 9ed020d commit 7c6c2bb
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 114 deletions.
7 changes: 5 additions & 2 deletions beautifier/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ default: mincaml2js test
mincaml2js: mincaml2js.scala
scalac mincaml2js.scala
test:
scala mincaml2js.test

time scala mincaml2js.test
js:
time node test.js
py:
time python test.py
clean:
rm -rf mincaml2js
116 changes: 27 additions & 89 deletions beautifier/beauty.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

function parse(i){
exports.parse = function(i){
function startsWith(i, param) {
var ilen = i.length;
var len = param.length;
Expand All @@ -11,7 +10,7 @@ function parse(i){
if(i.length > 0) return [i.substring(0,1), i.substring(1)];
else return null;
}
var str = function(s) { return skip.seq(nstr(s));};
var str = function(s) { return p(skip, nstr(s));};


Function.prototype.or=function(that) {
Expand All @@ -22,7 +21,7 @@ function parse(i){
};
};

function seq() {
function p() {
var args = arguments;
for(var i = 0; i < args.length;i++) {
if(typeof(args[i])=="string") args[i]=str(args[i]);
Expand All @@ -39,30 +38,21 @@ function parse(i){
};
}

Function.prototype.seq=function (that) {
var thiz = this;
return function(i) {
var r1 = thiz(i); if(r1 === null) return null;
var r2 = that(r1[1]); if(r2 === null) return null;
return [[r1[0], r2[0]], r2[1]];
};
};

Function.prototype.next=function(that) {
var thiz = this;
return function(i) {
var r = thiz.seq(that)(i); if(r === null) return r;
var r = p(thiz,that)(i); if(r === null) return r;
return [r[0][1], r[1]];
};
};

Function.prototype.prev=function(that) {
var thiz = this;
return function(i) {
var r = thiz.seq(that)(i); if(r === null) return r;
var r = p(thiz,that)(i); if(r === null) return r;
return [r[0][0], r[1]];
};
}
};

Function.prototype.action=function(f) {
var thiz = this;
Expand Down Expand Up @@ -127,22 +117,22 @@ function parse(i){
function nreg(param) {
return function(i) {
var m = i.match(param);
if(m === null) return null;
if (m === null) return null;
return [m[0],i.substring(m[0].length)];
};
}

var skip = nreg(/^(\s|\(\*[\s\S]*\*\))*/);

var reg = function(s) { return skip.seq(nreg(s));};
var reg = function(s) { return p(skip, nreg(s));};

function NestP(){this.p=true;}
function NestM(){this.m=true;}


function n(a,b,c) {
return seq(a,b,c).action(function(a){
return [a[0],new NestP(),a[1],new NestM(),a[2]];
function n() {
return p.apply(this,arguments).action(function(a){
return [new NestP(),a,new NestM()];
});
}

Expand Down Expand Up @@ -209,7 +199,7 @@ function parse(i){
}

e2 = [];
for(var i in e3) {
for (var i in e3) {
var s = e3[i];

if(s instanceof NestP) {nest++; continue; }
Expand All @@ -228,77 +218,25 @@ function parse(i){
var keywords = reg(/^(let|in|if|else|then|rec|begin|end|match|with)\b/);


var id = (notp(keywords).next( reg(/^[_a-zA-Z0-9]+/)))
.or( reg(/^[+\-*\/.<>=:@]+/) )
.or( reg(/^[,!]/) )
.or( reg(/^("(\\.|[^"])*")/) );
var exp1 = n("begin", exp, "end")
.or(seq("match", n("",exp,""), "with", opt("|"), n("",exp,""), rep(seq("|", n("",exp,""))) ) )
.or( n("(", opt(exp), ")") )
.or( n("{", opt(exp), "}") )
.or( n("[", opt(exp), "]") )
.or( seq(n("let", seq(opt("rec"), exp), "in"), exp) )
.or( seq(n("type", seq(id, "=", exp), ";;"), exp) )
.or( n("type", seq(id, "=", n(opt("|"), exp, ""), rep(n("|", exp, ""))), ";;") )
.or( seq(n("if", exp, ""), n("then", exp, "else"), exp) )
var id = (notp(keywords).next(reg(/^[_a-zA-Z0-9]+/)))
.or(reg(/^[+\-*\/.<>=:@]+/))
.or(reg(/^[,!]/))
.or(reg(/^("(\\.|[^"])*")/));
var exp1 = p("begin", n(exp), "end")
.or(p(p("match").or(p("try")), n(exp), "with", opt("|"), n(exp), rep(p("|", n(exp)))))
.or(p("(", n(opt(exp)), ")"))
.or(p("{", n(opt(exp)), "}"))
.or(p("[", n(opt(exp)), "]"))
.or(p("let", n(p(opt("rec"), exp)), "in", exp))
.or(p("type", n(p(id, "=", exp)), ";;", exp))
.or(p("type", n(id, "=", opt("|"), n(exp), rep(p("|", n(exp)))), ";;"))
.or(p("if", n(exp), "then", n(exp), "else", exp))
.or(id);
var exps = rep1(exp1);
function exp(i) {
return exps.seq(rep(str(";").next(exps)))(i);
return p(exps, rep(p(";", exps)))(i);
}
var e = exp(i);
//console.log(JSON.stringify(e));
return cnv(flat(e));
}


console.log(parse("begin\n1 + 2\nend"));


console.log(parse(
"let a = 1 in\n"+
"if a then b else c\n"+
""));

console.log(parse(
"let a = 1 in\n"+
"if a\n"+
"then b\n"+
"else c\n"+
""));

console.log(parse(
"let a = 1 in\n"+
"if a\n"+
"then\n"+
"b\n"+
"else\n"+
"c\n"+
""));

console.log(parse(
"let a =\n"+
"let a = 1 in\n"+
"if a > 10 \n"+
"then\n"+
"b\n"+
"else\n"+
"c\n"+
"in\n"+
"a + b\n"+
""));

console.log(parse(
"if a > 10 then\n"+
"b\n"+
"else if a > 20 then\n"+
"c\n"+
"else\n"+
"d\n"+
""));

console.log(parse(
"if a > 10 then b else\n"+
"if a > 20 then c else\n"+
"d\n"+
""));
};
2 changes: 1 addition & 1 deletion beautifier/beauty.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def parse(s):
| p("[", n(~exp), "]") \
| p("if", n(exp), "then", n(exp), "else", exp) \
| p("let", n(~p("rec"), exp), "in", exp) \
| p((st("match") | st("try")), n(exp), "with", ~p("|"), n(exp), -p("|", n(exp))) \
| p(p("match") | p("try"), n(exp), "with", ~p("|"), n(exp), -p("|", n(exp))) \
| p("function", ~p("|"), n(exp), -p("|", n(exp))) \
| p("type", n(id, "=", ~p("|"), n(exp), -p("|", n(exp))), ~p(";;")) \
| p("type", n(id, "=", exp), ~p(";;")) \
Expand Down
39 changes: 17 additions & 22 deletions beautifier/mincaml2js.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,8 @@ class PrityPrintParser extends RegexParsers {
}
}

def p[A](p:Parser[A]):Parser[List[Any]] = p ^^ {a => List(a,NestP) }
def m[A](p:Parser[A]):Parser[List[Any]] = p ^^ {a => List(NestM,a) }

def nest(p:(Parser[Any],Parser[Any],Parser[Any])):Parser[List[Any]] =
p._1~p._2~p._3 ^^ {case a~b~c => List(a,NestP,b,NestM,c) }
def n(p:Parser[Any]):Parser[List[Any]] =
p ^^ {case a~b~c => List(a,NestP,b,NestM,c)}
p ^^ {List(NestP,_,NestM)}


def flat(a:Any):List[Any] = {
Expand Down Expand Up @@ -128,23 +123,23 @@ object parse extends PrityPrintParser {

override protected val whiteSpace = """(?s)(\s|\(\*.*\*\))+""".r

def keywords = { """(let|in|if|else|then|rec|begin|end|match|with)\b""".r }
def id = { not(keywords) ~> """[_a-zA-Z0-9]+""".r }.
| { """[+\-*/.<>=:@]+""".r }.
| { """[,!]""".r }.
| { """("(\\.|[^"])*")""".r }
def keywords = ( """(let|in|if|else|then|rec|begin|end|match|with|try)\b""".r )
def id = ( not(keywords) ~> """[_a-zA-Z0-9]+""".r ).
| ( """[+\-*/.<>=:@]+""".r ).
| ( """[,!]""".r ).
| ( """("(\\.|[^"])*")""".r )
def exp:Parser[Any]
= { exps ~ rep(";" ~ exps) }
def exps = { rep1(exp1) }
val exp1 = ( n{"begin" ~ exp ~ "end"} ).
| ( "match" ~ n(""~exp~"") ~ "with" ~ opt("|") ~ n{""~exp~""} ~ rep("|" ~ n{""~exp~""}) ).
| ( n{"(" ~ opt(exp) ~ ")"} ).
| ( n{"{" ~ opt(exp) ~ "}"} ).
| ( n{"[" ~ opt(exp) ~ "]"} ).
| ( n{"let" ~ (opt("rec") ~ exp) ~ "in"} ~ exp ).
| ( n{"type" ~ (id ~ "=" ~ exp) ~ ";;"} ~ exp ).
| ( n{"type" ~ {id ~ "=" ~ n(opt("|") ~ exp ~ "") ~ rep(n("|" ~ exp ~ ""))} ~ ";;"} ).
| ( n{"if" ~ exp ~ ""} ~ n{"then" ~ exp ~ "else"} ~ exp ).
= ( exps ~ rep(";" ~ exps) )
def exps = ( rep1(exp1) )
val exp1 = ( "begin" ~ n(exp) ~ "end" ).
| ( ("match" | "try") ~ n(exp) ~ "with" ~ opt("|") ~ n{exp} ~ rep("|" ~ n{exp}) ).
| ( "(" ~ n{opt(exp)} ~ ")" ).
| ( "{" ~ n{opt(exp)} ~ "}" ).
| ( "[" ~ n{opt(exp)} ~ "]" ).
| ( "let" ~ n(opt("rec") ~ exp) ~ "in" ~ exp ).
| ( "type" ~ n(id ~ "=" ~ exp) ~ ";;" ~ exp ).
| ( "type" ~ n{id ~ "=" ~ n(opt("|") ~ exp) ~ rep("|" ~ n(exp))} ~ ";;" ).
| ( "if" ~ n{exp} ~ "then" ~ n{exp} ~ "else" ~ exp ).
| ( id )

def apply(str: String):String = apply(exp,str)
Expand Down
74 changes: 74 additions & 0 deletions beautifier/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

var beauty = require('./beauty.js');
var parse=beauty.parse;


console.log(parse("begin\n1 + 2\nend"));


console.log(parse(
"let a = 1 in\n"+
"if a then b else c\n"+
""));

console.log(parse(
"let a = 1 in\n"+
"if a\n"+
"then b\n"+
"else c\n"+
""));

console.log(parse(
"let a = 1 in\n"+
"if a\n"+
"then\n"+
"b\n"+
"else\n"+
"c\n"+
""));

console.log(parse(
"let a =\n"+
"let a = 1 in\n"+
"if a > 10 \n"+
"then\n"+
"b\n"+
"else\n"+
"c\n"+
"in\n"+
"a + b\n"+
""));

console.log(parse(
"if a > 10 then\n"+
"b\n"+
"else if a > 20 then\n"+
"c\n"+
"else\n"+
"d\n"+
""));

console.log(parse(
"if a > 10 then b else\n"+
"if a > 20 then c else\n"+
"d\n"+
""));

var fs = require('fs');

var tests = [
"print", "sum-tail", "gcd", "sum", "fib", "ack", "even-odd",
"adder", "funcomp", "cls-rec", "cls-bug", "cls-bug2",
"shuffle", "spill", "spill2", "spill3", "join-stack", "join-stack2", "join-stack3",
"join-reg", "join-reg2", "non-tail-if", "non-tail-if2",
"inprod", "inprod-rec", "inprod-loop", "matmul", "matmul-flat",
"ref", "record", "string", "as", "list1", "match", "begin",
"variant", "when", "list"
];

for(var i in tests) {
var f = tests[i];
var path = "../test/" + f + ".ml";
console.log(parse(fs.readFileSync(path,"utf-8")));
}

0 comments on commit 7c6c2bb

Please sign in to comment.