94 lines
2.2 KiB
Plaintext
94 lines
2.2 KiB
Plaintext
(* 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/. *)
|
|
module;
|
|
|
|
from FIO import WriteString, WriteChar, WriteLine, StdErr;
|
|
import cstdlib, 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: Word;
|
|
result: ^CommandLine;
|
|
parsed: Bool;
|
|
begin
|
|
i := 1u;
|
|
NEW(result);
|
|
result^.lex := false;
|
|
result^.parse := false;
|
|
memset(@result^.input, 0, 256);
|
|
result^.output[1] := CHAR(0);
|
|
|
|
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 + 1u;
|
|
|
|
if i = argc then
|
|
WriteString(StdErr, "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[1] <> '-') & ~parsed then
|
|
parsed := true;
|
|
|
|
if result^.input <> nil then
|
|
WriteString(StdErr, "Fatal error: only one source file can be compiled at once. First given \"");
|
|
WriteString(StdErr, result^.input);
|
|
WriteString(StdErr, "\", then \"");
|
|
WriteString(StdErr, parameter);
|
|
WriteString(StdErr, "\".");
|
|
WriteLine(StdErr);
|
|
result := nil
|
|
end;
|
|
if result <> nil then
|
|
result^.input := parameter
|
|
end
|
|
end;
|
|
if ~parsed then
|
|
WriteString(StdErr, "Fatal error: unknown command line options: ");
|
|
|
|
WriteString(StdErr, parameter);
|
|
WriteChar(StdErr, '.');
|
|
WriteLine(StdErr);
|
|
|
|
result := nil
|
|
end;
|
|
|
|
i := i + 1u
|
|
end;
|
|
if result <> nil & result^.input = nil then
|
|
WriteString(StdErr, "Fatal error: no input files.");
|
|
WriteLine(StdErr);
|
|
result := nil
|
|
end;
|
|
|
|
return result
|
|
end;
|
|
|
|
end.
|