Parse and transpile unary operations
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
IMPLEMENTATION MODULE Transpiler;
|
||||
|
||||
FROM FIO IMPORT WriteNBytes, WriteLine, WriteChar, WriteString;
|
||||
FROM FIO IMPORT StdErr, WriteNBytes, WriteLine, WriteChar, WriteString;
|
||||
FROM SYSTEM IMPORT ADR, ADDRESS, TSIZE;
|
||||
|
||||
FROM NumberIO IMPORT IntToStr;
|
||||
@ -9,12 +9,14 @@ FROM MemUtils IMPORT MemCopy, MemZero;
|
||||
|
||||
FROM Common IMPORT Identifier, PIdentifier, ShortString;
|
||||
FROM Lexer IMPORT Lexer, LexerToken, lexer_current, lexer_lex, LexerKind;
|
||||
FROM Parser IMPORT AstModule, PAstModule, AstTypeExpressionKind,
|
||||
FROM Parser IMPORT AstTypeExpressionKind, AstExpressionKind, AstLiteralKind, AstUnaryOperator,
|
||||
AstModule, PAstModule, AstExpression, PAstExpression, PAstLiteral,
|
||||
PAstConstantDeclaration, PPAstConstantDeclaration,
|
||||
AstTypeDeclaration, PAstTypeDeclaration, PPAstTypeDeclaration,
|
||||
PAstVariableDeclaration, PPAstVariableDeclaration, PAstImportStatement, PPAstImportStatement,
|
||||
PAstTypeExpression, PPAstTypeExpression, AstFieldDeclaration, PAstFieldDeclaration,
|
||||
parse_type_expression, parse_variable_part, parse_type_part, parse_constant_part, parse_import_part;
|
||||
parse_type_expression, parse_variable_part, parse_type_part, parse_constant_part, parse_import_part,
|
||||
parse_designator;
|
||||
|
||||
(* Calls lexer_lex() but skips the comments. *)
|
||||
PROCEDURE transpiler_lex(lexer: PLexer): LexerToken;
|
||||
@ -110,7 +112,7 @@ VAR
|
||||
token: LexerToken;
|
||||
result: PAstModule;
|
||||
BEGIN
|
||||
ALLOCATE(result, TSIZE(AstModule));
|
||||
NEW(result);
|
||||
token := transpiler_lex(context^.lexer);
|
||||
|
||||
IF token.kind = lexerKindModule THEN
|
||||
@ -383,12 +385,12 @@ BEGIN
|
||||
|
||||
RETURN result
|
||||
END transpile_procedure_heading;
|
||||
PROCEDURE transpile_expression(context: PTranspilerContext; trailing_token: LexerKind);
|
||||
PROCEDURE transpile_unchanged(context: PTranspilerContext; trailing_token: LexerKind);
|
||||
VAR
|
||||
token: LexerToken;
|
||||
written_bytes: CARDINAL;
|
||||
BEGIN
|
||||
token := transpiler_lex(context^.lexer);
|
||||
token := lexer_current(context^.lexer);
|
||||
|
||||
WHILE (token.kind <> trailing_token) AND (token.kind <> lexerKindEnd) DO
|
||||
written_bytes := 0;
|
||||
@ -412,7 +414,7 @@ BEGIN
|
||||
WriteString(context^.output, 'AND ');
|
||||
written_bytes := 1
|
||||
END;
|
||||
IF token.kind = lexerKindNot THEN
|
||||
IF token.kind = lexerKindTilde THEN
|
||||
WriteString(context^.output, 'NOT ');
|
||||
written_bytes := 1
|
||||
END;
|
||||
@ -422,13 +424,90 @@ BEGIN
|
||||
END;
|
||||
token := transpiler_lex(context^.lexer)
|
||||
END
|
||||
END transpile_unchanged;
|
||||
PROCEDURE parse_expression(lexer: PLexer): PAstExpression;
|
||||
VAR
|
||||
next_token: LexerToken;
|
||||
result: PAstExpression;
|
||||
written_bytes: CARDINAL;
|
||||
BEGIN
|
||||
result := parse_designator(lexer);
|
||||
|
||||
written_bytes := WriteNBytes(StdErr, ADDRESS(lexer^.Current - lexer^.Start), lexer^.Start);
|
||||
WriteLine(StdErr);
|
||||
|
||||
RETURN result
|
||||
END parse_expression;
|
||||
PROCEDURE transpile_unary_operator(context: PTranspilerContext; operator: AstUnaryOperator);
|
||||
BEGIN
|
||||
IF operator = astUnaryOperatorMinus THEN
|
||||
WriteChar(context^.output, '-')
|
||||
END;
|
||||
IF operator = astUnaryOperatorNot THEN
|
||||
WriteChar(context^.output, '~')
|
||||
END
|
||||
END transpile_unary_operator;
|
||||
PROCEDURE transpile_expression(context: PTranspilerContext; expression: PAstExpression);
|
||||
VAR
|
||||
literal: PAstLiteral;
|
||||
buffer: ARRAY[1..20] OF CHAR;
|
||||
written_bytes: CARDINAL;
|
||||
BEGIN
|
||||
IF expression^.kind = astExpressionKindLiteral THEN
|
||||
literal := expression^.literal;
|
||||
|
||||
IF literal^.kind = astLiteralKindInteger THEN
|
||||
IntToStr(literal^.integer, 0, buffer);
|
||||
WriteString(context^.output, buffer);
|
||||
END;
|
||||
IF literal^.kind = astLiteralKindString THEN
|
||||
WriteString(context^.output, literal^.string)
|
||||
END
|
||||
END;
|
||||
IF expression^.kind = astExpressionKindIdentifier THEN
|
||||
written_bytes := WriteNBytes(context^.output, ORD(expression^.identifier[1]), ADR(expression^.identifier[2]))
|
||||
END;
|
||||
IF expression^.kind = astExpressionKindDereference THEN
|
||||
transpile_expression(context, expression^.reference);
|
||||
WriteChar(context^.output, '^')
|
||||
END;
|
||||
IF expression^.kind = astExpressionKindArrayAccess THEN
|
||||
transpile_expression(context, expression^.array);
|
||||
WriteChar(context^.output, '[');
|
||||
transpile_expression(context, expression^.index);
|
||||
WriteChar(context^.output, ']')
|
||||
END;
|
||||
IF expression^.kind = astExpressionKindFieldAccess THEN
|
||||
transpile_expression(context, expression^.aggregate);
|
||||
WriteChar(context^.output, '.');
|
||||
written_bytes := WriteNBytes(context^.output, ORD(expression^.field[1]), ADR(expression^.field[2]));
|
||||
END;
|
||||
IF expression^.kind = astExpressionKindUnary THEN
|
||||
transpile_unary_operator(context, expression^.unary_operator);
|
||||
transpile_expression(context, expression^.unary_operand)
|
||||
END
|
||||
END transpile_expression;
|
||||
PROCEDURE transpile_if_statement(context: PTranspilerContext);
|
||||
VAR
|
||||
token: LexerToken;
|
||||
expression: PAstExpression;
|
||||
lexer: Lexer;
|
||||
BEGIN
|
||||
WriteString(context^.output, ' IF ');
|
||||
transpile_expression(context, lexerKindThen);
|
||||
|
||||
lexer := context^.lexer^;
|
||||
token := transpiler_lex(ADR(lexer));
|
||||
expression := parse_expression(ADR(lexer));
|
||||
|
||||
IF expression <> NIL THEN
|
||||
context^.lexer^ := lexer;
|
||||
transpile_expression(context, expression);
|
||||
WriteChar(context^.output, ' ')
|
||||
END;
|
||||
IF expression = NIL THEN
|
||||
token := transpiler_lex(context^.lexer)
|
||||
END;
|
||||
transpile_unchanged(context, lexerKindThen);
|
||||
|
||||
WriteString(context^.output, 'THEN');
|
||||
WriteLine(context^.output);
|
||||
@ -441,7 +520,8 @@ VAR
|
||||
token: LexerToken;
|
||||
BEGIN
|
||||
WriteString(context^.output, ' WHILE ');
|
||||
transpile_expression(context, lexerKindDo);
|
||||
token := transpiler_lex(context^.lexer);
|
||||
transpile_unchanged(context, lexerKindDo);
|
||||
|
||||
WriteString(context^.output, 'DO');
|
||||
WriteLine(context^.output);
|
||||
@ -450,9 +530,12 @@ BEGIN
|
||||
token := transpiler_lex(context^.lexer)
|
||||
END transpile_while_statement;
|
||||
PROCEDURE transpile_assignment_statement(context: PTranspilerContext);
|
||||
VAR
|
||||
token: LexerToken;
|
||||
BEGIN
|
||||
WriteString(context^.output, ' := ');
|
||||
transpile_expression(context, lexerKindSemicolon);
|
||||
token := transpiler_lex(context^.lexer);
|
||||
transpile_unchanged(context, lexerKindSemicolon);
|
||||
END transpile_assignment_statement;
|
||||
PROCEDURE transpile_call_statement(context: PTranspilerContext);
|
||||
VAR
|
||||
@ -514,7 +597,8 @@ VAR
|
||||
token: LexerToken;
|
||||
BEGIN
|
||||
WriteString(context^.output, ' RETURN ');
|
||||
transpile_expression(context, lexerKindSemicolon)
|
||||
token := transpiler_lex(context^.lexer);
|
||||
transpile_unchanged(context, lexerKindSemicolon)
|
||||
END transpile_return_statement;
|
||||
PROCEDURE transpile_statement(context: PTranspilerContext);
|
||||
VAR
|
||||
|
Reference in New Issue
Block a user