From bbd73d5f4a02bd81cb185b20ff3722210b2c05f1 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Tue, 13 Feb 2018 17:43:40 -0600 Subject: [PATCH] initial --- .gitignore | 6 ++++++ Makefile | 28 ++++++++++++++++++++++++++++ lambda.ml | 26 ++++++++++++++++++++++++++ lexer.mll | 12 ++++++++++++ parser.mly | 25 +++++++++++++++++++++++++ types.ml | 9 +++++++++ 6 files changed, 106 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 lambda.ml create mode 100644 lexer.mll create mode 100644 parser.mly create mode 100644 types.ml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..365e168 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +lexer.ml +parser.ml +parser.mli +*.cmo +*.cmi + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0e2a1cd --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +.PHONY: all clean + +types.cmo: types.ml + ocamlc -o $@ -c $^ + +parser: parser.mly + ocamlyacc $^ + +parser.cmo: parser + ocamlc -c parser.mli + ocamlc -c parser.ml + +lexer.cmo: lexer.ml + ocamlc -o $@ -c $^ + +lexer.ml: parser.cmo + ocamllex lexer.mll + +lambda.cmo: lambda.ml + ocamlc -o $@ -c $^ + +clean: + rm -f lexer.ml parser.ml parser.mli + rm -f *.cmo *.cmi + +all: types.cmo lexer.cmo parser.cmo lambda.cmo + ocaml types.cmo parser.cmo lexer.cmo lambda.cmo + diff --git a/lambda.ml b/lambda.ml new file mode 100644 index 0000000..e2870d9 --- /dev/null +++ b/lambda.ml @@ -0,0 +1,26 @@ +open Lexer +open Lexing +open Parser +open Types + +let rec string_of_term (t:term) = + match t with + | TmAbs(x, t1) -> + "(\\" ^ (String.make 1 x) ^ "." ^ (string_of_term t1) ^ ")" + | TmApp(t1, t2) -> + "(" ^ (string_of_term t1) ^ (string_of_term t2) ^ ")" + | TmVar(x) -> + String.make 1 x + +let _ = + try + let rec loop ctx = + print_string "> "; flush stdout; + let lexbuf = Lexing.from_channel stdin in + let result = Parser.main Lexer.token lexbuf in + print_endline (string_of_term result); flush stdout; + loop ctx in + loop [] + with Lexer.Eof -> + print_endline "error"; + exit 0 diff --git a/lexer.mll b/lexer.mll new file mode 100644 index 0000000..f6ac937 --- /dev/null +++ b/lexer.mll @@ -0,0 +1,12 @@ +{ + open Parser + exception Eof +} + +rule token = parse + | ' ' | '\t' { token lexbuf } + | '\n' { EOL } + | '\\' { Lambda } + | '.' { Dot } + | ['a'-'z'] as c | ['A'-'Z'] as c { Ident(c) } + | eof { raise Eof } diff --git a/parser.mly b/parser.mly new file mode 100644 index 0000000..c2959bd --- /dev/null +++ b/parser.mly @@ -0,0 +1,25 @@ +%{ + open Types +%} + +%token EOL +%token Dot +%token Ident +%token Lambda + +%start main +%type main +%% + +main: + expr EOL { $1 } +; +expr: + | var { Types.TmVar($1) } + | Lambda var Dot expr { Types.TmAbs($2, $4) } + | expr expr { Types.TmApp ($1, $2) } +; +var: + Ident { $1 } +; + diff --git a/types.ml b/types.ml new file mode 100644 index 0000000..83286fa --- /dev/null +++ b/types.ml @@ -0,0 +1,9 @@ +type term = + | TmVar of char + | TmAbs of char * term + | TmApp of term * term + +type binding = NameBind + +type context = (char * binding) list +