mirror of
https://github.com/LucasVbr/postscript-compiler.git
synced 2026-05-13 17:22:00 +00:00
Merge Parser and typing
This commit is contained in:
@@ -8,6 +8,7 @@ src/parser.ml
|
|||||||
*.mli
|
*.mli
|
||||||
src/**/*.cmi
|
src/**/*.cmi
|
||||||
comp
|
comp
|
||||||
|
*/**/*.output
|
||||||
|
|
||||||
# Tests out
|
# Tests out
|
||||||
tests/out/
|
tests/out/
|
||||||
|
|||||||
+8
-8
@@ -1,5 +1,5 @@
|
|||||||
# Import lib + compile
|
# Compile
|
||||||
all: lib comp
|
all: comp
|
||||||
|
|
||||||
# Compilation of Ocaml files
|
# Compilation of Ocaml files
|
||||||
# Attention: order of object files important
|
# 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
|
# ocaml lexer and parser
|
||||||
|
|
||||||
# Comment in for your own lexer
|
# Comment in for your own lexer
|
||||||
# lexer.ml: lexer.mll lang.cmo
|
lexer.ml: lexer.mll lang.cmo
|
||||||
# ocamllex $<
|
ocamllex $<
|
||||||
|
|
||||||
# Comment in for your own parser
|
# Comment in for your own parser
|
||||||
# parser.ml parser.mli: parser.mly lang.cmo
|
parser.ml parser.mli: parser.mly lang.cmo
|
||||||
# ocamlyacc $<
|
ocamlyacc $<
|
||||||
|
|
||||||
lexer.cmo: lexer.ml parser.cmo
|
lexer.cmo: lexer.ml parser.cmo
|
||||||
ocamlc -c $<
|
ocamlc -c $<
|
||||||
@@ -52,8 +52,8 @@ parser.cmo: parser.ml parser.cmi lang.cmo
|
|||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
|
|
||||||
### Import files from /lib (temporarly)
|
### Import files from /lib (temporarly)
|
||||||
lib:
|
# lib:
|
||||||
cp ../lib/* ./
|
# cp ../lib/* ./
|
||||||
|
|
||||||
## Remove compiled modules and lib
|
## Remove compiled modules and lib
|
||||||
clean:
|
clean:
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
*.output
|
|
||||||
@@ -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
|
|
||||||
@@ -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."
|
|
||||||
;;
|
|
||||||
@@ -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)
|
|
||||||
|
|
||||||
@@ -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" ;;
|
|
||||||
|
|
||||||
*)
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user