Parse call expressions
This commit is contained in:
@ -9,14 +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 AstTypeExpressionKind, AstExpressionKind, AstLiteralKind, AstUnaryOperator,
|
||||
AstModule, PAstModule, AstExpression, PAstExpression, PAstLiteral,
|
||||
PAstConstantDeclaration, PPAstConstantDeclaration,
|
||||
from Parser import AstTypeExpressionKind, AstExpressionKind, AstLiteralKind, AstUnaryOperator, AstBinaryOperator,
|
||||
AstModule, PAstModule, AstExpression, PPAstExpression, PAstExpression, PAstLiteral,
|
||||
PAstConstantDeclaration, PPAstConstantDeclaration, PAstStatement, AstStatementKind,
|
||||
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_designator;
|
||||
parse_designator, parse_expression, parse_return_statement, parse_assignment_statement, parse_call_statement;
|
||||
|
||||
(* Calls lexer_lex() but skips the comments. *)
|
||||
proc transpiler_lex(lexer: PLexer) -> LexerToken;
|
||||
@ -42,8 +42,12 @@ end;
|
||||
proc write_current(lexer: PLexer, output: File);
|
||||
var
|
||||
written_bytes: CARDINAL;
|
||||
count: CARDINAL;
|
||||
begin
|
||||
written_bytes := WriteNBytes(output, ADDRESS(lexer^.Current - lexer^.Start), lexer^.Start)
|
||||
count := lexer^.current;
|
||||
DEC(count, lexer^.start);
|
||||
|
||||
written_bytes := WriteNBytes(output, count, lexer^.start)
|
||||
end;
|
||||
|
||||
proc transpile_import_statement(context: PTranspilerContext, import_statement: PAstImportStatement);
|
||||
@ -406,61 +410,6 @@ begin
|
||||
return result
|
||||
end;
|
||||
|
||||
proc transpile_unchanged(context: PTranspilerContext, trailing_token: LexerKind);
|
||||
var
|
||||
token: LexerToken;
|
||||
written_bytes: CARDINAL;
|
||||
begin
|
||||
token := lexer_current(context^.lexer);
|
||||
|
||||
while (token.kind <> trailing_token) & (token.kind <> lexerKindEnd) do
|
||||
written_bytes := 0;
|
||||
if token.kind = lexerKindNull then
|
||||
WriteString(context^.output, 'NIL ');
|
||||
written_bytes := 1
|
||||
end;
|
||||
if (token.kind = lexerKindBoolean) & token.booleanKind then
|
||||
WriteString(context^.output, 'TRUE ');
|
||||
written_bytes := 1
|
||||
end;
|
||||
if (token.kind = lexerKindBoolean) & (~token.booleanKind) then
|
||||
WriteString(context^.output, 'FALSE ');
|
||||
written_bytes := 1
|
||||
end;
|
||||
if token.kind = lexerKindOr then
|
||||
WriteString(context^.output, 'OR ');
|
||||
written_bytes := 1
|
||||
end;
|
||||
if token.kind = lexerKindAnd then
|
||||
WriteString(context^.output, 'AND ');
|
||||
written_bytes := 1
|
||||
end;
|
||||
if token.kind = lexerKindTilde then
|
||||
WriteString(context^.output, 'NOT ');
|
||||
written_bytes := 1
|
||||
end;
|
||||
if written_bytes = 0 then
|
||||
write_current(context^.lexer, context^.output);
|
||||
WriteChar(context^.output, ' ')
|
||||
end;
|
||||
token := transpiler_lex(context^.lexer)
|
||||
end
|
||||
end;
|
||||
|
||||
proc 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;
|
||||
|
||||
proc transpile_unary_operator(context: PTranspilerContext, operator: AstUnaryOperator);
|
||||
begin
|
||||
if operator = astUnaryOperatorMinus then
|
||||
@ -471,11 +420,50 @@ begin
|
||||
end
|
||||
end;
|
||||
|
||||
proc transpile_binary_operator(context: PTranspilerContext, operator: AstBinaryOperator);
|
||||
begin
|
||||
if operator = astBinaryOperatorSum then
|
||||
WriteChar(context^.output, '+')
|
||||
end;
|
||||
if operator = astBinaryOperatorSubtraction then
|
||||
WriteChar(context^.output, '-')
|
||||
end;
|
||||
if operator = astBinaryOperatorMultiplication then
|
||||
WriteChar(context^.output, '*')
|
||||
end;
|
||||
if operator = astBinaryOperatorEquals then
|
||||
WriteChar(context^.output, '=')
|
||||
end;
|
||||
if operator = astBinaryOperatorNotEquals then
|
||||
WriteChar(context^.output, '#')
|
||||
end;
|
||||
if operator = astBinaryOperatorLess then
|
||||
WriteChar(context^.output, '<')
|
||||
end;
|
||||
if operator = astBinaryOperatorGreater then
|
||||
WriteChar(context^.output, '>')
|
||||
end;
|
||||
if operator = astBinaryOperatorLessEqual then
|
||||
WriteString(context^.output, '<=')
|
||||
end;
|
||||
if operator = astBinaryOperatorGreaterEqual then
|
||||
WriteString(context^.output, '>=')
|
||||
end;
|
||||
if operator = astBinaryOperatorDisjunction then
|
||||
WriteString(context^.output, 'OR')
|
||||
end;
|
||||
if operator = astBinaryOperatorConjunction then
|
||||
WriteString(context^.output, 'AND')
|
||||
end
|
||||
end;
|
||||
|
||||
proc transpile_expression(context: PTranspilerContext, expression: PAstExpression);
|
||||
var
|
||||
literal: PAstLiteral;
|
||||
buffer: [20]CHAR;
|
||||
written_bytes: CARDINAL;
|
||||
argument_index: CARDINAL;
|
||||
current_argument: PPAstExpression;
|
||||
begin
|
||||
if expression^.kind = astExpressionKindLiteral then
|
||||
literal := expression^.literal;
|
||||
@ -486,7 +474,16 @@ begin
|
||||
end;
|
||||
if literal^.kind = astLiteralKindString then
|
||||
WriteString(context^.output, literal^.string)
|
||||
end
|
||||
end;
|
||||
if literal^.kind = astLiteralKindNull then
|
||||
WriteString(context^.output, 'NIL')
|
||||
end;
|
||||
if (literal^.kind = astLiteralKindBoolean) & literal^.boolean then
|
||||
WriteString(context^.output, 'TRUE')
|
||||
end;
|
||||
if (literal^.kind = astLiteralKindBoolean) & (literal^.boolean = false) then
|
||||
WriteString(context^.output, 'FALSE')
|
||||
end
|
||||
end;
|
||||
if expression^.kind = astExpressionKindIdentifier then
|
||||
written_bytes := WriteNBytes(context^.output, ORD(expression^.identifier[1]), ADR(expression^.identifier[2]))
|
||||
@ -509,152 +506,135 @@ begin
|
||||
if expression^.kind = astExpressionKindUnary then
|
||||
transpile_unary_operator(context, expression^.unary_operator);
|
||||
transpile_expression(context, expression^.unary_operand)
|
||||
end;
|
||||
if expression^.kind = astExpressionKindBinary then
|
||||
WriteChar(context^.output, '(');
|
||||
transpile_expression(context, expression^.lhs);
|
||||
WriteChar(context^.output, ' ');
|
||||
transpile_binary_operator(context, expression^.binary_operator);
|
||||
WriteChar(context^.output, ' ');
|
||||
transpile_expression(context, expression^.rhs);
|
||||
WriteChar(context^.output, ')')
|
||||
end;
|
||||
if expression^.kind = astExpressionKindCall then
|
||||
transpile_expression(context, expression^.callable);
|
||||
WriteChar(context^.output, '(');
|
||||
|
||||
current_argument := expression^.arguments;
|
||||
if expression^.argument_count > 0 then
|
||||
transpile_expression(context, current_argument^);
|
||||
|
||||
argument_index := 1;
|
||||
INC(current_argument, TSIZE(PAstExpression));
|
||||
|
||||
while argument_index < expression^.argument_count do
|
||||
WriteString(context^.output, ', ');
|
||||
|
||||
transpile_expression(context, current_argument^);
|
||||
|
||||
INC(current_argument, TSIZE(PAstExpression));
|
||||
INC(argument_index)
|
||||
end
|
||||
end;
|
||||
WriteChar(context^.output, ')')
|
||||
end
|
||||
end;
|
||||
|
||||
proc transpile_if_statement(context: PTranspilerContext);
|
||||
proc transpile_if_statement(context: PTranspilerContext) -> PAstStatement;
|
||||
var
|
||||
token: LexerToken;
|
||||
expression: PAstExpression;
|
||||
lexer: Lexer;
|
||||
result: PAstStatement;
|
||||
begin
|
||||
NEW(result);
|
||||
result^.kind := astStatementKindIf;
|
||||
WriteString(context^.output, ' IF ');
|
||||
|
||||
lexer := context^.lexer^;
|
||||
token := transpiler_lex(ADR(lexer));
|
||||
expression := parse_expression(ADR(lexer));
|
||||
token := transpiler_lex(context^.lexer);
|
||||
result^.if_condition := parse_expression(context^.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);
|
||||
transpile_expression(context, result^.if_condition);
|
||||
token := lexer_current(context^.lexer);
|
||||
|
||||
WriteString(context^.output, 'THEN');
|
||||
WriteString(context^.output, ' THEN');
|
||||
WriteLine(context^.output);
|
||||
transpile_statements(context);
|
||||
WriteString(context^.output, ' END');
|
||||
token := transpiler_lex(context^.lexer)
|
||||
token := transpiler_lex(context^.lexer);
|
||||
|
||||
return result
|
||||
end;
|
||||
|
||||
proc transpile_while_statement(context: PTranspilerContext);
|
||||
proc transpile_while_statement(context: PTranspilerContext) -> PAstStatement;
|
||||
var
|
||||
token: LexerToken;
|
||||
result: PAstStatement;
|
||||
begin
|
||||
NEW(result);
|
||||
result^.kind := astStatementKindWhile;
|
||||
WriteString(context^.output, ' WHILE ');
|
||||
token := transpiler_lex(context^.lexer);
|
||||
transpile_unchanged(context, lexerKindDo);
|
||||
|
||||
WriteString(context^.output, 'DO');
|
||||
token := transpiler_lex(context^.lexer);
|
||||
result^.while_condition := parse_expression(context^.lexer);
|
||||
|
||||
transpile_expression(context, result^.while_condition);
|
||||
token := lexer_current(context^.lexer);
|
||||
|
||||
WriteString(context^.output, ' DO');
|
||||
WriteLine(context^.output);
|
||||
transpile_statements(context);
|
||||
WriteString(context^.output, ' END');
|
||||
token := transpiler_lex(context^.lexer)
|
||||
token := transpiler_lex(context^.lexer);
|
||||
|
||||
return result
|
||||
end;
|
||||
|
||||
proc transpile_assignment_statement(context: PTranspilerContext);
|
||||
var
|
||||
token: LexerToken;
|
||||
proc transpile_assignment_statement(context: PTranspilerContext, statement: PAstStatement);
|
||||
begin
|
||||
transpile_expression(context, statement^.assignee);
|
||||
WriteString(context^.output, ' := ');
|
||||
token := transpiler_lex(context^.lexer);
|
||||
transpile_unchanged(context, lexerKindSemicolon);
|
||||
transpile_expression(context, statement^.assignment)
|
||||
end;
|
||||
|
||||
proc transpile_call_statement(context: PTranspilerContext);
|
||||
var
|
||||
token: LexerToken;
|
||||
begin
|
||||
WriteString(context^.output, '(');
|
||||
token := transpiler_lex(context^.lexer);
|
||||
|
||||
while (token.kind <> lexerKindSemicolon) & (token.kind <> lexerKindEnd) do
|
||||
write_current(context^.lexer, context^.output);
|
||||
token := transpiler_lex(context^.lexer)
|
||||
end
|
||||
end;
|
||||
|
||||
proc transpile_designator_expression(context: PTranspilerContext);
|
||||
var
|
||||
token: LexerToken;
|
||||
begin
|
||||
WriteString(context^.output, ' ');
|
||||
write_current(context^.lexer, context^.output);
|
||||
token := transpiler_lex(context^.lexer);
|
||||
|
||||
while token.kind = lexerKindLeftSquare do
|
||||
WriteChar(context^.output, '[');
|
||||
token := transpiler_lex(context^.lexer);
|
||||
while token.kind <> lexerKindRightSquare do
|
||||
write_current(context^.lexer, context^.output);
|
||||
token := transpiler_lex(context^.lexer)
|
||||
end;
|
||||
WriteChar(context^.output, ']');
|
||||
token := transpiler_lex(context^.lexer)
|
||||
end;
|
||||
if token.kind = lexerKindHat then
|
||||
WriteChar(context^.output, '^');
|
||||
token := transpiler_lex(context^.lexer)
|
||||
end;
|
||||
if token.kind = lexerKindDot then
|
||||
WriteChar(context^.output, '.');
|
||||
token := transpiler_lex(context^.lexer);
|
||||
write_current(context^.lexer, context^.output);
|
||||
token := transpiler_lex(context^.lexer)
|
||||
end;
|
||||
if token.kind = lexerKindHat then
|
||||
WriteChar(context^.output, '^');
|
||||
token := transpiler_lex(context^.lexer)
|
||||
end;
|
||||
while token.kind = lexerKindLeftSquare do
|
||||
WriteChar(context^.output, '[');
|
||||
token := transpiler_lex(context^.lexer);
|
||||
while token.kind <> lexerKindRightSquare do
|
||||
write_current(context^.lexer, context^.output);
|
||||
token := transpiler_lex(context^.lexer)
|
||||
end;
|
||||
WriteChar(context^.output, ']');
|
||||
token := transpiler_lex(context^.lexer)
|
||||
end
|
||||
end;
|
||||
|
||||
proc transpile_return_statement(context: PTranspilerContext);
|
||||
var
|
||||
token: LexerToken;
|
||||
proc transpile_return_statement(context: PTranspilerContext, statement: PAstStatement);
|
||||
begin
|
||||
WriteString(context^.output, ' RETURN ');
|
||||
token := transpiler_lex(context^.lexer);
|
||||
transpile_unchanged(context, lexerKindSemicolon)
|
||||
|
||||
transpile_expression(context, statement^.returned);
|
||||
end;
|
||||
|
||||
proc transpile_statement(context: PTranspilerContext);
|
||||
var
|
||||
token: LexerToken;
|
||||
written_bytes: CARDINAL;
|
||||
statement: PAstStatement;
|
||||
designator: PAstExpression;
|
||||
begin
|
||||
token := transpiler_lex(context^.lexer);
|
||||
|
||||
if token.kind = lexerKindIf then
|
||||
transpile_if_statement(context)
|
||||
statement := transpile_if_statement(context)
|
||||
end;
|
||||
if token.kind = lexerKindWhile then
|
||||
transpile_while_statement(context)
|
||||
statement := transpile_while_statement(context)
|
||||
end;
|
||||
if token.kind = lexerKindReturn then
|
||||
transpile_return_statement(context)
|
||||
statement := parse_return_statement(context^.lexer);
|
||||
transpile_return_statement(context, statement)
|
||||
end;
|
||||
if token.kind = lexerKindIdentifier then
|
||||
transpile_designator_expression(context);
|
||||
designator := parse_designator(context^.lexer);
|
||||
token := lexer_current(context^.lexer);
|
||||
|
||||
if token.kind = lexerKindAssignment then
|
||||
transpile_assignment_statement(context)
|
||||
statement := parse_assignment_statement(context^.lexer, designator);
|
||||
transpile_assignment_statement(context, statement)
|
||||
end;
|
||||
if token.kind = lexerKindLeftParen then
|
||||
transpile_call_statement(context)
|
||||
if token.kind <> lexerKindAssignment then
|
||||
statement := parse_call_statement(context^.lexer, designator);
|
||||
transpile_expression(context, designator);
|
||||
|
||||
written_bytes := WriteNBytes(StdErr, 5, context^.lexer^.start);
|
||||
WriteLine(StdErr);
|
||||
end
|
||||
end
|
||||
end;
|
||||
|
Reference in New Issue
Block a user