~thon/thon

7aad84c68ac182f34d6bdcc33da3c9591c4f57a2 — Evan Bergeron 3 months ago ee41ae6
Lex let and anon funcs
2 files changed, 37 insertions(+), 4 deletions(-)

M lex.sml
M thon.sml
M lex.sml => lex.sml +30 -4
@@ 1,13 1,13 @@
structure Lex : sig
datatype Token = FUN | NAT | COLON | LPAREN | RPAREN | NAME of string | INDENT | DEDENT | RETURN
datatype Token = FUN | FN | NAT | COLON | LPAREN | RPAREN | NAME of string | INDENT | DEDENT | RETURN | ZERO | SUCC | LET | SARROW | EQUAL | DARROW
val lexFile : string -> Token list
end  =
struct

datatype Token = FUN | NAT | COLON | LPAREN | RPAREN | NAME of string | INDENT | DEDENT | RETURN

datatype Token = FUN | FN | NAT | COLON | LPAREN | RPAREN | NAME of string | INDENT | DEDENT | RETURN | ZERO | SUCC | LET | SARROW | EQUAL | DARROW

exception UnexpectedIndentLevel
exception UnexpectedToken

fun lookaheadN s n =
    (* Can raise Size *)


@@ 82,7 82,33 @@ and lexLines' s out indentLevel =
            else
                raise UnexpectedIndentLevel
        end
      | "f" => eatKeywordOrName ("fun", FUN) s indentLevel out
      | "=" =>
        if onKeyword "=>" s then (
            eatWord "=>" s;
            lexLines' s (DARROW::out) indentLevel
        ) else if onKeyword "=" s then (
            eatWord "=" s;
            lexLines' s (EQUAL::out) indentLevel
        ) else (
            raise UnexpectedToken
        )
      | "-" => eatKeywordOrName ("->", SARROW) s indentLevel out
      | "z" => eatKeywordOrName ("z", ZERO) s indentLevel out
      | "s" => eatKeywordOrName ("s", SUCC) s indentLevel out
      | "l" => eatKeywordOrName ("let", LET) s indentLevel out
      | "f" => (* eatKeywordOrName ("fun", FUN) s indentLevel out *)
        if onKeyword "fun" s then (
            eatWord "fun" s;
            lexLines' s (FUN::out) indentLevel
        ) else if onKeyword "fn" s then (
            eatWord "fn" s;
            lexLines' s (FN::out) indentLevel
        ) else (
            let val name = getName s in
                eatWord name s;
                lexLines' s ((NAME name)::out) indentLevel
            end
        )
      | "n" => eatKeywordOrName ("nat", NAT) s indentLevel out
      | "r" => eatKeywordOrName ("return", RETURN) s indentLevel out
      | "(" => (

M thon.sml => thon.sml +7 -0
@@ 1240,6 1240,13 @@ open Lex;
val [FUN,NAME "foo",LPAREN,NAME "a",NAT,RPAREN,INDENT,FUN,NAME "bar",LPAREN,
     NAME "n",NAT,RPAREN,INDENT,RETURN,NAME "n",DEDENT,RETURN,NAME "a"] = Lex.lexFile "/home/evan/thon/examples/lex00.thon";

val
  [FUN,NAME "foo",LPAREN,NAME "a",NAT,RPAREN,INDENT,LET,NAME "x",NAT,SARROW,
   NAT,EQUAL,FN,LPAREN,NAME "x",NAT,RPAREN,NAT,DARROW,SUCC,NAME "x",FUN,
   NAME "bar",LPAREN,NAME "n",NAT,RPAREN,INDENT,RETURN,NAME "n",DEDENT,RETURN,
   NAME "a"] : Lex.Token list =
    Lex.lexFile "/home/evan/thon/examples/lex01.thon";

in
()
end