Skip to content

Commit

Permalink
fix(Gen): 数组可以被赋值了
Browse files Browse the repository at this point in the history
  • Loading branch information
wangziwenhk committed Jul 26, 2024
1 parent 3fce1c3 commit 025f193
Show file tree
Hide file tree
Showing 11 changed files with 1,017 additions and 862 deletions.
6 changes: 3 additions & 3 deletions parser/RiddleLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void riddlelexerLexerInitialize() {
"WHITESPACE"},
std::vector<std::string>{
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"},
std::vector<std::string>{
std::vector<std::string>{
"DEFAULT_MODE"
},
std::vector<std::string>{
Expand All @@ -95,8 +95,8 @@ void riddlelexerLexerInitialize() {
6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14,
7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 2, 18, 7, 18, 2, 19, 7, 19, 2, 20, 7, 20, 2, 21,
7, 21, 2, 22, 7, 22, 2, 23, 7, 23, 2, 24, 7, 24, 2, 25, 7, 25, 2, 26, 7, 26, 2, 27, 7, 27, 2, 28,
7,28,2,29,7,29,2,30,7,30,2,31,7,31,2,32,7,32,2,33,7,33,2,34,7,34,2,35,
7,35,2,36,7,36,2,37,7,37,2,38,7,38,2,39,7,39,2,40,7,40,2,41,7,41,2,42,
7, 28, 2, 29, 7, 29, 2, 30, 7, 30, 2, 31, 7, 31, 2, 32, 7, 32, 2, 33, 7, 33, 2, 34, 7, 34, 2, 35,
7,35,2,36,7,36,2,37,7,37,2,38,7,38,2,39,7,39,2,40,7,40,2,41,7,41,2,42,
7,42,2,43,7,43,2,44,7,44,2,45,7,45,2,46,7,46,2,47,7,47,2,48,7,48,2,49,
7,49,2,50,7,50,2,51,7,51,2,52,7,52,2,53,7,53,2,54,7,54,2,55,7,55,2,56,
7, 56, 2, 57, 7, 57, 2, 58, 7, 58, 2, 59, 7, 59, 2, 60, 7, 60, 2, 61, 7, 61, 1, 0, 1, 0, 1, 0, 1,
Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class RiddleLexer : public antlr4::Lexer {

explicit RiddleLexer(antlr4::CharStream *input);

~RiddleLexer() override;
~RiddleLexer() override;


std::string getGrammarFileName() const override;
Expand Down
1,493 changes: 801 additions & 692 deletions parser/RiddleParser.cpp

Large diffs are not rendered by default.

235 changes: 128 additions & 107 deletions parser/RiddleParser.h

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions parser/RiddleParser.interp

Large diffs are not rendered by default.

21 changes: 12 additions & 9 deletions parser/RiddleParserBaseListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ class RiddleParserBaseListener : public RiddleParserListener {
virtual void enterClassBody(RiddleParser::ClassBodyContext * /*ctx*/) override {}
virtual void exitClassBody(RiddleParser::ClassBodyContext * /*ctx*/) override {}

virtual void enterFuncExpr(RiddleParser::FuncExprContext * /*ctx*/) override {}
virtual void exitFuncExpr(RiddleParser::FuncExprContext * /*ctx*/) override {}

virtual void enterSquareExpr(RiddleParser::SquareExprContext * /*ctx*/) override {}
virtual void exitSquareExpr(RiddleParser::SquareExprContext * /*ctx*/) override {}

virtual void enterObjectExpr(RiddleParser::ObjectExprContext * /*ctx*/) override {}
virtual void exitObjectExpr(RiddleParser::ObjectExprContext * /*ctx*/) override {}

virtual void enterSelfSubRightExpr(RiddleParser::SelfSubRightExprContext * /*ctx*/) override { }
virtual void exitSelfSubRightExpr(RiddleParser::SelfSubRightExprContext * /*ctx*/) override { }

Expand Down Expand Up @@ -109,6 +118,9 @@ class RiddleParserBaseListener : public RiddleParserListener {
virtual void enterBitXorExpr(RiddleParser::BitXorExprContext * /*ctx*/) override { }
virtual void exitBitXorExpr(RiddleParser::BitXorExprContext * /*ctx*/) override { }

virtual void enterPtrExpr(RiddleParser::PtrExprContext * /*ctx*/) override {}
virtual void exitPtrExpr(RiddleParser::PtrExprContext * /*ctx*/) override {}

virtual void enterStringExpr(RiddleParser::StringExprContext * /*ctx*/) override { }
virtual void exitStringExpr(RiddleParser::StringExprContext * /*ctx*/) override { }

Expand Down Expand Up @@ -142,12 +154,6 @@ class RiddleParserBaseListener : public RiddleParserListener {
virtual void enterSubAssignExpr(RiddleParser::SubAssignExprContext * /*ctx*/) override { }
virtual void exitSubAssignExpr(RiddleParser::SubAssignExprContext * /*ctx*/) override { }

virtual void enterFuncExpr(RiddleParser::FuncExprContext * /*ctx*/) override {}
virtual void exitFuncExpr(RiddleParser::FuncExprContext * /*ctx*/) override {}

virtual void enterSquareExpr(RiddleParser::SquareExprContext * /*ctx*/) override {}
virtual void exitSquareExpr(RiddleParser::SquareExprContext * /*ctx*/) override {}

virtual void enterBracketExpr(RiddleParser::BracketExprContext * /*ctx*/) override { }
virtual void exitBracketExpr(RiddleParser::BracketExprContext * /*ctx*/) override { }

Expand Down Expand Up @@ -202,9 +208,6 @@ class RiddleParserBaseListener : public RiddleParserListener {
virtual void enterAndExpr(RiddleParser::AndExprContext * /*ctx*/) override { }
virtual void exitAndExpr(RiddleParser::AndExprContext * /*ctx*/) override { }

virtual void enterObjectExpr(RiddleParser::ObjectExprContext * /*ctx*/) override { }
virtual void exitObjectExpr(RiddleParser::ObjectExprContext * /*ctx*/) override { }

virtual void enterId(RiddleParser::IdContext * /*ctx*/) override { }
virtual void exitId(RiddleParser::IdContext * /*ctx*/) override { }

Expand Down
28 changes: 16 additions & 12 deletions parser/RiddleParserBaseVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ class RiddleParserBaseVisitor : public RiddleParserVisitor {
return visitChildren(ctx);
}

virtual std::any visitFuncExpr(RiddleParser::FuncExprContext *ctx) override {
return visitChildren(ctx);
}

virtual std::any visitSquareExpr(RiddleParser::SquareExprContext *ctx) override {
return visitChildren(ctx);
}

virtual std::any visitObjectExpr(RiddleParser::ObjectExprContext *ctx) override {
return visitChildren(ctx);
}

virtual std::any visitSelfSubRightExpr(RiddleParser::SelfSubRightExprContext *ctx) override {
return visitChildren(ctx);
}
Expand Down Expand Up @@ -139,6 +151,10 @@ class RiddleParserBaseVisitor : public RiddleParserVisitor {
return visitChildren(ctx);
}

virtual std::any visitPtrExpr(RiddleParser::PtrExprContext *ctx) override {
return visitChildren(ctx);
}

virtual std::any visitStringExpr(RiddleParser::StringExprContext *ctx) override {
return visitChildren(ctx);
}
Expand Down Expand Up @@ -183,14 +199,6 @@ class RiddleParserBaseVisitor : public RiddleParserVisitor {
return visitChildren(ctx);
}

virtual std::any visitFuncExpr(RiddleParser::FuncExprContext *ctx) override {
return visitChildren(ctx);
}

virtual std::any visitSquareExpr(RiddleParser::SquareExprContext *ctx) override {
return visitChildren(ctx);
}

virtual std::any visitBracketExpr(RiddleParser::BracketExprContext *ctx) override {
return visitChildren(ctx);
}
Expand Down Expand Up @@ -263,10 +271,6 @@ class RiddleParserBaseVisitor : public RiddleParserVisitor {
return visitChildren(ctx);
}

virtual std::any visitObjectExpr(RiddleParser::ObjectExprContext *ctx) override {
return visitChildren(ctx);
}

virtual std::any visitId(RiddleParser::IdContext *ctx) override {
return visitChildren(ctx);
}
Expand Down
21 changes: 12 additions & 9 deletions parser/RiddleParserListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class RiddleParserListener : public antlr4::tree::ParseTreeListener {
virtual void enterClassBody(RiddleParser::ClassBodyContext *ctx) = 0;
virtual void exitClassBody(RiddleParser::ClassBodyContext *ctx) = 0;

virtual void enterFuncExpr(RiddleParser::FuncExprContext *ctx) = 0;
virtual void exitFuncExpr(RiddleParser::FuncExprContext *ctx) = 0;

virtual void enterSquareExpr(RiddleParser::SquareExprContext *ctx) = 0;
virtual void exitSquareExpr(RiddleParser::SquareExprContext *ctx) = 0;

virtual void enterObjectExpr(RiddleParser::ObjectExprContext *ctx) = 0;
virtual void exitObjectExpr(RiddleParser::ObjectExprContext *ctx) = 0;

virtual void enterSelfSubRightExpr(RiddleParser::SelfSubRightExprContext *ctx) = 0;
virtual void exitSelfSubRightExpr(RiddleParser::SelfSubRightExprContext *ctx) = 0;

Expand Down Expand Up @@ -107,6 +116,9 @@ class RiddleParserListener : public antlr4::tree::ParseTreeListener {
virtual void enterBitXorExpr(RiddleParser::BitXorExprContext *ctx) = 0;
virtual void exitBitXorExpr(RiddleParser::BitXorExprContext *ctx) = 0;

virtual void enterPtrExpr(RiddleParser::PtrExprContext *ctx) = 0;
virtual void exitPtrExpr(RiddleParser::PtrExprContext *ctx) = 0;

virtual void enterStringExpr(RiddleParser::StringExprContext *ctx) = 0;
virtual void exitStringExpr(RiddleParser::StringExprContext *ctx) = 0;

Expand Down Expand Up @@ -140,12 +152,6 @@ class RiddleParserListener : public antlr4::tree::ParseTreeListener {
virtual void enterSubAssignExpr(RiddleParser::SubAssignExprContext *ctx) = 0;
virtual void exitSubAssignExpr(RiddleParser::SubAssignExprContext *ctx) = 0;

virtual void enterFuncExpr(RiddleParser::FuncExprContext *ctx) = 0;
virtual void exitFuncExpr(RiddleParser::FuncExprContext *ctx) = 0;

virtual void enterSquareExpr(RiddleParser::SquareExprContext *ctx) = 0;
virtual void exitSquareExpr(RiddleParser::SquareExprContext *ctx) = 0;

virtual void enterBracketExpr(RiddleParser::BracketExprContext *ctx) = 0;
virtual void exitBracketExpr(RiddleParser::BracketExprContext *ctx) = 0;

Expand Down Expand Up @@ -200,9 +206,6 @@ class RiddleParserListener : public antlr4::tree::ParseTreeListener {
virtual void enterAndExpr(RiddleParser::AndExprContext *ctx) = 0;
virtual void exitAndExpr(RiddleParser::AndExprContext *ctx) = 0;

virtual void enterObjectExpr(RiddleParser::ObjectExprContext *ctx) = 0;
virtual void exitObjectExpr(RiddleParser::ObjectExprContext *ctx) = 0;

virtual void enterId(RiddleParser::IdContext *ctx) = 0;
virtual void exitId(RiddleParser::IdContext *ctx) = 0;

Expand Down
14 changes: 8 additions & 6 deletions parser/RiddleParserVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class RiddleParserVisitor : public antlr4::tree::AbstractParseTreeVisitor {

virtual std::any visitClassBody(RiddleParser::ClassBodyContext *context) = 0;

virtual std::any visitFuncExpr(RiddleParser::FuncExprContext *context) = 0;

virtual std::any visitSquareExpr(RiddleParser::SquareExprContext *context) = 0;

virtual std::any visitObjectExpr(RiddleParser::ObjectExprContext *context) = 0;

virtual std::any visitSelfSubRightExpr(RiddleParser::SelfSubRightExprContext *context) = 0;

virtual std::any visitAndAssignExpr(RiddleParser::AndAssignExprContext *context) = 0;
Expand Down Expand Up @@ -81,6 +87,8 @@ class RiddleParserVisitor : public antlr4::tree::AbstractParseTreeVisitor {

virtual std::any visitBitXorExpr(RiddleParser::BitXorExprContext *context) = 0;

virtual std::any visitPtrExpr(RiddleParser::PtrExprContext *context) = 0;

virtual std::any visitStringExpr(RiddleParser::StringExprContext *context) = 0;

virtual std::any visitGreaterExpr(RiddleParser::GreaterExprContext *context) = 0;
Expand All @@ -103,10 +111,6 @@ class RiddleParserVisitor : public antlr4::tree::AbstractParseTreeVisitor {

virtual std::any visitSubAssignExpr(RiddleParser::SubAssignExprContext *context) = 0;

virtual std::any visitFuncExpr(RiddleParser::FuncExprContext *context) = 0;

virtual std::any visitSquareExpr(RiddleParser::SquareExprContext *context) = 0;

virtual std::any visitBracketExpr(RiddleParser::BracketExprContext *context) = 0;

virtual std::any visitBooleanExpr(RiddleParser::BooleanExprContext *context) = 0;
Expand Down Expand Up @@ -143,8 +147,6 @@ class RiddleParserVisitor : public antlr4::tree::AbstractParseTreeVisitor {

virtual std::any visitAndExpr(RiddleParser::AndExprContext *context) = 0;

virtual std::any visitObjectExpr(RiddleParser::ObjectExprContext *context) = 0;

virtual std::any visitId(RiddleParser::IdContext *context) = 0;

virtual std::any visitNumber(RiddleParser::NumberContext *context) = 0;
Expand Down
50 changes: 30 additions & 20 deletions src/Visitors/GenVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,24 @@ namespace Riddle {
if(value1->getType()->isStructTy()) {
throw std::logic_error("还没实现");
} else {//基本类型处理
if(value1->getType() != value2->getType()) {
throw std::logic_error("Type mismatch");
}
auto it = opMap.find(op);
if(it != opMap.end()) {
return it->second(Builder, value1, value2);
}
}
return nullptr;
}
llvm::Value *GenVisitor::assignBinaryOp(llvm::AllocaInst *var, llvm::Value *value, std::string op) {
auto loadValue = Builder.CreateLoad(var->getAllocatedType(), var);
if(var->getAllocatedType() != value->getType()) {
value = cast[getTypeName(var->getAllocatedType())][getTypeName(value->getType())](Builder, value);
llvm::Value *GenVisitor::assignBinaryOp(llvm::Value *var, llvm::Value *value, std::string op) {
llvm::Type *type = nullptr;
if(auto gep = dyn_cast<llvm::GetElementPtrInst>(var)) {
type = gep->getResultElementType();
} else if(auto allocaTy = dyn_cast<llvm::AllocaInst>(var)) {
type = allocaTy->getAllocatedType();
}

auto loadValue = Builder.CreateLoad(type, var);
if(type != value->getType() && isSampleType(getTypeName(type))) {
value = cast[getTypeName(type)][getTypeName(value->getType())](Builder, value);
}
auto result = binaryOperator(loadValue, value, op.substr(0, op.size() - 1));
Builder.CreateStore(result, var);
Expand Down Expand Up @@ -325,62 +329,62 @@ namespace Riddle {
// endregion
// region 赋值的双目运算
std::any GenVisitor::visitAssignExpr(RiddleParser::AssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, "=");
}
std::any GenVisitor::visitAddAssignExpr(RiddleParser::AddAssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, "+=");
}
std::any GenVisitor::visitSubAssignExpr(RiddleParser::SubAssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, "-=");
}
std::any GenVisitor::visitMulAssignExpr(RiddleParser::MulAssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, "*=");
}
std::any GenVisitor::visitDivAssignExpr(RiddleParser::DivAssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, "/=");
}
std::any GenVisitor::visitModAssignExpr(RiddleParser::ModAssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, "%=");
}
std::any GenVisitor::visitShlAssignExpr(RiddleParser::ShlAssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, "<<=");
}
std::any GenVisitor::visitAShrAssignExpr(RiddleParser::AShrAssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, ">>=");
}
std::any GenVisitor::visitLShrAssignExpr(RiddleParser::LShrAssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, ">>>=");
}
std::any GenVisitor::visitAndAssignExpr(RiddleParser::AndAssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, "&=");
}
std::any GenVisitor::visitOrAssignExpr(RiddleParser::OrAssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, "|=");
}
std::any GenVisitor::visitXorAssignExpr(RiddleParser::XorAssignExprContext *ctx) {
auto var = any_cast<llvm::AllocaInst *>(visit(ctx->left));
auto var = any_cast<llvm::Value *>(visit(ctx->left));
auto value = any_cast<llvm::Value *>(visit(ctx->right));
return assignBinaryOp(var, value, "^=");
}
Expand Down Expand Up @@ -430,7 +434,13 @@ namespace Riddle {
auto index = any_cast<llvm::Value *>(visit(ctx->right));
auto ElementTy = dyn_cast<llvm::PointerType>(array->getType());
llvm::Value *ptr = Builder.CreateGEP(ElementTy, array, {index}, "nthElementPtr");
llvm::Value *value = Builder.CreateLoad(ElementTy, ptr);
// llvm::Value *value = Builder.CreateLoad(ElementTy, ptr);
return ptr;
}
std::any GenVisitor::visitPtrExpr(RiddleParser::PtrExprContext *ctx) {
auto ptr = any_cast<llvm::Value *>(visit(ctx->children[0]));
auto type = llvm::cast<llvm::PointerType>(ptr->getType());
llvm::Value *value = Builder.CreateLoad(type, ptr);
return value;
}
}// namespace Riddle
5 changes: 4 additions & 1 deletion src/Visitors/GenVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Riddle {
/// @param value 值
/// @param op 操作符
/// @returns llvm::Value*
llvm::Value *assignBinaryOp(llvm::AllocaInst *var, llvm::Value *value, std::string op);
llvm::Value *assignBinaryOp(llvm::Value *var, llvm::Value *value, std::string op);
// endregion
public:
[[maybe_unused]]
Expand Down Expand Up @@ -200,6 +200,9 @@ namespace Riddle {
/// @brief 获取数组内容
/// @returns llvm::Value*
std::any visitSquareExpr(RiddleParser::SquareExprContext *ctx) override;
/// @brief 解析指针内容
/// @returns llvm::Value*
std::any visitPtrExpr(RiddleParser::PtrExprContext *ctx) override;
};
}// namespace Riddle

Expand Down

0 comments on commit 025f193

Please sign in to comment.