From 7c6c2bba7c885fe28d774c43695498e975f987fb Mon Sep 17 00:00:00 2001 From: Hiroshi Sakurai Date: Sat, 25 Jul 2015 17:00:02 +0900 Subject: [PATCH] changed js and scala --- beautifier/Makefile | 7 ++- beautifier/beauty.js | 116 +++++++++--------------------------- beautifier/beauty.py | 2 +- beautifier/mincaml2js.scala | 39 ++++++------ beautifier/test.js | 74 +++++++++++++++++++++++ 5 files changed, 124 insertions(+), 114 deletions(-) create mode 100644 beautifier/test.js diff --git a/beautifier/Makefile b/beautifier/Makefile index e5cf4c5..c4c0271 100644 --- a/beautifier/Makefile +++ b/beautifier/Makefile @@ -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 diff --git a/beautifier/beauty.js b/beautifier/beauty.js index d58c9ba..8f62e18 100644 --- a/beautifier/beauty.js +++ b/beautifier/beauty.js @@ -1,5 +1,4 @@ - -function parse(i){ +exports.parse = function(i){ function startsWith(i, param) { var ilen = i.length; var len = param.length; @@ -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) { @@ -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]); @@ -39,19 +38,10 @@ 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]]; }; }; @@ -59,10 +49,10 @@ function parse(i){ 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; @@ -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()]; }); } @@ -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; } @@ -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"+ - "")); +}; diff --git a/beautifier/beauty.py b/beautifier/beauty.py index 0a548c7..e717865 100644 --- a/beautifier/beauty.py +++ b/beautifier/beauty.py @@ -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(";;")) \ diff --git a/beautifier/mincaml2js.scala b/beautifier/mincaml2js.scala index bb966f1..a8fdfc8 100644 --- a/beautifier/mincaml2js.scala +++ b/beautifier/mincaml2js.scala @@ -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] = { @@ -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) diff --git a/beautifier/test.js b/beautifier/test.js new file mode 100644 index 0000000..3146a9b --- /dev/null +++ b/beautifier/test.js @@ -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"))); +} +