Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created October 24, 2013 15:42
Show Gist options
  • Save pasberth/7139535 to your computer and use it in GitHub Desktop.
Save pasberth/7139535 to your computer and use it in GitHub Desktop.
{
open Parser
}
rule token = parse
[' ' '\t'] { token lexbuf }
| ['a'-'z' 'A'-'Z'] { IDENT(Lexing.lexeme lexbuf) }
| '\\' { LAMBDA }
| '.' { DOT }
| ':' '=' { ASSIGN }
| ['\n'] { EOL }
| '(' { OPEN }
| ')' { CLOSE }
USE_OCAMLFIND = true
OCAMLPACKS[] =
llvm
FILES[] =
types
lambda
parser
lexer
OCamlProgram(lambda, $(FILES))
OCamlGeneratedFiles(parser.ml lexer.ml)
open build/OCaml
.SUBDIRS: .
%{
%}
%token <string> IDENT
%token LAMBDA
%token DOT
%token ASSIGN
%token EOL
%token OPEN
%token CLOSE
%start main
%type <Types.stat> main
%%
main:
stat EOL { $1 }
stat:
IDENT ASSIGN expr { Types.Def($1, $3) }
| IDENT ASSIGN app { Types.Def($1, $3) }
| IDENT ASSIGN lam { Types.Def($1, $3) }
expr:
ref { $1 }
| OPEN app CLOSE { $2 }
| OPEN lam CLOSE { $2 }
| OPEN expr CLOSE { $2 }
ref:
IDENT { Types.Ref($1) }
lam:
LAMBDA param_list DOT expr { List.fold_right (fun x y -> Types.Lam(x, y)) $2 $4 }
| LAMBDA param_list DOT app { List.fold_right (fun x y -> Types.Lam(x, y)) $2 $4 }
param_list:
IDENT { [$1] }
| IDENT param_list { $1 :: $2 }
app:
expr expr { Types.App($1, $2) }
| app expr { Types.App($1, $2) }
%%
type ast =
| Ref of (string)
| Lam of (string * ast)
| App of (ast * ast)
type stat =
| Def of (string * ast)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment