Skip to content

Commit

Permalink
Manager修改module
Browse files Browse the repository at this point in the history
  • Loading branch information
wangziwenhk committed Sep 25, 2024
1 parent 41623bd commit 838c9d6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
18 changes: 8 additions & 10 deletions src/Tools/Managers/ClassManager.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
//
// Created by wangz on 24-7-29.
//
module;
#include <stdexcept>
module Manager.ClassManager;

#include "ClassManager.h"

namespace Riddle{
namespace Riddle {
/// @brief 查找相关的类
ClassNode ClassManager::getClass(const std::string &name){
if (const auto it = Classes.find(name);it == Classes.end()) {
ClassNode ClassManager::getClass(const std::string &name) {
if(const auto it = Classes.find(name); it == Classes.end()) {
throw std::logic_error("没有这个类或者使用了不支持的路径");
}
return Classes.find(name)->second;
}

void ClassManager::createClass(const ClassNode &theClass){
Classes.emplace(theClass.get().types->getName().str(),theClass);
void ClassManager::createClass(const ClassNode &theClass) {
Classes.emplace(theClass.get().types->getName().str(), theClass);
}
} // namespace Riddle
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
//
// Created by wangz on 24-7-29.
//

#ifndef RIDDLE_LANGUAGE_CLASSMANAGER_H
#define RIDDLE_LANGUAGE_CLASSMANAGER_H
module;
#include <llvm/IR/IRBuilder.h>

export module Manager.ClassManager;
import Types.Class;
import Types.ClassNode;

namespace Riddle{
export namespace Riddle {
/// @brief 管理类
class ClassManager{
class ClassManager {
/// @brief 存储数据字段
std::unordered_map<std::string,const ClassNode> Classes;
std::unordered_map<std::string, const ClassNode> Classes;

public:
ClassNode getClass(const std::string &name);

// class 在进入该函数后则不可修改
void createClass(const ClassNode &theClass);
};
} // namespace Riddle

#endif//RIDDLE_LANGUAGE_CLASSMANAGER_H
} // namespace Riddle
14 changes: 8 additions & 6 deletions src/Tools/Managers/VarManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "VarManager.h"
module;
#include <stdexcept>
#include <llvm/IR/Value.h>
module Manager.VarManager;

namespace Riddle {
bool VarManager::isDefined(const std::string &name) {
Expand All @@ -12,16 +14,16 @@ namespace Riddle {
Defined.emplace();
}
void VarManager::pop() {
for(auto i: Defined.top()) {
NamedVar[i.first].pop();
if(NamedVar[i.first].empty()) {
NamedVar.erase(i.first);
for(auto [name, iDefined]: Defined.top()) {
NamedVar[name].pop();
if(NamedVar[name].empty()) {
NamedVar.erase(name);
}
}
Defined.pop();
}
void VarManager::defineVar(const std::string &name, const bool &isConst, llvm::Value *value) {
if(Defined.top().count(name)) {
if(Defined.top().contains(name)) {
throw std::logic_error("The variable has been defined multiple times");
}
NamedVar[name].push(Variable(name, value, isConst));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#ifndef RIDDLE_LANGUAGE_VARMANAGER_H
#define RIDDLE_LANGUAGE_VARMANAGER_H

module;
#include <llvm/IR/Value.h>
#include <stack>
#include <string>
#include <unordered_map>
export module Manager.VarManager;

import Type.Variable;
namespace Riddle {

export namespace Riddle {
class VarManager {
private://成员区
//成员区
std::unordered_map<std::string, std::stack<Variable>> NamedVar;
std::stack<std::unordered_map<std::string, bool>> Defined;

private:
bool isDefined(const std::string &name);

public://函数区
Expand All @@ -35,6 +35,4 @@ namespace Riddle {
[[nodiscard]] bool isGlobal()const;
};

}// namespace Riddle

#endif//RIDDLE_LANGUAGE_VARMANAGER_H
}// namespace Riddle
1 change: 0 additions & 1 deletion src/Types/Variable.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module;
#include <utility>
export module Type.Variable;
export namespace Riddle {
const std::string Null = "null";
class Variable {
public:
const std::string name;
Expand Down
3 changes: 2 additions & 1 deletion src/Visitors/GenVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include <llvm/IR/LLVMContext.h>
#include <variant>

#include "Tools/Managers/ClassManager.h"
import Types.ClassNode;
import Manager.ClassManager;

namespace Riddle {
/// @brief 用于实现生成 IR 的类
Expand Down

0 comments on commit 838c9d6

Please sign in to comment.