Implement defer
This commit is contained in:
246
source.elna
246
source.elna
@ -1,91 +1,92 @@
|
||||
const
|
||||
SEEK_SET = 0; SEEK_CUR = 1; SEEK_END = 2;
|
||||
SEEK_SET* = 0; SEEK_CUR* = 1; SEEK_END* = 2;
|
||||
|
||||
TOKEN_IDENTIFIER = 1; TOKEN_IF = 2; TOKEN_THEN = 3; TOKEN_ELSE = 4; TOKEN_ELSIF = 5;
|
||||
TOKEN_WHILE = 6; TOKEN_DO = 7; TOKEN_PROC = 8; TOKEN_BEGIN = 9; TOKEN_END = 10;
|
||||
TOKEN_EXTERN = 11; TOKEN_CONST = 12; TOKEN_VAR = 13; TOKEN_ARRAY = 14; TOKEN_OF = 15;
|
||||
TOKEN_TYPE = 16; TOKEN_RECORD = 17; TOKEN_UNION = 18; TOKEN_POINTER = 19; TOKEN_TO = 20;
|
||||
TOKEN_BOOLEAN = 21; TOKEN_NIL = 22; TOKEN_AND = 23; TOKEN_OR = 24; TOKEN_NOT = 25;
|
||||
TOKEN_RETURN = 26; TOKEN_CAST = 27; TOKEN_AS = 28; TOKEN_SIZEOF = 29;
|
||||
TOKEN_LEFT_PAREN = 30; TOKEN_RIGHT_PAREN = 31; TOKEN_LEFT_SQUARE = 32;
|
||||
TOKEN_RIGHT_SQUARE = 33; TOKEN_GREATER_EQUAL = 34; TOKEN_LESS_EQUAL = 35;
|
||||
TOKEN_GREATER_THAN = 36; TOKEN_LESS_THAN = 37; TOKEN_NOT_EQUAL = 38; TOKEN_EQUAL = 39;
|
||||
TOKEN_SEMICOLON = 40; TOKEN_DOT = 41; TOKEN_COMMA = 42;
|
||||
TOKEN_PLUS = 43; TOKEN_MINUS = 44; TOKEN_MULTIPLICATION = 45; TOKEN_DIVISION = 46;
|
||||
TOKEN_REMAINDER = 47; TOKEN_ASSIGNMENT = 48; TOKEN_COLON = 49; TOKEN_HAT = 50;
|
||||
TOKEN_AT = 51; TOKEN_COMMENT = 52; TOKEN_INTEGER = 53; TOKEN_WORD = 54;
|
||||
TOKEN_CHARACTER = 55; TOKEN_STRING = 56;
|
||||
TOKEN_IDENTIFIER* = 1; TOKEN_IF* = 2; TOKEN_THEN* = 3; TOKEN_ELSE* = 4; TOKEN_ELSIF* = 5;
|
||||
TOKEN_WHILE* = 6; TOKEN_DO* = 7; TOKEN_PROC* = 8; TOKEN_BEGIN* = 9; TOKEN_END* = 10;
|
||||
TOKEN_EXTERN* = 11; TOKEN_CONST* = 12; TOKEN_VAR* = 13; TOKEN_ARRAY* = 14; TOKEN_OF* = 15;
|
||||
TOKEN_TYPE* = 16; TOKEN_RECORD* = 17; TOKEN_UNION* = 18; TOKEN_POINTER* = 19; TOKEN_TO* = 20;
|
||||
TOKEN_BOOLEAN* = 21; TOKEN_NIL* = 22; TOKEN_AND* = 23; TOKEN_OR* = 24; TOKEN_NOT* = 25;
|
||||
TOKEN_RETURN* = 26; TOKEN_CAST* = 27; TOKEN_AS* = 28; TOKEN_SIZEOF* = 29;
|
||||
TOKEN_LEFT_PAREN* = 30; TOKEN_RIGHT_PAREN* = 31; TOKEN_LEFT_SQUARE* = 32;
|
||||
TOKEN_RIGHT_SQUARE* = 33; TOKEN_GREATER_EQUAL* = 34; TOKEN_LESS_EQUAL* = 35;
|
||||
TOKEN_GREATER_THAN* = 36; TOKEN_LESS_THAN* = 37; TOKEN_NOT_EQUAL* = 38; TOKEN_EQUAL* = 39;
|
||||
TOKEN_SEMICOLON* = 40; TOKEN_DOT* = 41; TOKEN_COMMA* = 42;
|
||||
TOKEN_PLUS* = 43; TOKEN_MINUS* = 44; TOKEN_MULTIPLICATION* = 45; TOKEN_DIVISION* = 46;
|
||||
TOKEN_REMAINDER* = 47; TOKEN_ASSIGNMENT* = 48; TOKEN_COLON* = 49; TOKEN_HAT* = 50;
|
||||
TOKEN_AT* = 51; TOKEN_COMMENT* = 52; TOKEN_INTEGER* = 53; TOKEN_WORD* = 54;
|
||||
TOKEN_CHARACTER* = 55; TOKEN_STRING* = 56; TOKEN_DEFER* = 57;
|
||||
|
||||
type
|
||||
Position = record
|
||||
Position* = record
|
||||
line: Word;
|
||||
column: Word
|
||||
end,
|
||||
Location = record
|
||||
Location* = record
|
||||
first: Position;
|
||||
last: Position
|
||||
end,
|
||||
TokenValue = union
|
||||
TokenValue* = union
|
||||
int_value: Int;
|
||||
string_value: pointer to Char;
|
||||
boolean_value: Bool;
|
||||
char_value: Char
|
||||
end,
|
||||
Token = record
|
||||
Token* = record
|
||||
kind: Int;
|
||||
value: TokenValue;
|
||||
location: Location
|
||||
end,
|
||||
FILE = record
|
||||
FILE* = record
|
||||
dummy: Int
|
||||
end,
|
||||
CommandLine = record
|
||||
CommandLine* = record
|
||||
input: pointer to Char;
|
||||
tokenize: Bool
|
||||
tokenize: Bool;
|
||||
syntax_tree: Bool
|
||||
end,
|
||||
Literal = record
|
||||
Literal* = record
|
||||
value: Int
|
||||
end,
|
||||
ConstantDefinition = record
|
||||
ConstantDefinition* = record
|
||||
name: pointer to Char;
|
||||
body: pointer to Literal
|
||||
end,
|
||||
ConstantPart = record
|
||||
ConstantPart* = record
|
||||
elements: pointer to pointer to ConstantDefinition;
|
||||
count: Word
|
||||
end,
|
||||
Program = record
|
||||
Program* = record
|
||||
constants: ConstantPart
|
||||
end;
|
||||
|
||||
(*
|
||||
External procedures.
|
||||
*)
|
||||
proc fopen(pathname: String, mode: String): pointer to FILE; extern;
|
||||
proc fclose(stream: pointer to FILE): Int; extern;
|
||||
proc fseek(stream: pointer to FILE, off: Int, whence: Int): Int; extern;
|
||||
proc rewind(stream: pointer to FILE); extern;
|
||||
proc ftell(stream: pointer to FILE): Int; extern;
|
||||
proc fread(ptr: pointer to Byte, size: Word, nmemb: Word, stream: pointer to FILE): Word; extern;
|
||||
proc write(fd: Int, buf: pointer to Byte, Word: Int): Int; extern;
|
||||
proc fopen(pathname: pointer to Char, mode: pointer to Char): pointer to FILE; extern
|
||||
proc fclose(stream: pointer to FILE): Int; extern
|
||||
proc fseek(stream: pointer to FILE, off: Int, whence: Int): Int; extern
|
||||
proc rewind(stream: pointer to FILE); extern
|
||||
proc ftell(stream: pointer to FILE): Int; extern
|
||||
proc fread(ptr: pointer to Byte, size: Word, nmemb: Word, stream: pointer to FILE): Word; extern
|
||||
proc write(fd: Int, buf: pointer to Byte, Word: Int): Int; extern
|
||||
|
||||
proc malloc(size: Word): pointer to Byte; extern;
|
||||
proc free(ptr: pointer to Byte); extern;
|
||||
proc calloc(nmemb: Word, size: Word): pointer to Byte; extern;
|
||||
proc realloc(ptr: pointer to Byte, size: Word): pointer to Byte; extern;
|
||||
proc malloc(size: Word): pointer to Byte; extern
|
||||
proc free(ptr: pointer to Byte); extern
|
||||
proc calloc(nmemb: Word, size: Word): pointer to Byte; extern
|
||||
proc realloc(ptr: pointer to Byte, size: Word): pointer to Byte; extern
|
||||
|
||||
proc memset(ptr: pointer to Char, c: Int, n: Int): pointer to Char; extern;
|
||||
proc memset(ptr: pointer to Char, c: Int, n: Int): pointer to Char; extern
|
||||
|
||||
proc strcmp(s1: pointer to Char, s2: pointer to Char): Int; extern;
|
||||
proc strncmp(s1: pointer to Char, s2: pointer to Char, n: Word): Int; extern;
|
||||
proc strncpy(dst: pointer to Char, src: pointer to Char, dsize: Word): pointer to Char; extern;
|
||||
proc strcpy(dst: pointer to Char, src: pointer to Char): pointer to Char; extern;
|
||||
proc strlen(ptr: pointer to Char): Word; extern;
|
||||
proc strcmp(s1: pointer to Char, s2: pointer to Char): Int; extern
|
||||
proc strncmp(s1: pointer to Char, s2: pointer to Char, n: Word): Int; extern
|
||||
proc strncpy(dst: pointer to Char, src: pointer to Char, dsize: Word): pointer to Char; extern
|
||||
proc strcpy(dst: pointer to Char, src: pointer to Char): pointer to Char; extern
|
||||
proc strlen(ptr: pointer to Char): Word; extern
|
||||
|
||||
proc strtol(nptr: pointer to Char, endptr: pointer to pointer to Char, base: Int): Int; extern;
|
||||
proc strtol(nptr: pointer to Char, endptr: pointer to pointer to Char, base: Int): Int; extern
|
||||
|
||||
proc perror(s: pointer to Char); extern;
|
||||
proc exit(code: Int); extern;
|
||||
proc perror(s: pointer to Char); extern
|
||||
proc exit(code: Int); extern
|
||||
|
||||
(*
|
||||
Standard procedures.
|
||||
@ -93,12 +94,17 @@ proc exit(code: Int); extern;
|
||||
proc reallocarray(ptr: pointer to Byte, n: Word, size: Word): pointer to Byte;
|
||||
begin
|
||||
return realloc(ptr, n * size)
|
||||
end;
|
||||
end
|
||||
|
||||
proc write_s(value: String);
|
||||
begin
|
||||
write(0, value.ptr, value.length)
|
||||
end
|
||||
|
||||
proc write_z(value: pointer to Char);
|
||||
begin
|
||||
write(0, value, strlen(value))
|
||||
end;
|
||||
end
|
||||
|
||||
proc write_b(value: Bool);
|
||||
begin
|
||||
@ -107,12 +113,12 @@ begin
|
||||
else
|
||||
write_s("false")
|
||||
end
|
||||
end;
|
||||
end
|
||||
|
||||
proc write_c(value: Char);
|
||||
begin
|
||||
write(0, @value, 1)
|
||||
end;
|
||||
end
|
||||
|
||||
proc write_i(value: Int);
|
||||
var
|
||||
@ -136,44 +142,54 @@ begin
|
||||
n := n + 1;
|
||||
write_c(buffer[n])
|
||||
end
|
||||
end;
|
||||
end
|
||||
|
||||
proc write_u(value: Word);
|
||||
begin
|
||||
write_i(value)
|
||||
end;
|
||||
end
|
||||
|
||||
proc is_digit(c: Char): Bool;
|
||||
begin
|
||||
return cast(c as Int) >= cast('0' as Int) and cast(c as Int) <= cast('9' as Int)
|
||||
end;
|
||||
end
|
||||
|
||||
proc is_alpha(c: Char): Bool;
|
||||
begin
|
||||
return cast(c as Int) >= cast('A' as Int) and cast(c as Int) <= cast('z' as Int)
|
||||
end;
|
||||
end
|
||||
|
||||
proc is_alnum(c: Char): Bool;
|
||||
begin
|
||||
return is_digit(c) or is_alpha(c)
|
||||
end;
|
||||
end
|
||||
|
||||
proc is_space(c: Char): Bool;
|
||||
begin
|
||||
return c = ' ' or c = '\n' or c = '\t'
|
||||
end;
|
||||
end
|
||||
|
||||
proc string_equals_chars(this: String, that: pointer to Char, length: Word): Bool;
|
||||
var
|
||||
i: Word;
|
||||
begin
|
||||
if this.length <> length then
|
||||
return false
|
||||
end;
|
||||
return strncmp(this.ptr, that, length) = 0
|
||||
end
|
||||
|
||||
(*
|
||||
End of standard procedures.
|
||||
*)
|
||||
|
||||
proc read_source(filename: String): pointer to Char;
|
||||
proc read_source(filename: pointer to Char): pointer to Char;
|
||||
var
|
||||
input_file: pointer to FILE,
|
||||
source_size: Int,
|
||||
input: pointer to Byte;
|
||||
begin
|
||||
input_file := fopen(filename, "rb");
|
||||
input_file := fopen(filename, "rb\0".ptr);
|
||||
|
||||
if input_file = nil then
|
||||
return nil
|
||||
@ -196,7 +212,7 @@ begin
|
||||
fclose(input_file);
|
||||
|
||||
return input
|
||||
end;
|
||||
end
|
||||
|
||||
proc escape_char(escape: Char, result: pointer to Char): Bool;
|
||||
begin
|
||||
@ -239,7 +255,7 @@ begin
|
||||
else
|
||||
return false
|
||||
end
|
||||
end;
|
||||
end
|
||||
|
||||
proc skip_spaces(input: pointer to Char): pointer to Char;
|
||||
begin
|
||||
@ -247,7 +263,7 @@ begin
|
||||
input := input + 1
|
||||
end;
|
||||
return input
|
||||
end;
|
||||
end
|
||||
|
||||
proc lex_identifier(input: pointer to Char): pointer to Char;
|
||||
begin
|
||||
@ -255,7 +271,7 @@ begin
|
||||
input := input + 1
|
||||
end;
|
||||
return input
|
||||
end;
|
||||
end
|
||||
|
||||
proc lex_comment(input: pointer to Char): pointer to Char;
|
||||
var
|
||||
@ -270,7 +286,7 @@ begin
|
||||
input := next
|
||||
end;
|
||||
return nil
|
||||
end;
|
||||
end
|
||||
|
||||
proc lex_character(input: pointer to Char, current_token: pointer to Token): pointer to Char;
|
||||
begin
|
||||
@ -284,7 +300,7 @@ begin
|
||||
input := input + 1
|
||||
end;
|
||||
return input
|
||||
end;
|
||||
end
|
||||
|
||||
proc lex_string(input: pointer to Char, current_token: pointer to Token): pointer to Char;
|
||||
var
|
||||
@ -324,7 +340,7 @@ begin
|
||||
end;
|
||||
|
||||
return token_end
|
||||
end;
|
||||
end
|
||||
|
||||
proc print_tokens(tokens: pointer to Token, tokens_size: Word);
|
||||
var
|
||||
@ -395,7 +411,7 @@ begin
|
||||
write_s("SIZEOF")
|
||||
elsif current_token^.kind = TOKEN_IDENTIFIER then
|
||||
write_c('<');
|
||||
write_s(current_token^.value.string_value);
|
||||
write_z(current_token^.value.string_value);
|
||||
write_c('>')
|
||||
elsif current_token^.kind = TOKEN_LEFT_PAREN then
|
||||
write_s("(")
|
||||
@ -457,6 +473,8 @@ begin
|
||||
write_s("c>")
|
||||
elsif current_token^.kind = TOKEN_STRING then
|
||||
write_s("\"...\"")
|
||||
elsif current_token^.kind = TOKEN_DEFER then
|
||||
write_s("DEFER")
|
||||
else
|
||||
write_s("UNKNOWN<");
|
||||
write_i(current_token^.kind);
|
||||
@ -467,72 +485,74 @@ begin
|
||||
i := i + 1u
|
||||
end;
|
||||
write_c('\n')
|
||||
end;
|
||||
end
|
||||
|
||||
proc categorize_identifier(input_pointer: pointer to Char, token_length: Int): Token;
|
||||
var
|
||||
current_token: Token;
|
||||
begin
|
||||
if strncmp("if", input_pointer, token_length) = 0 then
|
||||
if string_equals_chars("if", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_IF
|
||||
elsif strncmp("then", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("then", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_THEN
|
||||
elsif strncmp("else", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("else", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_ELSE
|
||||
elsif strncmp("elsif", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("elsif", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_ELSIF
|
||||
elsif strncmp("while", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("while", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_WHILE
|
||||
elsif strncmp("do", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("do", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_DO
|
||||
elsif strncmp("proc", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("proc", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_PROC
|
||||
elsif strncmp("begin", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("begin", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_BEGIN
|
||||
elsif strncmp("end", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("end", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_END
|
||||
elsif strncmp("extern", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("extern", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_EXTERN
|
||||
elsif strncmp("const", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("const", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_CONST
|
||||
elsif strncmp("var", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("var", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_VAR
|
||||
elsif strncmp("array", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("array", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_ARRAY
|
||||
elsif strncmp("of", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("of", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_OF
|
||||
elsif strncmp("type", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("type", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_TYPE
|
||||
elsif strncmp("record", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("record", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_RECORD
|
||||
elsif strncmp("union", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("union", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_UNION
|
||||
elsif strncmp("pointer", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("pointer", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_POINTER
|
||||
elsif strncmp("to", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("to", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_TO
|
||||
elsif strncmp("true", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("true", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_BOOLEAN;
|
||||
current_token.value.boolean_value := true
|
||||
elsif strncmp("false", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("false", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_BOOLEAN;
|
||||
current_token.value.boolean_value := false
|
||||
elsif strncmp("nil", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("nil", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_NIL
|
||||
elsif strncmp("and", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("and", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_AND
|
||||
elsif strncmp("or", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("or", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_OR
|
||||
elsif strncmp("not", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("not", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_NOT
|
||||
elsif strncmp("return", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("return", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_RETURN
|
||||
elsif strncmp("cast", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("cast", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_CAST
|
||||
elsif strncmp("as", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("as", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_AS
|
||||
elsif strncmp("sizeof", input_pointer, token_length) = 0 then
|
||||
elsif string_equals_chars("sizeof", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_SIZEOF
|
||||
elsif string_equals_chars("defer", input_pointer, token_length) then
|
||||
current_token.kind := TOKEN_DEFER
|
||||
else
|
||||
current_token.kind := TOKEN_IDENTIFIER;
|
||||
current_token.value.string_value := cast(calloc(token_length + 1, 1) as pointer to Char);
|
||||
@ -540,7 +560,7 @@ begin
|
||||
end;
|
||||
|
||||
return current_token
|
||||
end;
|
||||
end
|
||||
|
||||
proc tokenize(input_pointer: pointer to Char, tokens_size: pointer to Word): pointer to Token;
|
||||
var
|
||||
@ -695,12 +715,12 @@ begin
|
||||
end;
|
||||
|
||||
return tokens
|
||||
end;
|
||||
end
|
||||
|
||||
proc parse_literal(tokens: pointer to pointer to Token, tokens_size: pointer to Word): pointer to Literal;
|
||||
begin
|
||||
return cast(calloc(1, sizeof(Literal)) as pointer to Literal)
|
||||
end;
|
||||
end
|
||||
|
||||
proc parse_constant_definition(tokens: pointer to pointer to Token,
|
||||
tokens_size: pointer to Word): pointer to ConstantDefinition;
|
||||
@ -715,7 +735,7 @@ begin
|
||||
tokens^ := tokens^ + 2u;
|
||||
tokens_size := tokens_size - 2u;
|
||||
|
||||
write_s(result^.name);
|
||||
write_z(result^.name);
|
||||
write_c('\n');
|
||||
|
||||
result^.body := parse_literal(tokens, tokens_size);
|
||||
@ -724,7 +744,7 @@ begin
|
||||
tokens_size := tokens_size - 2u;
|
||||
|
||||
return result
|
||||
end;
|
||||
end
|
||||
|
||||
proc parse_program(tokens: pointer to pointer to Token, tokens_size: pointer to Word): pointer to Program;
|
||||
var
|
||||
@ -754,9 +774,9 @@ begin
|
||||
end
|
||||
end
|
||||
end
|
||||
end;
|
||||
end
|
||||
|
||||
proc parse_command_line(argc: Int, argv: pointer to pointer to Char): pointer to CommandLine;
|
||||
proc parse_command_line*(argc: Int, argv: pointer to pointer to Char): pointer to CommandLine;
|
||||
var
|
||||
parameter: pointer to pointer to Char,
|
||||
i: Int,
|
||||
@ -765,20 +785,23 @@ begin
|
||||
i := 1;
|
||||
result := cast(malloc(sizeof(CommandLine)) as pointer to CommandLine);
|
||||
result^.tokenize := false;
|
||||
result^.syntax_tree := false;
|
||||
result^.input := nil;
|
||||
|
||||
while i < argc do
|
||||
parameter := argv + i;
|
||||
|
||||
if strcmp(parameter^, "--tokenize") = 0 then
|
||||
if strcmp(parameter^, "--tokenize\0".ptr) = 0 then
|
||||
result^.tokenize := true
|
||||
elsif strcmp(parameter^, "--syntax-tree\0".ptr) = 0 then
|
||||
result^.syntax_tree := true
|
||||
elsif parameter^^ <> '-' then
|
||||
result^.input := parameter^
|
||||
else
|
||||
write_s("Fatal error: Unknown command line options:");
|
||||
|
||||
write_c(' ');
|
||||
write_s(parameter^);
|
||||
write_z(parameter^);
|
||||
write_s(".\n");
|
||||
|
||||
return nil
|
||||
@ -792,7 +815,7 @@ begin
|
||||
end;
|
||||
|
||||
return result
|
||||
end;
|
||||
end
|
||||
|
||||
proc process(argc: Int, argv: pointer to pointer to Char): Int;
|
||||
var
|
||||
@ -802,10 +825,10 @@ var
|
||||
command_line: pointer to CommandLine;
|
||||
begin
|
||||
command_line := parse_command_line(argc, argv);
|
||||
|
||||
if cast(command_line as Word) = 0u then
|
||||
if command_line = nil then
|
||||
return 2
|
||||
end;
|
||||
|
||||
input := read_source(command_line^.input);
|
||||
if input = nil then
|
||||
perror(command_line^.input);
|
||||
@ -816,10 +839,11 @@ begin
|
||||
if command_line^.tokenize then
|
||||
print_tokens(tokens, tokens_size)
|
||||
end;
|
||||
|
||||
parse_program(@tokens, @tokens_size);
|
||||
if command_line^.syntax_tree then
|
||||
parse_program(@tokens, @tokens_size)
|
||||
end;
|
||||
return 0
|
||||
end;
|
||||
end
|
||||
|
||||
begin
|
||||
exit(process(count, parameters))
|
||||
|
Reference in New Issue
Block a user