Parse and transpile unary operations
This commit is contained in:
@ -58,7 +58,7 @@ PROCEDURE parse_record_type(lexer: PLexer): PAstTypeExpression;
|
||||
VAR
|
||||
result: PAstTypeExpression;
|
||||
BEGIN
|
||||
ALLOCATE(result, TSIZE(AstTypeExpression));
|
||||
NEW(result);
|
||||
result^.kind := astTypeExpressionKindRecord;
|
||||
result^.fields := parse_type_fields(lexer);
|
||||
|
||||
@ -69,7 +69,7 @@ VAR
|
||||
token: LexerToken;
|
||||
result: PAstTypeExpression;
|
||||
BEGIN
|
||||
ALLOCATE(result, TSIZE(AstTypeExpression));
|
||||
NEW(result);
|
||||
result^.kind := astTypeExpressionKindPointer;
|
||||
|
||||
token := lexer_current(lexer);
|
||||
@ -88,7 +88,7 @@ VAR
|
||||
buffer: ARRAY[1..20] OF CHAR;
|
||||
result: PAstTypeExpression;
|
||||
BEGIN
|
||||
ALLOCATE(result, TSIZE(AstTypeExpression));
|
||||
NEW(result);
|
||||
result^.kind := astTypeExpressionKindArray;
|
||||
result^.length := 0;
|
||||
|
||||
@ -116,7 +116,7 @@ VAR
|
||||
current_case: PIdentifier;
|
||||
case_count: CARDINAL;
|
||||
BEGIN
|
||||
ALLOCATE(result, TSIZE(AstTypeExpression));
|
||||
NEW(result);
|
||||
result^.kind := astTypeExpressionKindEnumeration;
|
||||
|
||||
case_count := 1;
|
||||
@ -147,10 +147,9 @@ PROCEDURE parse_named_type(lexer: PLexer): PAstTypeExpression;
|
||||
VAR
|
||||
token: LexerToken;
|
||||
result: PAstTypeExpression;
|
||||
written_bytes: CARDINAL;
|
||||
BEGIN
|
||||
token := lexer_current(lexer);
|
||||
ALLOCATE(result, TSIZE(AstTypeExpression));
|
||||
NEW(result);
|
||||
|
||||
result^.kind := astTypeExpressionKindNamed;
|
||||
result^.name := token.identifierKind;
|
||||
@ -165,7 +164,7 @@ VAR
|
||||
parameter_count: CARDINAL;
|
||||
BEGIN
|
||||
parameter_count := 0;
|
||||
ALLOCATE(result, TSIZE(AstTypeExpression));
|
||||
NEW(result);
|
||||
result^.kind := astTypeExpressionKindProcedure;
|
||||
|
||||
ALLOCATE(result^.parameters, 1);
|
||||
@ -227,7 +226,7 @@ VAR
|
||||
BEGIN
|
||||
token := lexer_current(lexer);
|
||||
|
||||
ALLOCATE(result, TSIZE(AstTypeDeclaration));
|
||||
NEW(result);
|
||||
result^.identifier := token.identifierKind;
|
||||
|
||||
token := transpiler_lex(lexer);
|
||||
@ -277,7 +276,7 @@ VAR
|
||||
token: LexerToken;
|
||||
result: PAstVariableDeclaration;
|
||||
BEGIN
|
||||
ALLOCATE(result, TSIZE(AstVariableDeclaration));
|
||||
NEW(result);
|
||||
|
||||
token := lexer_current(lexer);
|
||||
result^.variable_name := token.identifierKind;
|
||||
@ -329,7 +328,7 @@ VAR
|
||||
token: LexerToken;
|
||||
result: PAstConstantDeclaration;
|
||||
BEGIN
|
||||
ALLOCATE(result, TSIZE(AstConstantDeclaration));
|
||||
NEW(result);
|
||||
|
||||
token := lexer_current(lexer);
|
||||
result^.constant_name := token.identifierKind;
|
||||
@ -384,7 +383,7 @@ VAR
|
||||
symbol_count: CARDINAL;
|
||||
current_symbol: PIdentifier;
|
||||
BEGIN
|
||||
ALLOCATE(result, TSIZE(AstImportStatement));
|
||||
NEW(result);
|
||||
symbol_count := 1;
|
||||
|
||||
token := transpiler_lex(lexer);
|
||||
@ -446,4 +445,129 @@ BEGIN
|
||||
|
||||
RETURN result
|
||||
END parse_import_part;
|
||||
PROCEDURE parse_literal(lexer: PLexer): PAstLiteral;
|
||||
VAR
|
||||
literal: PAstLiteral;
|
||||
token: LexerToken;
|
||||
BEGIN
|
||||
literal := NIL;
|
||||
token := lexer_current(lexer);
|
||||
|
||||
IF token.kind = lexerKindInteger THEN
|
||||
NEW(literal);
|
||||
|
||||
literal^.kind := astLiteralKindInteger;
|
||||
literal^.integer := token.integerKind;
|
||||
END;
|
||||
IF token.kind = lexerKindCharacter THEN
|
||||
NEW(literal);
|
||||
|
||||
literal^.kind := astLiteralKindString;
|
||||
literal^.string := token.stringKind;
|
||||
END;
|
||||
IF token.kind = lexerKindNull THEN
|
||||
NEW(literal);
|
||||
|
||||
literal^.kind := astLiteralKindNull;
|
||||
END;
|
||||
IF literal <> NIL THEN
|
||||
token := transpiler_lex(lexer)
|
||||
END;
|
||||
|
||||
RETURN literal
|
||||
END parse_literal;
|
||||
PROCEDURE parse_factor(lexer: PLexer): PAstExpression;
|
||||
VAR
|
||||
next_token: LexerToken;
|
||||
result: PAstExpression;
|
||||
literal: PAstLiteral;
|
||||
BEGIN
|
||||
result := NIL;
|
||||
next_token := lexer_current(lexer);
|
||||
|
||||
literal := parse_literal(lexer);
|
||||
|
||||
IF (result = NIL) AND (literal <> NIL) THEN
|
||||
NEW(result);
|
||||
|
||||
result^.kind := astExpressionKindLiteral;
|
||||
result^.literal := literal;
|
||||
END;
|
||||
IF (result = NIL) AND (next_token.kind = lexerKindMinus) THEN
|
||||
NEW(result);
|
||||
next_token := transpiler_lex(lexer);
|
||||
|
||||
result^.kind := astExpressionKindUnary;
|
||||
result^.unary_operator := astUnaryOperatorMinus;
|
||||
result^.unary_operand := parse_factor(lexer)
|
||||
END;
|
||||
IF (result = NIL) AND (next_token.kind = lexerKindTilde) THEN
|
||||
NEW(result);
|
||||
next_token := transpiler_lex(lexer);
|
||||
|
||||
result^.kind := astExpressionKindUnary;
|
||||
result^.unary_operator := astUnaryOperatorNot;
|
||||
result^.unary_operand := parse_factor(lexer)
|
||||
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;
|
||||
|
||||
RETURN result
|
||||
END parse_factor;
|
||||
PROCEDURE parse_designator(lexer: PLexer): PAstExpression;
|
||||
VAR
|
||||
next_token: LexerToken;
|
||||
inner_expression: PAstExpression;
|
||||
designator: PAstExpression;
|
||||
handled: BOOLEAN;
|
||||
BEGIN
|
||||
designator := parse_factor(lexer);
|
||||
handled := designator <> NIL;
|
||||
next_token := lexer_current(lexer);
|
||||
|
||||
WHILE handled DO
|
||||
inner_expression := designator;
|
||||
handled := FALSE;
|
||||
|
||||
IF ~handled AND (next_token.kind = lexerKindHat) THEN
|
||||
NEW(designator);
|
||||
|
||||
designator^.kind := astExpressionKindDereference;
|
||||
designator^.reference := inner_expression;
|
||||
|
||||
next_token := transpiler_lex(lexer);
|
||||
handled := TRUE
|
||||
END;
|
||||
IF ~handled AND (next_token.kind = lexerKindLeftSquare) THEN
|
||||
NEW(designator);
|
||||
next_token := transpiler_lex(lexer);
|
||||
|
||||
designator^.kind := astExpressionKindArrayAccess;
|
||||
designator^.array := inner_expression;
|
||||
designator^.index := parse_designator(lexer);
|
||||
|
||||
next_token := transpiler_lex(lexer);
|
||||
handled := TRUE
|
||||
END;
|
||||
IF ~handled AND (next_token.kind = lexerKindDot) THEN
|
||||
NEW(designator);
|
||||
next_token := transpiler_lex(lexer);
|
||||
|
||||
designator^.kind := astExpressionKindFieldAccess;
|
||||
designator^.aggregate := inner_expression;
|
||||
designator^.field := next_token.identifierKind;
|
||||
|
||||
next_token := transpiler_lex(lexer);
|
||||
handled := TRUE
|
||||
END
|
||||
END;
|
||||
|
||||
RETURN designator
|
||||
END parse_designator;
|
||||
END Parser.
|
||||
|
Reference in New Issue
Block a user