Skip to content

Commit

Permalink
增加选项
Browse files Browse the repository at this point in the history
  • Loading branch information
wangziwenhk committed Sep 27, 2024
1 parent 838c9d6 commit 9ceaae5
Show file tree
Hide file tree
Showing 16 changed files with 501 additions and 429 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
mkdir build && cd build
cmake .. -DANTLR4_INSTALL=ON -G "Ninja" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
ninja
sudo make install
sudo ninja install
- name: Configure CMake
run: |
Expand Down
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ find_path(ANTLR4_INCLUDE_DIR antlr4-runtime/antlr4-runtime.h

find_package(LLVM REQUIRED CONFIG)
find_package(antlr4-runtime CONFIG REQUIRED)
find_path(TERMCOLOR_INCLUDE_DIRS "termcolor/termcolor.hpp")

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "Found Antlr4 include ${ANTLR4_INCLUDE_DIR}")

include_directories(${TERMCOLOR_INCLUDE_DIRS})

include_directories(src)
include_directories(${ANTLR4_INCLUDE_DIR})
include_directories(${LLVM_INCLUDE_DIRS})
Expand All @@ -47,4 +50,5 @@ target_compile_definitions(Riddle_Language PRIVATE ${PLATFORM_DEFINES})

target_link_libraries(Riddle_Language LLVMCore)
target_link_libraries(Riddle_Language antlr4_shared)
target_link_libraries(Riddle_Language Riddle_Module)
target_link_libraries(Riddle_Language Riddle_Module)

