summaryrefslogtreecommitdiff
path: root/caml/Main2.ml
blob: 41815758126c7c81100d1363c1ebfc34edf8a859 (plain)
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
open Printf
open Datatypes

let process_cminor_file sourcename =
  let targetname = Filename.chop_suffix sourcename ".cm" ^ ".s" in
  let ic = open_in sourcename in
  let lb = Lexing.from_channel ic in
  try
    match Main.transf_cminor_program
            (CMtypecheck.type_program
              (CMparser.prog CMlexer.token lb)) with
    | None ->
        eprintf "Compiler failure\n";
        exit 2
    | Some p ->
        let oc = open_out targetname in
        PrintPPC.print_program oc p;
        close_out oc
  with Parsing.Parse_error ->
         eprintf "File %s, character %d: Syntax error\n"
                 sourcename (Lexing.lexeme_start lb);
         exit 2
     | CMlexer.Error msg ->  
         eprintf "File %s, character %d: %s\n"
                 sourcename (Lexing.lexeme_start lb) msg;
         exit 2
     | CMtypecheck.Error msg ->
         eprintf "File %s, type-checking error:\n%s"
                 sourcename msg;
         exit 2

let process_file filename =
  if Filename.check_suffix filename ".cm" then
    process_cminor_file filename
  else
    raise (Arg.Bad ("unknown file type " ^ filename))

let _ =
  Arg.parse [] process_file
    "Usage: ccomp <options> <files>\nOptions are:"