Commit 1854c887 authored by Julien Lopez's avatar Julien Lopez
Browse files

[TESTS][EVAL] Improvments in lexer; beginning to match with lambda.ml;

	GRAMMAR file added; some fixes.
parent 099784a2
(* TODO: Update this grammar if needed.
Formaly add comments.
expr = id
| expr expr
| abstr
| "(" expr "," expr ")"
| "match" expr "with" "|" expr ["&" type_id] "->" expr branches
abstr = "let" ["fun"] id "=" expr
| "let" ["fun"] "_" "=" expr
branches = (* empty *)
| "|" expr ["&" type_id] "->" expr branches
id = [A-Za-z][A-Za-z0-9]*
OCAMLC ?= ocamlc
ROOTDIR ?= ../..
SRCDIR ?= src
INCDIR ?= src
EXTDIR ?= $(SRCDIR)/externals
INCDIR ?= -I $(SRCDIR) -I $(EXTDIR)
FILES = lexer.ml parser.ml
SRCFILES = $(FILES:%=$(SRCDIR)/%) $(SRCDIR)/main.ml
OBJSRCFILES = $(SRCFILES:%.ml=%.cmo)
INCFILES = $(FILES:%=$(SRCDIR)/%i)
OBJINCFILES = $(INCFILES:%.mli=%.cmi)
# TODO: Improve this, we have to add twice an external file in this Makefile
INEXTFILES = misc/custom.ml misc/encodings.ml types/ident.ml\
compile/lambda.mli compile/lambda.ml misc/ns.ml misc/ns.mli misc/upool.ml\
misc/upool.mli types/sortedList.ml types/sortedList.mli types/compunit.ml\
types/compunit.mli types/types.mli types/types.ml
INEXTSRCFILES = custom.ml encodings.ml upool.mli upool.ml ns.mli ns.ml\
sortedList.mli sortedList.ml ident.ml compunit.mli compunit.ml types.mli\
types.ml lambda.mli lambda.ml
EXTFILES = $(INEXTFILES:%=$(ROOTDIR)/%)
EXTSRCFILES = $(INEXTSRCFILES:%=$(EXTDIR)/%)
TMPEXTOBJFILES = $(EXTSRCFILES:%.ml=%.cmo)
EXTOBJFILES = $(TMPEXTOBJFILES:%.mli=%.cmi)
OTHERFILES = str.cma
RM ?= rm -f
OUT ?= lambdaparser
OBJFILES = $(OBJSRCFILES) $(OBJINCFILES) $(EXTOBJFILES)
all: $(OBJINCFILES) $(OBJSRCFILES)
$(OCAMLC) -I $(INCDIR) -o $(OUT) $(OTHERFILES) $(OBJSRCFILES)
.PHONY: clean check test
%.cmo: %.ml
$(OCAMLC) -c -I $(INCDIR) -o $@ $<
all: _import $(EXTOBJFILES) $(OBJINCFILES) $(OBJSRCFILES)
$(OCAMLC) $(INCDIR) -o $(OUT) $(OTHERFILES) $(EXTOBJFILES) $(OBJSRCFILES)
%.cmi: %.mli
$(OCAMLC) -c -I $(INCDIR) -o $@ $<
_import:
@echo -n "Copying external files..."
@test -d $(EXTDIR) || mkdir $(EXTDIR)
@cp $(EXTFILES) $(EXTDIR)
@echo "done"
clean:
$(RM) $(OBJINCFILES) $(OBJSRCFILES) $(SRCDIR)/main.cmi $(OUT)
$(RM) $(OBJFILES) $(EXTSRCFILES) $(EXTINCFILES) $(SRCDIR)/main.cmi $(OUT)
test $(EXTDIR) = "src" || test $(EXTDIR) = "." || $(RM) -r $(EXTDIR)
check: test
test: all
tests/test.sh
.PHONY: clean
%.cmo: %.ml
$(OCAMLC) -c $(INCDIR) -o $@ $<
%.cmi: %.mli
$(OCAMLC) -c $(INCDIR) -o $@ $<
......@@ -17,7 +17,7 @@ let rec gen_ops ops list = match list with
| [] -> ops
let keywords = gen_keywords (StrMap.add "let" (Keyword "let") StrMap.empty)
[ "fun"; "match"; "with" ]
[ "fun"; "match"; "with"; "_" ]
let ops = gen_ops (StrMap.add "=" (Operator "=") StrMap.empty)
[ "("; ")"; ","; "|"; "&"; "->"; ";" ]
......@@ -26,20 +26,19 @@ let ops = gen_ops (StrMap.add "=" (Operator "=") StrMap.empty)
let strops = "[=(),|&;]\\|->"
let rec print_expr expr = match expr with
| Keyword str :: rest -> printf "%s" ("Keyword: " ^ str ^ "\n");
print_expr rest
| Operator str :: rest -> printf "%s" ("Operator: " ^ str ^ "\n");
print_expr rest
| Id str :: rest -> printf "%s" ("Id: " ^ str ^ "\n"); print_expr rest
| Int i :: rest -> printf "%s%d%s" "Int: " i "\n"; print_expr rest
| Keyword str :: rest -> printf "Keyword: %s\n" str; print_expr rest
| Operator str :: rest -> printf "Operator: %s\n" str; print_expr rest
| Id str :: rest -> printf "Id: %s\n" str; print_expr rest
| Int i :: rest -> printf "Int: %d\n" i; print_expr rest
| [] -> printf ""
(* TODO: Add support for comments *)
let lex str =
let rec _lex_noblanks list res = match list with
| Delim d :: rest -> _lex_noblanks rest (res @ [Operator d])
| Text t :: rest ->
(try _lex_noblanks rest (res @ [Int (int_of_string t)]);
with Failure exn -> _lex_noblanks rest (res @ [Id t]))
with Failure _ -> _lex_noblanks rest (res @ [Id t]))
| [] -> res
in
let rec _lex list res = match list with
......
......@@ -15,4 +15,7 @@ let _ =
if Array.length Sys.argv - 1 <> 0 then
let str = load_file Sys.argv.(1) in
let tokens = Lexer.lex str in
Lexer.print_expr tokens
let env = [||] in
let locals = [||] in
Lexer.print_expr tokens;
Parser.parse_expr env locals tokens
open Printf
open Lexer
let rec parse_expr expr = match expr with
let rec parse_expr env locals expr = match expr with
| Keyword str :: rest -> printf "%s" ("Keyword: " ^ str ^ "\n");
parse_expr rest
parse_expr env locals rest
| Operator str :: rest -> printf "%s" ("Operator: " ^ str ^ "\n");
parse_expr rest
| Id str :: rest -> printf "%s" ("Id: " ^ str ^ "\n"); parse_expr rest
| Int i :: rest -> printf "%s%d%s" "Int: " i "\n"; parse_expr rest
parse_expr env locals rest
| Id str :: rest -> printf "%s" ("Id: " ^ str ^ "\n");
parse_expr env locals rest
| Int i :: rest -> printf "%s%d%s" "Int: " i "\n"; parse_expr env locals rest
| [] -> printf ""
val parse_expr : Lexer.token list -> unit
val parse_expr : Lambda.Env ref array -> Lambda.Local ref array ->
Lexer.token list -> Lambda.expr list
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment