summaryrefslogtreecommitdiff
path: root/src/main.ml
blob: c64381c52deeb82274c0a0a294823dca5c1729d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
open Format
open Lexing

let parse_only = ref false
let dump = ref false

let ifile = ref ""

let set_var v s = v := s

let usage = "usage: mini-cpp [options] file.cpp"

let localisation pos =
	let l = pos.pos_lnum in
	let c = pos.pos_cnum - pos.pos_bol + 1 in
	eprintf "File \"%s\", line %d, characters %d-%d:\n"
		!ifile l (c-1) c
	
let options = [
	"--parse-only", Arg.Set parse_only, "Stops after parsing of the input file.";
	"--dump", Arg.Set dump, "Dump the AST after parsing."
	]

let localisation pos =
  let l = pos.pos_lnum in
  let c = pos.pos_cnum - pos.pos_bol + 1 in
  eprintf "File \"%s\", line %d, characters %d-%d:\n" !ifile l (c-1) c

let () =
	Arg.parse options (set_var ifile) usage;

	if !ifile = "" then (
		eprintf "No input file\n@?";
		exit 1);
	
	if not (Filename.check_suffix !ifile ".cpp") then (
		eprintf "Input files must have suffix .cpp\n@?";
		Arg.usage options usage;
		exit 1);
	
	let f = open_in !ifile in
	let buf = Lexing.from_channel f in

	try
		let p = Parser.prog Lexer.token buf in
		close_in f;
		
		if !dump then Pretty.print_prog p;
	with
		| Lexer.Lexing_error s ->
			localisation (Lexing.lexeme_start_p buf);
			eprintf "Lexical analysis error: %s@." s;
			exit 1
		| Parser.Error -> 
			localisation (Lexing.lexeme_start_p buf);
			eprintf "Parsing error.@.";
			exit 1
		| _ ->
			eprintf "Unexpected error...@.";
			exit 2