summaryrefslogtreecommitdiff
path: root/source/command_line_interface.elna
blob: 040fdeb86aa03e130f1800d1544882361959d377 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(* This Source Code Form is subject to the terms of the Mozilla Public License,
  v. 2.0. If a copy of the MPL was not distributed with this file, You can
  obtain one at https://mozilla.org/MPL/2.0/. *)

(*
  Command line handling.
*)
module;

import cstdlib, cstring, common;

type
  CommandLine* = record
    input: ^Char;
    output: ^Char;
    lex: Bool;
    parse: Bool
  end;

proc parse_command_line*(argc: Int, argv: ^^Char) -> ^CommandLine;
var
  parameter: ^Char;
  i: Int;
  result: ^CommandLine;
  parsed: Bool;
begin
  i := 1;
  result := cast(malloc(#size(CommandLine)): ^CommandLine);
  result^.lex := false;
  result^.parse := false;
  result^.input := nil;
  result^.output := nil;

  while i < argc & result <> nil do
    parameter := (argv + i)^;
    parsed := false;

    if strcmp(parameter, "--lex\0".ptr) = 0 then
      parsed := true;
      result^.lex := true
    end;
    if strcmp(parameter, "--parse\0".ptr) = 0 then
      parsed := true;
      result^.parse := true
    end;
    if strcmp(parameter, "-o\0".ptr) = 0 then
      i := i + 1;

      if i = argc then
        write_s("Fatal error: expecting a file name following -o.");
        result := nil
      end;
      if i < argc then
        parameter := (argv + i)^;
        result^.output := parameter
      end;
      parsed := true
    end;
    if (parameter^ <> '-') & ~parsed then
      parsed := true;

      if result^.input <> nil then
        write_s("Fatal error: only one source file can be compiled at once. First given \"");
        write_z(result^.input);
        write_s("\", then \"");
        write_z(parameter);
        write_s("\".\n");
        result := nil
      end;
      if result <> nil then
        result^.input := parameter
      end
    end;
    if ~parsed then
      write_s("Fatal error: unknown command line options: ");

      write_z(parameter);
      write_s(".\n");

      result := nil
    end;

    i := i + 1
  end;
  if result <> nil & result^.input = nil then
    write_s("Fatal error: no input files.\n");
    result := nil
  end;

  return result
end;

end.