ladybird/Userland/Utilities/wasm.cpp

45 lines
1.3 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/FileStream.h>
2021-04-27 22:13:01 +04:30
#include <LibWasm/Printer/Printer.h>
#include <LibWasm/Types.h>
int main(int argc, char* argv[])
{
const char* filename = nullptr;
2021-04-27 22:13:01 +04:30
bool print = false;
Core::ArgsParser parser;
parser.add_positional_argument(filename, "File name to parse", "file");
2021-04-27 22:13:01 +04:30
parser.add_option(print, "Print the parsed module", "print", 'p');
parser.parse(argc, argv);
auto result = Core::File::open(filename, Core::OpenMode::ReadOnly);
if (result.is_error()) {
warnln("Failed to open {}: {}", filename, result.error());
return 1;
}
auto stream = Core::InputFileStream(result.release_value());
auto parse_result = Wasm::Module::parse(stream);
if (parse_result.is_error()) {
warnln("Something went wrong, either the file is invalid, or there's a bug with LibWasm!");
warnln("The parse error was {}", Wasm::parse_error_to_string(parse_result.error()));
return 2;
}
2021-04-27 22:13:01 +04:30
if (print) {
auto out_stream = Core::OutputFileStream::standard_output();
Wasm::Printer printer(out_stream);
printer.print(parse_result.value());
}
return 0;
}