Merge Parser and typing

This commit is contained in:
Lucàs
2023-04-07 17:37:51 +02:00
parent e691dc72f8
commit 41cc5c6189
10 changed files with 9 additions and 168 deletions
+1
View File
@@ -8,6 +8,7 @@ src/parser.ml
*.mli
src/**/*.cmi
comp
*/**/*.output
# Tests out
tests/out/
+8 -8
View File
@@ -1,5 +1,5 @@
# Import lib + compile
all: lib comp
# Compile
all: comp
# Compilation of Ocaml files
# Attention: order of object files important
@@ -30,12 +30,12 @@ comp.cmo: comp.ml gen.cmo typing.cmo parser.cmo interf.cmo
# ocaml lexer and parser
# Comment in for your own lexer
# lexer.ml: lexer.mll lang.cmo
# ocamllex $<
lexer.ml: lexer.mll lang.cmo
ocamllex $<
# Comment in for your own parser
# parser.ml parser.mli: parser.mly lang.cmo
# ocamlyacc $<
parser.ml parser.mli: parser.mly lang.cmo
ocamlyacc $<
lexer.cmo: lexer.ml parser.cmo
ocamlc -c $<
@@ -52,8 +52,8 @@ parser.cmo: parser.ml parser.cmi lang.cmo
.PHONY: clean
### Import files from /lib (temporarly)
lib:
cp ../lib/* ./
# lib:
# cp ../lib/* ./
## Remove compiled modules and lib
clean:
-1
View File
@@ -1 +0,0 @@
*.output
-46
View File
@@ -1,46 +0,0 @@
all: lang.cmo parser.cmo lexer.cmo interf.cmo
# Compilation of .ml files
lang.cmo: lang.ml
ocamlc -c $<
# typing.cmo: typing.ml lang.cmo
# ocamlc -c $<
# instrs.cmo: instrs.ml lang.cmo
# ocamlc -c $<
# gen.cmo: gen.ml lang.cmo instrs.cmo typing.cmo
# ocamlc -c $<
#interf.cmo: interf.ml lexer.cmo parser.cmo gen.cmo typing.cmo
interf.cmo: interf.ml lexer.cmo parser.cmo
ocamlc -c $<
# comp.cmo: comp.ml gen.cmo typing.cmo parser.cmo interf.cmo
# ocamlc -c $<
# ocaml lexer and parser
lexer.ml: lexer.mll lang.cmo
ocamllex $<
parser.ml parser.mli: parser.mly lang.cmo
ocamlyacc -v $<
lexer.cmo: lexer.ml parser.cmo
ocamlc -c $<
parser.cmo: parser.ml parser.cmi lang.cmo
ocamlc -c $<
#### Generic rules
%.cmi: %.mli
ocamlc -c $<
.PHONY: clean
clean:
rm -f lexer.ml parser.ml *.mli *.cmi *.cmo
-33
View File
@@ -1,33 +0,0 @@
(* Interface with parser *)
exception ParseLexError of exn * (string * int * int * string * string)
let parse_file infile =
let lexbuf = Lexing.from_channel (open_in infile) in
try
Parser.start Lexer.token lexbuf
with exn ->
begin
let curr = lexbuf.Lexing.lex_curr_p in
let line = curr.Lexing.pos_lnum in
let cnum = curr.Lexing.pos_cnum - curr.Lexing.pos_bol in
let tok = Lexing.lexeme lexbuf in
let tail = Lexer.ruleTail "" lexbuf in
raise (ParseLexError (exn,(infile, line,cnum,tok,tail)))
end
;;
let print_parse_error (filename, line,cnum,tok,tail) =
print_string ("Parsing error in file: " ^ filename ^
" on line: " ^ (string_of_int line) ^
" column: " ^ (string_of_int cnum) ^
" token: " ^ tok ^
"\nrest: " ^ tail ^ "\n")
;;
let parse infile =
try parse_file infile
with ParseLexError (e, r) ->
print_parse_error r;
failwith "Stopped execution."
;;
-64
View File
@@ -1,64 +0,0 @@
(* Definition of source language data structures *)
(* variable names *)
type vname = string
(* function names *)
type fname = string
(* binary arithmetic operators *)
type barith = BAadd | BAsub | BAmul | BAdiv | BAmod (* integer *)
| BAfadd | BAfsub | BAfmul | BAfdiv (* float *)
(* binary boolean operators: and, or *)
type bbool = BBand | BBor
(* binary comparison operators: =, >=, >, <=, <, != *)
type bcompar = BCeq | BCge | BCgt | BCle | BClt | BCne
(* binary operators, combining all of the above *)
type binop =
BArith of barith
| BBool of bbool
| BCompar of bcompar
type value =
BoolV of bool
| FloatV of float
| IntV of int
| LitV of string
| StringV of string
(* Expresssions *)
type expr =
Const of value (* constant *)
| VarE of vname (* variable *)
| BinOp of binop * expr * expr (* binary operation *)
| CondE of expr * expr * expr (* conditional expr *)
| CallE of fname * (expr list) (* call expression *)
(* Commands *)
type com =
Skip (* no operation *)
| Exit (* exit from loop *)
| Assign of vname * expr (* assign expression to var *)
| Seq of com * com (* sequence of statements *)
| CondC of expr * com * com (* conditional com *)
| Loop of com (* loop until exit *)
| CallC of fname * (expr list) (* call statement *)
| Return of expr (* return from call *)
(* Types *)
type tp = BoolT | FloatT | IntT | LitT | StringT | VoidT
(* variable / parameter declaration *)
type vardecl = Vardecl of tp * vname
(* function declaration: return type; parameter declarations *)
type fundecl = Fundecl of tp * fname * (vardecl list)
(* function definition: function declaration; function body *)
type fundefn = Fundefn of fundecl * com
type prog = Prog of (fundecl list) * (fundefn list)
-16
View File
@@ -1,16 +0,0 @@
#load "lang.cmo";;
#load "parser.cmo" ;;
#load "lexer.cmo" ;;
#load "interf.cmo";;
open Interf;;
open Lang;;
(* For using the parser:
- Evaluate this file (use.ml)
- parse "Tests/progsimple.c" ;;
*)