program; from FIO import Close, IsNoError, File, OpenToRead, OpenToWrite, StdErr, StdOut, WriteLine, WriteString; from SYSTEM import ADR; from M2RTS import HALT, ExitOnHalt; from Lexer import Lexer, lexer_destroy, lexer_initialize; from Parser import Parser; from Transpiler import transpile; from CommandLineInterface import PCommandLine, parse_command_line; from Parser import PAstModule, parse; from Strings import Length; var command_line: PCommandLine; proc compile_from_stream(); var lexer: Lexer; source_input: File; source_output: File; ast_module: PAstModule; begin source_input := OpenToRead(command_line^.input); if IsNoError(source_input) = false then WriteString(StdErr, 'Fatal error: failed to read the input file "'); WriteString(StdErr, command_line^.input); WriteString(StdErr, '".'); WriteLine(StdErr); ExitOnHalt(2) end; source_output := nil; if Length(command_line^.output) > 0 then source_output := OpenToWrite(command_line^.output); if IsNoError(source_output) = false then WriteString(StdErr, 'Fatal error: failed to create the output file "'); WriteString(StdErr, command_line^.output); WriteString(StdErr, '".'); WriteLine(StdErr); ExitOnHalt(2) end end; if IsNoError(source_input) then lexer_initialize(ADR(lexer), source_input); ast_module := parse(ADR(lexer)); transpile(ast_module, StdOut, source_output, command_line^.input); lexer_destroy(ADR(lexer)); Close(source_output); Close(source_input) end end; begin ExitOnHalt(0); command_line := parse_command_line(); if command_line <> nil then compile_from_stream() end; if command_line = nil then ExitOnHalt(1) end; HALT() end.