(* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. *) program; from FIO import Close, IsNoError, File, OpenToRead, OpenToWrite, StdErr, StdOut, WriteLine, WriteString; from M2RTS import HALT, ExitOnHalt; from Lexer import Lexer, lexer_destroy, lexer_initialize; from Transpiler import transpile; from CommandLineInterface import CommandLine, parse_command_line; from Parser import AstModule, parse; from Strings import Length; var command_line: ^CommandLine; proc compile_from_stream(); var lexer: Lexer; source_input: File; source_output: File; ast_module: ^AstModule; 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(@lexer, source_input); ast_module := parse(@lexer); transpile(ast_module, StdOut, source_output, command_line^.input); lexer_destroy(@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.