|
| 1 | +structure Parse : |
| 2 | + sig |
| 3 | + val parse : string -> Absyn.exp |
| 4 | + val parseTestcase : string -> Absyn.exp |
| 5 | + val parseAllTests : unit -> Absyn.exp list |
| 6 | + val test : string -> unit |
| 7 | + val testLocal : string -> unit |
| 8 | + val testAll : unit -> unit |
| 9 | + end = |
| 10 | +struct |
| 11 | +structure TigerLrVals = TigerLrValsFun(structure Token = LrParser.Token) |
| 12 | +structure Lex = TigerLexFun(structure Tokens = TigerLrVals.Tokens) |
| 13 | +structure TigerP = Join(structure ParserData = TigerLrVals.ParserData |
| 14 | + structure Lex=Lex |
| 15 | + structure LrParser = LrParser) |
| 16 | +fun parse filename = |
| 17 | + let val _ = (ErrorMsg.reset(); ErrorMsg.fileName := filename) |
| 18 | + val file = TextIO.openIn filename |
| 19 | + fun get _ = TextIO.input file |
| 20 | + fun parseerror(s,p1,p2) = ErrorMsg.error p1 s |
| 21 | + val lexer = LrParser.Stream.streamify (Lex.makeLexer get) |
| 22 | + val (absyn, _) = TigerP.parse(30,lexer,parseerror,()) |
| 23 | + in TextIO.closeIn file; |
| 24 | + absyn |
| 25 | + end handle LrParser.ParseError => raise ErrorMsg.Error |
| 26 | + |
| 27 | +fun parseTestcase name = |
| 28 | + parse ("../tests/" ^ name ^ ".tig") |
| 29 | + |
| 30 | +fun test file = |
| 31 | + let val exp = parseTestcase file |
| 32 | + val ostream = TextIO.openOut ("./out/" ^ file ^ ".out") |
| 33 | + in |
| 34 | + PrintAbsyn.print (ostream, exp) |
| 35 | + end |
| 36 | + |
| 37 | +fun testLocal file = |
| 38 | + let val exp = parse file |
| 39 | + val ostream = TextIO.openOut("./out/" ^ file ^ "_local.out") |
| 40 | + in |
| 41 | + PrintAbsyn.print (ostream, exp) |
| 42 | + end |
| 43 | + |
| 44 | +fun getAllTestCases () = |
| 45 | + let val tests = ["merge", "queens"]; |
| 46 | + fun addTests (0, tests) = tests |
| 47 | + | addTests (n, tests) = addTests (n - 1, ("test" ^ Int.toString n)::tests) |
| 48 | + in |
| 49 | + addTests (49, tests) |
| 50 | + end |
| 51 | + |
| 52 | +fun parseAllTests () = |
| 53 | + map parseTestcase (getAllTestCases ()) |
| 54 | + |
| 55 | +fun testAll () = |
| 56 | + app test (getAllTestCases ()) |
| 57 | +end |
0 commit comments