740 changes: 361 additions & 379 deletions parser/RiddleParser.cpp

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions parser/RiddleParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1157,10 +1157,7 @@ class RiddleParser : public antlr4::Parser {
public:
IdContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<antlr4::tree::TerminalNode *> Identifier();
antlr4::tree::TerminalNode* Identifier(size_t i);
std::vector<antlr4::tree::TerminalNode *> Dot();
antlr4::tree::TerminalNode* Dot(size_t i);
antlr4::tree::TerminalNode *Identifier();

virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParser.interp

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Tools/BuildQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ namespace Riddle {
// 目前只做了main的解析
void BuildQueue::parserFile(std::string filePath) {
std::ifstream stream(filePath);
auto input = new antlr4::ANTLRInputStream(stream);
auto lexer = new RiddleLexer(input);
auto tokens = new antlr4::CommonTokenStream(lexer);
const auto input = new antlr4::ANTLRInputStream(stream);
const auto lexer = new RiddleLexer(input);
const auto tokens = new antlr4::CommonTokenStream(lexer);
auto *parser = new RiddleParser(tokens);
antlr4::tree::ParseTree *p = parser->program();
PackageVisitor visitor(filePath, p);
Expand Down
8 changes: 7 additions & 1 deletion src/Tools/Files.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "Files.h"
module;
#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>
module Tools.Files;
namespace fs = std::filesystem;
namespace Riddle {
std::vector<std::string> Files::getTreeFile(const std::string &path) {// NOLINT(*-no-recursion)
Expand Down Expand Up @@ -75,6 +77,10 @@ namespace Riddle {
// 如果文件大小为0,则文件为空
return fileSize == 0;
}
std::string Files::getFileName(const std::string &path){
const fs::path file_path(path);
return file_path.filename().string();
}
std::vector<std::string> Files::getFiles(const std::string &path) {
std::vector<std::string> files;
try {
Expand Down
26 changes: 0 additions & 26 deletions src/Tools/Files.h

This file was deleted.

42 changes: 42 additions & 0 deletions src/Tools/Files.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module;
#include <string>
#include <vector>
export module Tools.Files;
export namespace Riddle {
class Files {
//我们应该增加一个缓存吗
public:
/// @brief 递归获取某个路径下的所有文件
/// @param path 路径
/// @return 递归路径下的所有文件
static std::vector<std::string> getTreeFile(const std::string &path);
/// @brief 递归获取某个路经下的所有源文件
/// @param path 路径
/// @return 递归路径下的所有源文件
static std::vector<std::string> getTreeSource(const std::string &path);
/// @brief 获取某个路径下的所有文件
/// @param path 路径
/// @return 路径下的所有文件
static std::vector<std::string> getFiles(const std::string &path);
/// @brief 获取某个路径下的所有源文件
/// @param path 路径
/// @return 路径下的所有源文件
static std::vector<std::string> getSources(const std::string &path);
/// @brief 读取某个文件中的所有内容
/// @param path 文件路径
/// @return 文件内容
static std::vector<std::string> getFileTextLine(const std::string &path);
/// @brief 获取某个文件的第一行
/// @param path 文件路径
/// @return 文件第一行
static std::string getFileFirstLine(const std::string &path);
/// @brief 检查某个文件是否为空
/// @param path 文件路径
/// @return 文件是否为空
static bool isFileEmpty(const std::string &path);
/// @brief 获取文件名称,也就是去除路径
/// @param path 文件路径
/// @return 文件名称
static std::string getFileName(const std::string &path);
};
}// namespace Riddle
1 change: 0 additions & 1 deletion src/Tools/GenTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import Types.ClassNode;
#include <antlr4-runtime.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Instructions.h>
#include <string>
#include <variant>

Expand Down
10 changes: 4 additions & 6 deletions src/Tools/Linker.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#include "Linker.h"
#include "Files.h"
import Tools.Files;
#include <filesystem>
#include <regex>
namespace fs = std::filesystem;

std::string getLibName(std::string PackageName) {
auto pos = std::find(PackageName.rbegin(), PackageName.rend(), '.');
const auto pos = std::find(PackageName.rbegin(), PackageName.rend(), '.');
return {pos.base(), PackageName.end()};
}

namespace Riddle {
Linker::Linker() {
}
Linker::Linker() = default;
std::string Linker::findSourceLib(const std::string &libPackName, const std::string &sourcePath) {
const std::string libName = getLibName(libPackName);
const auto files = Files::getTreeSource(sourcePath);
Expand All @@ -35,8 +34,7 @@ namespace Riddle {
return "UNKNOWN";
}
std::string Linker::findLib(const std::string &libPackName, const std::string &sourcePath) {
std::string sp = findSourceLib(libPackName, sourcePath);
if(sp != "UNKNOWN") return sp;
if(std::string sp = findSourceLib(libPackName, sourcePath); sp != "UNKNOWN") return sp;
return "UNKNOWN";
}
}// namespace Riddle
4 changes: 2 additions & 2 deletions src/Tools/Linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ namespace Riddle {
/// @param libPackName 需要寻找的库的包名
/// @param sourcePath 主动导入该库的源文件的绝对路径
/// @return 返回库源文件的绝对路径
std::string findSourceLib(const std::string &libPackName, const std::string &sourcePath);
static std::string findSourceLib(const std::string &libPackName, const std::string &sourcePath);

public:
Linker();
/// @brief 从多个路径寻找库
/// @param libPackName 需要寻找的库的包名
/// @param sourcePath 主动导入该库的源文件的绝对路径
/// @return 返回库源文件的绝对路径
std::string findLib(const std::string &libPackName, const std::string &sourcePath);
static std::string findLib(const std::string &libPackName, const std::string &sourcePath);
};
static Linker linker;
}// namespace Riddle
Expand Down
56 changes: 56 additions & 0 deletions src/Tools/Options.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module;
#include <stdexcept>
#include <string>
#include <vector>
export module Tools.Options;
import Tools.Files;
export namespace Riddle {
class Options {
public:
std::string source;
std::string output;
bool isMultiThread = false;
Options() = default;
explicit Options(std::vector<std::string>&args) {
if (args.empty()) {
return;
}
// 解析 args
bool isExpect = false;
std::string pre;
for (const auto& arg : args) {
if(isExpect) {
if(pre == "--source" || pre == "-s") {
source = arg;
pre = "";
}
else if (pre == "--output" || pre == "-o") {
output = arg;
pre = "";
}
isExpect = false;
}
if(arg[0]=='-') {
isExpect = true;
}
if(arg == "-m" || arg == "--multi-thread") {
isMultiThread = true;
isExpect = false;
pre = "";
}
if(source.empty()) {
source = arg;
}
pre = arg;

}
// 自动补全
if(source.empty()) {
throw std::invalid_argument("Options source is empty");
}
if(output.empty()) {
output = Files::getFileName(source);
}
}
};
} // namespace Riddle
2 changes: 1 addition & 1 deletion src/Visitors/GenVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <system_error>

namespace Riddle {
[[maybe_unused]] GenVisitor::GenVisitor(const std::string &moduleName): Builder(globalContext) {
GenVisitor::GenVisitor(const std::string &moduleName): Builder(globalContext) {
module = new llvm::Module(moduleName, globalContext);
module->setSourceFileName(moduleName + ".red");
opMap = getBinaryOpMap(Builder);
Expand Down
2 changes: 1 addition & 1 deletion src/Visitors/GenVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#define RIDDLE_LANGUAGE_GENVISITOR_H

#include "../Setup.h"
#include "Tools/Managers/VarManager.h"
#include <RiddleParserBaseVisitor.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LLVMContext.h>
#include <variant>

import Types.ClassNode;
import Manager.ClassManager;
import Manager.VarManager;

namespace Riddle {
/// @brief 用于实现生成 IR 的类
Expand Down
18 changes: 16 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
#include "Tools/BuildQueue.h"
#include "Visitors/GenVisitor.h"
#include "termcolor/termcolor.hpp"
import Tools.Options;
using namespace std;
int main(const int argv, char *argc[]) {
if(argv < 2) return 0;
Riddle::buildQueue.parserFile(argc[1]);
// 交由 Options 进行处理
vector<string> args;
for(int i = 1; i < argv; i++) {
args.push_back(argc[i]);
}
Riddle::Options opt;
try{
opt = Riddle::Options(args);
}
catch(std::invalid_argument) {
cout<<termcolor::red<<"No source file"<<termcolor::reset;
}
// Parser
Riddle::buildQueue.parserFile(opt.source);
Riddle::buildQueue.start();
Riddle::GenVisitor a("123");
return 0;
Expand Down

0 comments on commit 9ceaae5

Please sign in to comment.