Merge main() files.

This commit is contained in:
ArthurSonzogni
2021-03-15 00:41:30 +01:00
parent 95f070f953
commit 34b68f63e4
6 changed files with 131 additions and 75 deletions
+46 -19
View File
@@ -3,6 +3,39 @@ project(Diagon LANGUAGES C CXX)
include(FetchContent)
#-------------------------------------------------------------------------------
FetchContent_Declare(json
GIT_REPOSITORY https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent
GIT_TAG v3.9.1)
FetchContent_GetProperties(json)
if(NOT json_POPULATED)
FetchContent_Populate(json)
add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
#-------------------------------------------------------------------------------
FetchContent_Declare(antlr
GIT_REPOSITORY https://github.com/antlr/antlr4
GIT_TAG 4.8)
FetchContent_GetProperties(antlr)
if(NOT antlr_POPULATED)
FetchContent_Populate(antlr)
SET(WITH_LIBCXX OFF CACHE BOOL "")
add_subdirectory(
${antlr_SOURCE_DIR}/runtime/Cpp
${antlr_BINARY_DIR}
EXCLUDE_FROM_ALL)
target_include_directories(antlr4_static
PUBLIC ${antlr_SOURCE_DIR}/runtime/Cpp/runtime/src
)
endif()
#-------------------------------------------------------------------------------
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools)
function(target_set_common target)
@@ -67,6 +100,8 @@ add_subdirectory(src/translator/table)
add_subdirectory(src/translator/tree)
add_library(diagon_lib STATIC
src/api.cpp
src/api.hpp
src/translator/Factory.cpp
src/translator/Factory.h
)
@@ -84,15 +119,24 @@ target_link_libraries(diagon_lib
PRIVATE translator_tree
PRIVATE antlr4_static
PRIVATE screen
PRIVATE nlohmann_json::nlohmann_json
)
target_set_common(diagon_lib)
add_executable(diagon src/main.cpp)
target_link_libraries(diagon PRIVATE diagon_lib)
target_set_common(diagon)
if (EMSCRIPTEN)
# Emscripten options
string(APPEND CMAKE_CXX_FLAGS " -Os")
string(APPEND CMAKE_CXX_FLAGS " -s WASM=1")
string(APPEND CMAKE_CXX_FLAGS " --closure 1")
string(APPEND CMAKE_CXX_FLAGS " -s DISABLE_EXCEPTION_CATCHING=0")
string(APPEND CMAKE_CXX_FLAGS " -fexceptions")
#string(APPEND CMAKE_CXX_FLAGS " -s STANDALONE_WASM")
#string(APPEND CMAKE_CXX_FLAGS " -s WASM_ASYNC_COMPILATION=0")
#string(APPEND CMAKE_CXX_FLAGS " -s SIDE_MODULE=1")
#string(APPEND CMAKE_CXX_FLAGS " -s DISABLE_EXCEPTION_CATCHING=0")
string(APPEND CMAKE_CXX_FLAGS " -s EXPORTED_FUNCTIONS='[\"_translate\", \"_main\", \"_API\"]'")
string(APPEND CMAKE_CXX_FLAGS " -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"ccall\",\"cwrap\"]'")
@@ -113,30 +157,13 @@ if (EMSCRIPTEN)
)
endforeach(file)
add_executable(diagon src/main_webassembly.cpp)
target_link_libraries(diagon PRIVATE diagon_lib)
target_set_common(diagon)
FetchContent_Declare(json
GIT_REPOSITORY https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent
GIT_TAG v3.9.1)
FetchContent_GetProperties(json)
if(NOT json_POPULATED)
FetchContent_Populate(json)
add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
target_link_libraries(diagon PRIVATE nlohmann_json::nlohmann_json)
#set_target_properties(diagon PROPERTIES SUFFIX ".wasm")
else()
target_link_libraries(diagon_lib PRIVATE -static-libstdc++)
target_link_libraries(diagon_lib PRIVATE stdc++fs)
add_executable(diagon src/main.cpp)
target_link_libraries(diagon PRIVATE diagon_lib)
install(TARGETS diagon RUNTIME DESTINATION "bin")
target_set_common(diagon)
add_executable(input_output_test src/input_output_test.cpp)
target_link_libraries(input_output_test diagon_lib)
+2 -27
View File
@@ -1,33 +1,8 @@
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include "api.hpp"
#include <emscripten.h>
#include <iostream>
#include <nlohmann/json.hpp>
#include "util.hpp"
#include "translator/Factory.h"
extern "C" void translate(const char* translator_name,
const char* input,
const char* options) {
auto* translator = FindTranslator(translator_name);
if (!translator)
std::cerr << "Translator not found" << std::endl;
std::string command = translator->Translate(input, options);
replaceAll(command, "\\", "\\\\");
replaceAll(command, "\"", "\\\"");
replaceAll(command, "\n", "\\n");
command = "output.value=\"" + command + "\";";
emscripten_run_script(command.c_str());
}
int main(int, const char**) {
return 0;
}
using json = nlohmann::json;
template <class T>
@@ -76,7 +51,7 @@ json API(const TranslatorPtr& translator) {
extern "C" const char* API() {
static std::string out;
if (out.size() == 0)
out = API(TranslatorList()).dump();
out = API(TranslatorList()).dump(2);
return out.c_str();
}
+6
View File
@@ -0,0 +1,6 @@
#ifndef DIAGON_API_H
#define DIAGON_API_H
extern "C" const char* API();
#endif /* end of include guard: DIAGON_API_H */
+36 -6
View File
@@ -288,12 +288,15 @@ let withKatex = f => {
document.head.appendChild(katex_js);
}
let translate = function() {};
let API = function() {}
let diagon = {
translate: () => {},
API: () => {},
}
function UpdateOutput() {
errors.value = '';
translate(tools_data[tools.value].tool, input.value, GetOptions());
output.value =
diagon.translate(tools_data[tools.value].tool, input.value, GetOptions());
if (GetOptions().includes("Latex")) {
document.documentElement.setAttribute("data-display-latex", "true");
@@ -319,8 +322,8 @@ function UpdateOutput() {
}
function OnRuntimeInitialized() {
translate = Module.cwrap('translate', 'void', ['string','string', 'string']);
API = Module.cwrap('API', 'string', []);
diagon.translate = Module.cwrap('translate', 'string', ['string','string', 'string']);
diagon.API = Module.cwrap('API', 'string', []);
tools.addEventListener("input", () => {
UpdateOptions();
@@ -330,7 +333,7 @@ function OnRuntimeInitialized() {
examples.addEventListener("input", UpdateInput);
input.addEventListener('input', UpdateOutput);
let api = API();
let api = diagon.API();
tools_data = JSON.parse(api);
UpdateTools();
UpdateExamples()
@@ -435,6 +438,33 @@ copyOutput.addEventListener("click", async function() {
copyOutput.setAttribute("data-active", "false")
});
//const main = async function() {
// const wasm = fetch("diagon.wasm");
// const import_object = {
// wasi_snapshot_preview1: {
// proc_exit: (a) => { console.log("here"); return 0; },
// fd_close: (a) => { console.log("here"); return 0; },
// fd_write: (a, b, c, d) => { console.log("here"); return 0; },
// args_sizes_get: (a, b) => { console.log("here"); return 0; },
// environ_get: (a, b) => { console.log("here"); return 0; },
// environ_sizes_get: (a, b) => { console.log("here"); return 0; },
// fd_seek: (a, b, c, d) => { console.log("here"); return 0; },
// fd_read: (a, b, c, d) => { console.log("here"); return 0; },
// args_get: (a, b) => { console.log("here"); return 0; },
// },
// env: {
// memory,
// __sys_dup: (a) => { console.log("here"); return 0;},
// __sys_dup2: (a, b) => { console.log("here"); return 0; },
// },
// js: {
// mem: new WebAssembly.Memory({initial:10}),
// },
// }
// diagon = await WebAssembly.instantiateStreaming(wasm, import_object);
//}
//
//main();
</script>
<script src="diagon.js"></script>
<link
+41 -5
View File
@@ -3,9 +3,29 @@
// the LICENSE file.
#include <iostream>
#include "api.hpp"
#include "environment.h"
#include "translator/Factory.h"
#include "util.hpp"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
extern "C" const char* translate(const char* translator_name,
const char* input,
const char* options) {
auto* translator = FindTranslator(translator_name);
if (!translator)
std::cerr << "Translator not found" << std::endl;
static std::string out;
try {
out = translator->Translate(input, options);
} catch (...) {
std::cerr << "Error" << std::endl;
}
return out.c_str();
}
#endif
namespace {
@@ -254,18 +274,34 @@ int PrintTranslatorNotFound(const std::string& translator) {
return EXIT_SUCCESS;
}
int PrintAPI() {
std::cout << API() << std::endl;
return EXIT_SUCCESS;
}
} // namespace
int main(int argument_count, const char** arguments) {
if (argument_count <= 1)
return PrintHelp();
std::string argument_1 = arguments[1];
if (argument_1 == "-h" || argument_1 == "--help")
return PrintHelp();
if (argument_1 == "-v" || argument_1 == "--version")
if (argument_1 == "API")
return PrintAPI();
if (argument_1 == "help" || //
argument_1 == "-h" || //
argument_1 == "--help" //
) {
return PrintHelp();
}
if (argument_1 == "-v" || //
argument_1 == "--version" || //
argument_1 == "version" //
) {
return PrintVersion();
}
std::string translator_name = arguments[1];
auto* translator = FindTranslator(translator_name);
-18
View File
@@ -8,23 +8,6 @@ execute_process(
WORKING_DIRECTORY ${DIAGON_TOOLS_DIR}
)
FetchContent_Declare(antlr
GIT_REPOSITORY https://github.com/antlr/antlr4
GIT_TAG 4.8)
FetchContent_GetProperties(antlr)
if(NOT json_POPULATED)
FetchContent_Populate(antlr)
SET(WITH_LIBCXX OFF CACHE BOOL "")
add_subdirectory(
${antlr_SOURCE_DIR}/runtime/Cpp
${antlr_BINARY_DIR}
EXCLUDE_FROM_ALL)
target_include_directories(antlr4_static
PUBLIC ${antlr_SOURCE_DIR}/runtime/Cpp/runtime/src
)
endif()
# Function
# ANTLR(<file.g4>)
#
@@ -60,4 +43,3 @@ function(ANTLR source)
${source_src_dir}/${source_filename}.g4
)
endfunction()