Split the parser from the code generator

This commit is contained in:
2025-06-11 22:36:05 +02:00
parent 6cfeb46dbf
commit 90aa5a0030
6 changed files with 392 additions and 222 deletions

View File

@ -34,9 +34,9 @@ BEGIN
WHILE token.kind <> lexerKindEnd DO
INC(field_count);
INC(field_count);
REALLOCATE(field_declarations, TSIZE(AstFieldDeclaration) * field_count);
DEC(field_count);
INC(field_count);
REALLOCATE(field_declarations, TSIZE(AstFieldDeclaration) * field_count);
DEC(field_count);
current_field := field_declarations;
INC(current_field , TSIZE(AstFieldDeclaration) * (field_count - 1));
@ -105,7 +105,7 @@ BEGIN
result^.length := token.integerKind;
token := transpiler_lex(lexer);
token := transpiler_lex(lexer)
END;
token := transpiler_lex(lexer);
result^.base := parse_type_expression(lexer);
@ -138,8 +138,8 @@ BEGIN
REALLOCATE(result^.cases, TSIZE(Identifier) * case_count);
DEC(case_count);
current_case := result^.cases;
INC(current_case, TSIZE(Identifier) * (case_count - 1));
current_case^ := token.identifierKind;
INC(current_case, TSIZE(Identifier) * (case_count - 1));
current_case^ := token.identifierKind;
token := transpiler_lex(lexer)
END;
@ -183,7 +183,7 @@ BEGIN
REALLOCATE(result^.parameters, TSIZE(PAstTypeExpression) * parameter_count);
DEC(parameter_count);
current_parameter := result^.parameters;
INC(current_parameter, TSIZE(PAstTypeExpression) * (parameter_count - 1));
INC(current_parameter, TSIZE(PAstTypeExpression) * (parameter_count - 1));
current_parameter^ := parse_type_expression(lexer);
@ -226,10 +226,10 @@ BEGIN
END;
RETURN result
END parse_type_expression;
PROCEDURE parse_type_declaration(lexer: PLexer): PAstTypeDeclaration;
PROCEDURE parse_type_declaration(lexer: PLexer): PAstTypedDeclaration;
VAR
token: LexerToken;
result: PAstTypeDeclaration;
result: PAstTypedDeclaration;
BEGIN
token := lexer_current(lexer);
@ -244,16 +244,16 @@ BEGIN
RETURN result
END parse_type_declaration;
PROCEDURE parse_type_part(lexer: PLexer): PPAstTypeDeclaration;
PROCEDURE parse_type_part(lexer: PLexer): PPAstTypedDeclaration;
VAR
token: LexerToken;
result: PPAstTypeDeclaration;
current_declaration: PPAstTypeDeclaration;
result: PPAstTypedDeclaration;
current_declaration: PPAstTypedDeclaration;
declaration_count: CARDINAL;
BEGIN
token := lexer_current(lexer);
ALLOCATE(result, TSIZE(PAstTypeDeclaration));
ALLOCATE(result, TSIZE(PAstTypedDeclaration));
current_declaration := result;
declaration_count := 0;
@ -263,16 +263,16 @@ BEGIN
WHILE token.kind = lexerKindIdentifier DO
INC(declaration_count);
REALLOCATE(result, TSIZE(PAstTypeDeclaration) * (declaration_count + 1));
REALLOCATE(result, TSIZE(PAstTypedDeclaration) * (declaration_count + 1));
current_declaration := result;
INC(current_declaration, TSIZE(PAstTypeDeclaration) * (declaration_count - 1));
INC(current_declaration, TSIZE(PAstTypedDeclaration) * (declaration_count - 1));
current_declaration^ := parse_type_declaration(lexer);
token := transpiler_lex(lexer)
END
END;
IF declaration_count <> 0 THEN
INC(current_declaration, TSIZE(PAstTypeDeclaration))
INC(current_declaration, TSIZE(PAstTypedDeclaration))
END;
current_declaration^ := NIL;
@ -411,7 +411,7 @@ BEGIN
REALLOCATE(result^.symbols, TSIZE(Identifier) * (symbol_count + 1));
current_symbol := result^.symbols;
INC(current_symbol, TSIZE(Identifier) * (symbol_count - 1));
INC(current_symbol, TSIZE(Identifier) * (symbol_count - 1));
current_symbol^ := token.identifierKind;
token := transpiler_lex(lexer)
@ -464,18 +464,18 @@ BEGIN
NEW(literal);
literal^.kind := astLiteralKindInteger;
literal^.integer := token.integerKind;
literal^.integer := token.integerKind
END;
IF (token.kind = lexerKindCharacter) OR (token.kind = lexerKindString) THEN
NEW(literal);
literal^.kind := astLiteralKindString;
literal^.string := token.stringKind;
literal^.string := token.stringKind
END;
IF token.kind = lexerKindNull THEN
NEW(literal);
literal^.kind := astLiteralKindNull;
literal^.kind := astLiteralKindNull
END;
IF token.kind = lexerKindBoolean THEN
NEW(literal);
@ -504,7 +504,7 @@ BEGIN
NEW(result);
result^.kind := astExpressionKindLiteral;
result^.literal := literal;
result^.literal := literal
END;
IF (result = NIL) AND (next_token.kind = lexerKindMinus) THEN
NEW(result);
@ -531,10 +531,10 @@ BEGIN
END;
IF (result = NIL) AND (next_token.kind = lexerKindIdentifier) THEN
NEW(result);
result^.kind := astExpressionKindIdentifier;
result^.identifier := next_token.identifierKind;
next_token := transpiler_lex(lexer)
END;
@ -638,7 +638,7 @@ BEGIN
result^.kind := astExpressionKindBinary;
result^.binary_operator := operator;
result^.lhs := left;
result^.rhs := right;
result^.rhs := right
END;
RETURN result
@ -732,4 +732,234 @@ BEGIN
RETURN result
END parse_call_statement;
PROCEDURE parse_compound_statement(lexer: PLexer): AstCompoundStatement;
VAR
result: AstCompoundStatement;
token: LexerToken;
current_statement: PPAstStatement;
old_count: CARDINAL;
BEGIN
result.count := 0;
result.statements := NIL;
token := lexer_current(lexer);
WHILE token.kind <> lexerKindEnd DO
old_count := result.count;
INC(result.count);
REALLOCATE(result.statements, TSIZE(PAstStatement) * result.count);
current_statement := result.statements;
INC(current_statement, TSIZE(PAstStatement) * old_count);
current_statement^ := parse_statement(lexer);
token := lexer_current(lexer)
END;
RETURN result
END parse_compound_statement;
PROCEDURE parse_statement(lexer: PLexer): PAstStatement;
VAR
token: LexerToken;
statement: PAstStatement;
designator: PAstExpression;
BEGIN
statement := NIL;
token := transpiler_lex(lexer);
IF token.kind = lexerKindIf THEN
statement := parse_if_statement(lexer)
END;
IF token.kind = lexerKindWhile THEN
statement := parse_while_statement(lexer)
END;
IF token.kind = lexerKindReturn THEN
statement := parse_return_statement(lexer)
END;
IF token.kind = lexerKindIdentifier THEN
designator := parse_designator(lexer);
token := lexer_current(lexer);
IF token.kind = lexerKindAssignment THEN
statement := parse_assignment_statement(lexer, designator)
END;
IF token.kind <> lexerKindAssignment THEN
statement := parse_call_statement(lexer, designator)
END
END;
RETURN statement
END parse_statement;
PROCEDURE parse_if_statement(lexer: PLexer): PAstStatement;
VAR
token: LexerToken;
result: PAstStatement;
BEGIN
NEW(result);
result^.kind := astStatementKindIf;
token := transpiler_lex(lexer);
result^.if_condition := parse_expression(lexer);
result^.if_branch := parse_compound_statement(lexer);
token := transpiler_lex(lexer);
RETURN result
END parse_if_statement;
PROCEDURE parse_while_statement(lexer: PLexer): PAstStatement;
VAR
token: LexerToken;
result: PAstStatement;
BEGIN
NEW(result);
result^.kind := astStatementKindWhile;
token := transpiler_lex(lexer);
result^.while_condition := parse_expression(lexer);
result^.while_body := parse_compound_statement(lexer);
token := transpiler_lex(lexer);
RETURN result
END parse_while_statement;
PROCEDURE parse_statement_part(lexer: PLexer): AstCompoundStatement;
VAR
token: LexerToken;
compound: AstCompoundStatement;
BEGIN
compound.count := 0;
compound.statements := NIL;
token := lexer_current(lexer);
IF token.kind = lexerKindBegin THEN
compound := parse_compound_statement(lexer)
END;
RETURN compound
END parse_statement_part;
PROCEDURE parse_procedure_heading(lexer: PLexer): PAstProcedureDeclaration;
VAR
token: LexerToken;
declaration: PAstProcedureDeclaration;
parameter_index: CARDINAL;
current_parameter: PAstTypedDeclaration;
BEGIN
NEW(declaration);
token := transpiler_lex(lexer);
declaration^.name := token.identifierKind;
token := transpiler_lex(lexer);
declaration^.parameters := NIL;
declaration^.parameter_count := 0;
token := transpiler_lex(lexer);
WHILE token.kind <> lexerKindRightParen DO
parameter_index := declaration^.parameter_count;
INC(declaration^.parameter_count);
REALLOCATE(declaration^.parameters, TSIZE(AstTypedDeclaration) * declaration^.parameter_count);
current_parameter := declaration^.parameters;
INC(current_parameter, TSIZE(AstTypedDeclaration) * parameter_index);
current_parameter^.identifier := token.identifierKind;
token := transpiler_lex(lexer);
token := transpiler_lex(lexer);
current_parameter^.type_expression := parse_type_expression(lexer);
token := transpiler_lex(lexer);
IF token.kind = lexerKindComma THEN
token := transpiler_lex(lexer)
END
END;
token := transpiler_lex(lexer);
declaration^.return_type := NIL;
(* Check for the return type and write it. *)
IF token.kind = lexerKindArrow THEN
token := transpiler_lex(lexer);
declaration^.return_type := parse_type_expression(lexer);
token := transpiler_lex(lexer)
END;
token := transpiler_lex(lexer);
RETURN declaration
END parse_procedure_heading;
PROCEDURE parse_procedure_declaration(lexer: PLexer): PAstProcedureDeclaration;
VAR
token: LexerToken;
declaration: PAstProcedureDeclaration;
BEGIN
declaration := parse_procedure_heading(lexer);
declaration^.constants := parse_constant_part(lexer);
declaration^.variables := parse_variable_part(lexer);
declaration^.statements := parse_statement_part(lexer);
token := transpiler_lex(lexer);
token := transpiler_lex(lexer);
RETURN declaration
END parse_procedure_declaration;
PROCEDURE parse_procedure_part(lexer: PLexer): PPAstProcedureDeclaration;
VAR
token: LexerToken;
current_declaration: PPAstProcedureDeclaration;
result: PPAstProcedureDeclaration;
declaration_count: CARDINAL;
declaration_index: CARDINAL;
BEGIN
token := lexer_current(lexer);
declaration_count := 0;
declaration_index := 0;
ALLOCATE(result, TSIZE(PAstProcedureDeclaration));
WHILE token.kind = lexerKindProc DO
INC(declaration_count);
REALLOCATE(result, TSIZE(PAstProcedureDeclaration) * (declaration_count + 1));
current_declaration := result;
INC(current_declaration, TSIZE(PAstProcedureDeclaration) * declaration_index);
current_declaration^ := parse_procedure_declaration(lexer);
token := lexer_current(lexer);
declaration_index := declaration_count
END;
current_declaration := result;
INC(current_declaration, TSIZE(PAstProcedureDeclaration) * declaration_index);
current_declaration^ := NIL;
RETURN result
END parse_procedure_part;
PROCEDURE parse_module(lexer: PLexer): PAstModule;
VAR
token: LexerToken;
result: PAstModule;
BEGIN
NEW(result);
token := transpiler_lex(lexer);
result^.main := TRUE;
IF token.kind = lexerKindModule THEN
result^.main := FALSE
END;
token := transpiler_lex(lexer);
(* Write the module body. *)
token := transpiler_lex(lexer);
result^.imports := parse_import_part(lexer);
result^.constants := parse_constant_part(lexer);
result^.types := parse_type_part(lexer);
result^.variables := parse_variable_part(lexer);
result^.procedures := parse_procedure_part(lexer);
result^.statements := parse_statement_part(lexer);
token := transpiler_lex(lexer);
token := transpiler_lex(lexer);
RETURN result
END parse_module;
END Parser.