Add A command line parsing procedure
This commit is contained in:
3
source/CommandLine.def
Normal file
3
source/CommandLine.def
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
DEFINITION MODULE CommandLine;
|
||||||
|
|
||||||
|
END CommandLine.
|
3
source/CommandLine.mod
Normal file
3
source/CommandLine.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
MODULE CommandLine;
|
||||||
|
|
||||||
|
END CommandLine.
|
15
source/CommandLineInterface.def
Normal file
15
source/CommandLineInterface.def
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
DEFINITION MODULE CommandLineInterface;
|
||||||
|
|
||||||
|
FROM Common IMPORT ShortString;
|
||||||
|
|
||||||
|
TYPE
|
||||||
|
CommandLine = RECORD
|
||||||
|
input: ShortString;
|
||||||
|
lex: BOOLEAN;
|
||||||
|
parse: BOOLEAN
|
||||||
|
END;
|
||||||
|
PCommandLine = POINTER TO CommandLine;
|
||||||
|
|
||||||
|
PROCEDURE parse_command_line(): PCommandLine;
|
||||||
|
|
||||||
|
END CommandLineInterface.
|
74
source/CommandLineInterface.mod
Normal file
74
source/CommandLineInterface.mod
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
IMPLEMENTATION MODULE CommandLineInterface;
|
||||||
|
|
||||||
|
FROM SYSTEM IMPORT ADR, TSIZE;
|
||||||
|
|
||||||
|
FROM Args IMPORT GetArg, Narg;
|
||||||
|
FROM FIO IMPORT WriteString, WriteChar, WriteLine, StdErr;
|
||||||
|
FROM Storage IMPORT ALLOCATE;
|
||||||
|
FROM Strings IMPORT CompareStr, Length;
|
||||||
|
FROM MemUtils IMPORT MemZero;
|
||||||
|
|
||||||
|
FROM Common IMPORT ShortString;
|
||||||
|
|
||||||
|
PROCEDURE parse_command_line(): PCommandLine;
|
||||||
|
VAR
|
||||||
|
parameter: ShortString;
|
||||||
|
i: CARDINAL;
|
||||||
|
result: PCommandLine;
|
||||||
|
parsed: BOOLEAN;
|
||||||
|
BEGIN
|
||||||
|
i := 1;
|
||||||
|
ALLOCATE(result, TSIZE(CommandLine));
|
||||||
|
result^.lex := FALSE;
|
||||||
|
result^.parse := FALSE;
|
||||||
|
MemZero(ADR(result^.input), 256);
|
||||||
|
|
||||||
|
WHILE (i < Narg()) AND (result <> NIL) DO
|
||||||
|
parsed := GetArg(parameter, i);
|
||||||
|
parsed := FALSE;
|
||||||
|
|
||||||
|
IF CompareStr(parameter, '--lex') = 0 THEN
|
||||||
|
parsed := TRUE;
|
||||||
|
result^.lex := TRUE
|
||||||
|
END;
|
||||||
|
IF CompareStr(parameter, '--parse') = 0 THEN
|
||||||
|
parsed := TRUE;
|
||||||
|
result^.parse := TRUE
|
||||||
|
END;
|
||||||
|
IF parameter[0] <> '-' THEN
|
||||||
|
parsed := TRUE;
|
||||||
|
|
||||||
|
IF Length(result^.input) > 0 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 = FALSE THEN
|
||||||
|
WriteString(StdErr, 'Fatal error: unknown command line options: ');
|
||||||
|
|
||||||
|
WriteString(StdErr, parameter);
|
||||||
|
WriteChar(StdErr, '.');
|
||||||
|
WriteLine(StdErr);
|
||||||
|
|
||||||
|
result := NIL
|
||||||
|
END;
|
||||||
|
|
||||||
|
i := i + 1
|
||||||
|
END;
|
||||||
|
IF (result <> NIL) AND (Length(result^.input) = 0) THEN
|
||||||
|
WriteString(StdErr, 'Fatal error: no input files.');
|
||||||
|
WriteLine(StdErr);
|
||||||
|
result := NIL
|
||||||
|
END;
|
||||||
|
|
||||||
|
RETURN result
|
||||||
|
END parse_command_line;
|
||||||
|
END CommandLineInterface.
|
6
source/Common.def
Normal file
6
source/Common.def
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
DEFINITION MODULE Common;
|
||||||
|
|
||||||
|
TYPE
|
||||||
|
ShortString = ARRAY[0..255] OF CHAR;
|
||||||
|
|
||||||
|
END Common.
|
3
source/Common.mod
Normal file
3
source/Common.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
IMPLEMENTATION MODULE Common;
|
||||||
|
|
||||||
|
END Common.
|
@ -5,11 +5,15 @@ FROM SYSTEM IMPORT ADR;
|
|||||||
|
|
||||||
FROM Lexer IMPORT Lexer, lexer_destroy, lexer_initialize;
|
FROM Lexer IMPORT Lexer, lexer_destroy, lexer_initialize;
|
||||||
FROM Transpiler IMPORT transpile;
|
FROM Transpiler IMPORT transpile;
|
||||||
|
FROM CommandLineInterface IMPORT PCommandLine, parse_command_line;
|
||||||
|
|
||||||
VAR
|
VAR
|
||||||
lexer: Lexer;
|
lexer: Lexer;
|
||||||
|
command_line: PCommandLine;
|
||||||
|
|
||||||
BEGIN
|
BEGIN
|
||||||
|
command_line := parse_command_line();
|
||||||
|
|
||||||
lexer_initialize(ADR(lexer), StdIn);
|
lexer_initialize(ADR(lexer), StdIn);
|
||||||
|
|
||||||
transpile(ADR(lexer));
|
transpile(ADR(lexer));
|
||||||
|
3
source/Parser.def
Normal file
3
source/Parser.def
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
DEFINITION MODULE Parser;
|
||||||
|
|
||||||
|
END Parser.
|
3
source/Parser.mod
Normal file
3
source/Parser.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
MODULE Parser;
|
||||||
|
|
||||||
|
END Parser.
|
@ -421,6 +421,14 @@ BEGIN
|
|||||||
WriteString('NIL ');
|
WriteString('NIL ');
|
||||||
written_bytes := 1
|
written_bytes := 1
|
||||||
END;
|
END;
|
||||||
|
IF (token.kind = lexerKindBoolean) AND token.booleanKind THEN
|
||||||
|
WriteString('TRUE ');
|
||||||
|
written_bytes := 1
|
||||||
|
END;
|
||||||
|
IF (token.kind = lexerKindBoolean) AND (~token.booleanKind) THEN
|
||||||
|
WriteString('FALSE ');
|
||||||
|
written_bytes := 1
|
||||||
|
END;
|
||||||
IF token.kind = lexerKindOr THEN
|
IF token.kind = lexerKindOr THEN
|
||||||
WriteString('OR ');
|
WriteString('OR ');
|
||||||
written_bytes := 1
|
written_bytes := 1
|
||||||
|
Reference in New Issue
Block a user