~thon/thon

dd2129ec7c09649cf6b6720ea79e5b3b40cc3601 — Evan Bergeron 3 months ago e00cdb5
First newparse test
4 files changed, 74 insertions(+), 1 deletions(-)

A examples/parse00.thon
A parse.sml
M thon.cm
M thon.sml
A examples/parse00.thon => examples/parse00.thon +2 -0
@@ 0,0 1,2 @@
fun zero(a nat):
    z

A parse.sml => parse.sml +55 -0
@@ 0,0 1,55 @@
structure NewParse : PARSE =
struct 

exception UnexpectedToken
exception Unimplemented

fun parse s = A.Zero

fun expect tokens (token : Lex.Token) i =
    if List.nth (tokens, !i) <> token then
        raise UnexpectedToken
    else (i := !i + 1)

fun consumeName tokens i =
    let val res  = 
            (case List.nth (tokens, !i) of
                 Lex.NAME n => n
               | _ => raise UnexpectedToken)
    in i := (!i) + 1;
       res end
        
fun parseType tokens i = 
    case List.nth (tokens, !i) of
        Lex.NAT => (i := (!i) + 1; A.Nat)
      | _ => raise Unimplemented

fun parseExpr tokens i = 
    (
     (case List.nth (tokens, !i) of
          Lex.FUN =>
          let 
              val () = expect tokens Lex.FUN i
              val funcName = consumeName tokens i
              val () = expect tokens Lex.LPAREN i
              (* TODO multiple params *)
              val argName = consumeName tokens i
              val t = parseType tokens i
              val () = expect tokens Lex.RPAREN i
              val () = expect tokens Lex.NEWLINE i
              val () = expect tokens Lex.INDENT i
              val body = parseExpr tokens i
          in 
              A.Fn(argName, t, body)
          end
        | Lex.ZERO => A.Zero
        | _ => raise Unimplemented)
    )

fun parseFile filename = 
    let val tokens = Lex.lexFile filename
        val i = ref 0;
    in
        parseExpr tokens i
    end
end

M thon.cm => thon.cm +2 -1
@@ 8,10 8,11 @@ Group is
  ml-yacc-lib.cm
#endif

  lex.sml
  parse/ast.sml
  parse/thon.grm
  parse/thon.lex
  parse/parse.sml
  lex.sml
  parse.sml
  elaborate.sml
  thon.sml
\ No newline at end of file

M thon.sml => thon.sml +15 -0
@@ 11,6 11,7 @@ structure Thon : sig
                   val run : string -> A.exp
                   val eraseNamesInTyp : A.typ -> A.typ
                   val runFile : string -> A.exp
                   val newRunFile : string -> A.exp
                   val findParseErrors : string -> unit
                   val elaborateDatatypes : A.exp -> A.exp
                   val shiftDeBruijinIndicesInExp : int -> A.exp -> int -> A.exp


@@ 682,6 683,12 @@ fun parseFile filename =
        setDeBruijnIndex ast [] []
    end

fun newParseFile filename =
    let val ast : A.exp = NewParse.parseFile filename
    in
        setDeBruijnIndex ast [] []
    end

fun findParseErrors filename =
    let val _ = parseFile filename
    in


@@ 704,6 711,12 @@ fun runFile s =
        if isval e then e else eval (step e)
    end

fun newRunFile s =
    let val e' = newParseFile s
        val e = elaborateDatatypes e'
    in
        if isval e then e else eval (step e)
    end

(******* Tests *******)



@@ 1267,6 1280,8 @@ val

val true = (Lex.lexFileNoPrintErrMsg "/home/evan/thon/examples/lex03.thon"; false) handle UnexpectedToken => true;

val Fn ("a",Nat,Zero) : Ast.exp = newRunFile "/home/evan/thon/examples/parse00.thon";

in
()
end