Skip to content

Commit 5b2ab8c

Browse files
committed
Add empty compiler
1 parent 8ee8f68 commit 5b2ab8c

File tree

10 files changed

+151
-7
lines changed

10 files changed

+151
-7
lines changed

CMakeLists.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@ cmake_minimum_required(VERSION 3.27)
22
project(LightStorm)
33

44
set(CMAKE_CXX_STANDARD 20)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
set(CMAKE_CXX_EXTENSIONS OFF)
57

6-
add_executable(LightStorm main.cpp)
8+
include(cmake/mruby.cmake)
9+
10+
find_package(LLVM CONFIG REQUIRED)
11+
message("Using LLVM_CONFIG: ${LLVM_CONFIG}")
12+
13+
add_subdirectory(lib)
14+
add_subdirectory(tools)

cmake/mruby.cmake

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
include(ExternalProject)
2+
set (MRUBY_DIR "${CMAKE_SOURCE_DIR}/third_party/mruby")
3+
set (MRUBY_BINARY "${MRUBY_DIR}/bin/mruby")
4+
set (MRUBY_STATIC "${MRUBY_DIR}/build/host/lib/libmruby.a")
5+
6+
ExternalProject_Add(mruby
7+
SOURCE_DIR ${MRUBY_DIR}
8+
CONFIGURE_COMMAND ""
9+
BUILD_COMMAND ${CMAKE_COMMAND} -E env rake all --verbose
10+
BUILD_IN_SOURCE ON
11+
INSTALL_COMMAND ""
12+
BUILD_BYPRODUCTS ${MRUBY_STATIC} ${MRUBY_BINARY}
13+
)
14+
15+
add_executable(mruby_binary IMPORTED GLOBAL)
16+
set_property(TARGET mruby_binary PROPERTY IMPORTED_LOCATION ${MRUBY_BINARY})
17+
add_dependencies(mruby_binary mruby)
18+
19+
add_library(mruby_static STATIC IMPORTED GLOBAL)
20+
set_property(TARGET mruby_static PROPERTY IMPORTED_LOCATION ${MRUBY_STATIC})
21+
add_dependencies(mruby_static mruby)
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <filesystem>
4+
5+
namespace lightstorm {
6+
7+
class Compiler {
8+
public:
9+
void compileSourceFile(const std::filesystem::path &file_path);
10+
11+
private:
12+
};
13+
14+
} // namespace lightstorm

lib/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(compiler)

lib/compiler/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
add_library(lightstorm_compiler
2+
compiler.cpp
3+
)
4+
target_include_directories(lightstorm_compiler
5+
PRIVATE
6+
${CMAKE_SOURCE_DIR}/third_party/mruby/include
7+
${CMAKE_SOURCE_DIR}/include
8+
)
9+
target_link_libraries(lightstorm_compiler PRIVATE mruby_static)
10+
target_compile_options(lightstorm_compiler PRIVATE -fno-rtti -fno-exceptions)

lib/compiler/compiler.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "lightstorm/compiler/compiler.h"
2+
#include <cassert>
3+
#include <iostream>
4+
#include <mruby.h>
5+
#include <mruby/compile.h>
6+
7+
using namespace lightstorm;
8+
9+
extern "C" {
10+
void mrb_codedump_all(mrb_state *, struct RProc *);
11+
}
12+
13+
void Compiler::compileSourceFile(const std::filesystem::path &file_path) {
14+
assert(exists(file_path) && "Cannot compile file");
15+
mrb_state *mrb = mrb_open();
16+
assert(mrb && "Out of memory?");
17+
mrbc_context *mrbc = mrbc_context_new(mrb);
18+
assert(mrbc && "Out of memory?");
19+
mrbc_filename(mrb, mrbc, file_path.c_str());
20+
mrbc->capture_errors = TRUE;
21+
mrbc->no_optimize = TRUE;
22+
23+
FILE *f = fopen(file_path.c_str(), "r");
24+
assert(f && "No file?");
25+
26+
struct mrb_parser_state *p = mrb_parse_file(mrb, f, mrbc);
27+
28+
if (!p) {
29+
/* only occur when memory ran out */
30+
std::cerr << "Failed to create parser state (out of memory)\n";
31+
fclose(f);
32+
mrbc_context_free(mrb, mrbc);
33+
mrb_close(mrb);
34+
return;
35+
}
36+
37+
if (0 < p->nerr) {
38+
/* parse error */
39+
std::cerr << file_path.c_str() << ":" << p->error_buffer[0].lineno << ": "
40+
<< p->error_buffer[0].message << "\n";
41+
fclose(f);
42+
mrb_parser_free(p);
43+
mrbc_context_free(mrb, mrbc);
44+
mrb_close(mrb);
45+
return;
46+
}
47+
48+
struct RProc *proc = mrb_generate_code(mrb, p);
49+
mrb_codedump_all(mrb, proc);
50+
51+
fclose(f);
52+
mrb_parser_free(p);
53+
mrbc_context_free(mrb, mrbc);
54+
mrb_close(mrb);
55+
}

main.cpp

-6
This file was deleted.

tools/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(lightstorm)

tools/lightstorm/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_executable(lightstorm lightstorm.cpp)
2+
target_link_libraries(lightstorm PRIVATE lightstorm_compiler LLVMOption)
3+
target_compile_options(lightstorm PRIVATE -fno-rtti -fno-exceptions)
4+
target_include_directories(lightstorm
5+
PRIVATE
6+
${CMAKE_SOURCE_DIR}/include
7+
${LLVM_INCLUDE_DIRS}
8+
)

tools/lightstorm/lightstorm.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <filesystem>
2+
#include <llvm/Support/CommandLine.h>
3+
#include <llvm/Support/FileSystem.h>
4+
5+
#include "lightstorm/compiler/compiler.h"
6+
7+
llvm::cl::OptionCategory LightstormCategory("lightstorm");
8+
9+
llvm::cl::opt<std::string> Input(llvm::cl::Positional, llvm::cl::Required,
10+
llvm::cl::desc("Input file"), llvm::cl::cat(LightstormCategory));
11+
12+
int main(int argc, char **argv) {
13+
llvm::llvm_shutdown_obj shutdownGuard;
14+
15+
llvm::cl::HideUnrelatedOptions(LightstormCategory);
16+
llvm::cl::ParseCommandLineOptions(argc, argv);
17+
18+
std::error_code ec;
19+
if (!std::filesystem::exists(Input.getValue(), ec)) {
20+
llvm::errs() << "Input file doesn't exist: ";
21+
if (ec) {
22+
llvm::errs() << ec.message();
23+
}
24+
llvm::errs() << "\n";
25+
return 1;
26+
}
27+
28+
lightstorm::Compiler compiler;
29+
compiler.compileSourceFile(Input.getValue());
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)