Skip to content

Commit

Permalink
✨ fix(Gen): 至少能用混合表达式了
Browse files Browse the repository at this point in the history
  • Loading branch information
wangziwenhk committed Aug 25, 2024
1 parent 79afde1 commit 4407456
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 28 deletions.
4 changes: 2 additions & 2 deletions RiddleParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ classBody
exprPtr
: funcName=Identifier LeftBracket args=argsExpr RightBracket #funcExpr
| Identifier #objectExpr
| parent=exprPtr Dot child=exprPtr #blendExpr
| parent=exprPtr Dot child=exprPtr #blendExpr
;

exprPtrParser
Expand All @@ -111,7 +111,7 @@ expression
| expr=exprPtrParser Add Add #selfAddRightExpr // x++
| Sub Sub expr=exprPtrParser #selfSubLeftExpr // ++x
| expr=exprPtrParser Sub Sub #selfSubRightExpr // x++
| exprPtrParser #ptrExpr
| exprPtr #ptrExpr
| left=expression Star right=expression #mulExpr // x*y
| left=expression Div right=expression #divExpr // x/y
| left=expression Mod right=expression #modExpr // x%y
Expand Down
8 changes: 4 additions & 4 deletions parser/RiddleParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void riddleparserParserInitialize() {
36,18,41,256,257,5,36,0,0,257,258,5,36,0,0,258,339,3,34,17,0,259,260,
3,34,17,0,260,261,5,36,0,0,261,262,5,36,0,0,262,339,1,0,0,0,263,264,5,
37,0,0,264,265,5,37,0,0,265,339,3,34,17,0,266,267,3,34,17,0,267,268,5,
37,0,0,268,269,5,37,0,0,269,339,1,0,0,0,270,339,3,34,17,0,271,272,3,34,
37,0,0,268,269,5,37,0,0,269,339,1,0,0,0,270,339,3,32,16,0,271,272,3,34,
17,0,272,273,5,30,0,0,273,274,3,36,18,16,274,339,1,0,0,0,275,276,3,34,
17,0,276,277,5,36,0,0,277,278,5,30,0,0,278,279,3,36,18,15,279,339,1,0,
0,0,280,281,3,34,17,0,281,282,5,37,0,0,282,283,5,30,0,0,283,284,3,36,
Expand Down Expand Up @@ -2757,8 +2757,8 @@ std::any RiddleParser::BitXorExprContext::accept(tree::ParseTreeVisitor *visitor
}
//----------------- PtrExprContext ------------------------------------------------------------------

RiddleParser::ExprPtrParserContext* RiddleParser::PtrExprContext::exprPtrParser() {
return getRuleContext<RiddleParser::ExprPtrParserContext>(0);
RiddleParser::ExprPtrContext* RiddleParser::PtrExprContext::exprPtr() {
return getRuleContext<RiddleParser::ExprPtrContext>(0);
}

RiddleParser::PtrExprContext::PtrExprContext(ExpressionContext *ctx) { copyFrom(ctx); }
Expand Down Expand Up @@ -3905,7 +3905,7 @@ RiddleParser::ExpressionContext* RiddleParser::expression(int precedence) {
_ctx = _localctx;
previousContext = _localctx;
setState(270);
exprPtrParser();
exprPtr(0);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ class RiddleParser : public antlr4::Parser {
public:
PtrExprContext(ExpressionContext *ctx);

ExprPtrParserContext *exprPtrParser();
ExprPtrContext *exprPtr();
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.

42 changes: 24 additions & 18 deletions src/Visitors/GenVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace Riddle {

std::any GenVisitor::visitObjectExpr(RiddleParser::ObjectExprContext *ctx) {
llvm::Value *value = varManager.getVar(ctx->Identifier()->getText()).value;
return std::tuple{value,value->getType()};
return std::tuple{value, value->getType()};
}

std::any GenVisitor::visitProgram(RiddleParser::ProgramContext *ctx) {
Expand Down Expand Up @@ -270,7 +270,7 @@ namespace Riddle {
}

const auto loadValue = Builder.CreateLoad(type, var);
if(type != value->getType() && isSampleType(getTypeName(type))) {
if(type != value->getType() && !value->getType()->isPointerTy() && isSampleType(getTypeName(type))) {
value = cast[getTypeName(type)][getTypeName(value->getType())](Builder, value);
}
const auto result = binaryOperator(loadValue, value, op.substr(0, op.size() - 1));
Expand Down Expand Up @@ -470,6 +470,7 @@ namespace Riddle {
return func(Builder, value);
}

//BUG 无法判断是否为常量还是引用,先搁置者
std::any GenVisitor::visitFuncExpr(RiddleParser::FuncExprContext *ctx) {
auto funcName = ctx->funcName->getText();
if(!FuncCalls.contains(funcName)) {
Expand All @@ -478,7 +479,8 @@ namespace Riddle {
const auto args = any_cast<std::vector<llvm::Value *>>(visit(ctx->args));
const auto func = FuncCalls[funcName];
llvm::Value *result = Builder.CreateCall(func, args);
return std::tuple{result,result->getType()};

return std::tuple{result, result->getType()};
}

std::any GenVisitor::visitArgsExpr(RiddleParser::ArgsExprContext *ctx) {
Expand All @@ -503,44 +505,47 @@ namespace Riddle {
}

std::any GenVisitor::visitPtrExpr(RiddleParser::PtrExprContext *ctx) {
auto ptr = any_cast<llvm::Value *>(visit(ctx->children[0]));
const auto type = llvm::dyn_cast<llvm::PointerType>(ptr->getType());
if(type == nullptr) return ptr;
llvm::Value *value = Builder.CreateLoad(type, ptr);
auto tp = any_cast<std::tuple<llvm::Value *, llvm::Type *>>(visit(ctx->children[0]));
auto [value, type] = tp;
if(type == nullptr) return value;
else if(value->getType()->isPointerTy()) {
llvm::Value *load = Builder.CreateLoad(type, value);
return load;
}
return value;
}

// BUG 这里不考虑对函数返回的对象的引用
std::any GenVisitor::visitBlendExpr(RiddleParser::BlendExprContext *ctx) {
// todo 这里暂时没法使用对象的函数
const auto [parent,type] = std::any_cast<std::tuple<llvm::Value*,llvm::Type*>>(visit(ctx->parent));
const auto [parent, type] = std::any_cast<std::tuple<llvm::Value *, llvm::Type *>>(visit(ctx->parent));

const auto childName = ctx->child->getText();
ClassNode theClass;

if(const auto Alloca = llvm::dyn_cast<llvm::AllocaInst>(parent)) {
theClass = classManager.getClass(Alloca->getAllocatedType()->getStructName().str());
}else {
} else {
theClass = classManager.getClass(type->getStructName().str());
}

if(const auto FuncExpr = dynamic_cast<RiddleParser::FuncExprContext *>(ctx->child)) {
const auto func = theClass.get().funcs.at(childName);
const auto args = any_cast<std::vector<llvm::Value *>>(visit(FuncExpr->args));
const auto funcName = FuncExpr->funcName->getText();
const auto func = theClass.get().funcs.at(funcName);
auto args = any_cast<std::vector<llvm::Value *>>(visit(FuncExpr->args));
args.insert(args.begin(), parent);
llvm::Value *result = Builder.CreateCall(func, args);
return std::tuple{result,result->getType()};
return std::tuple{result, result->getType()};
} else {
const auto childName = ctx->child->getText();
const auto it = theClass.get().names.find(childName);
if(it == theClass.get().names.end()) {
throw std::logic_error("没有这个成员");
}
llvm::Value *result = Builder.CreateStructGEP(theClass.get().types, parent, it->second);
return std::tuple{result,theClass.get().types->getElementType(it->second)};
return std::tuple{result, theClass.get().types->getElementType(it->second)};
}
}
std::any GenVisitor::visitExprPtrParser(RiddleParser::ExprPtrParserContext *ctx) {
auto tp = any_cast<std::tuple<llvm::Value*,llvm::Type*>>(visit(ctx->children[0]));
auto [value,type] = tp;
auto [value,type] = any_cast<std::tuple<llvm::Value *, llvm::Type *>>(visit(ctx->children[0]));
return value;
}

Expand All @@ -559,14 +564,15 @@ namespace Riddle {
const std::string name = packStack.top() + ctx->className->getText();
const ClassNode theClass;
theClass.get().types = llvm::StructType::create(globalContext, name);
classManager.createClass(theClass);
packStack.push(packStack.top() + ctx->className->getText());
varManager.push();
ParentStack.push(theClass);
visit(ctx->body);
packStack.pop();
varManager.pop();
ParentStack.pop();
classManager.createClass(theClass);

return nullptr;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Visitors/GenVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ namespace Riddle {
/// @returns nullptr
std::any visitClassBody(RiddleParser::ClassBodyContext *ctx) override;
/// @brief 获取一个混合对象
/// @return llvm::Value*
/// @return tuple{llvm::Value*,llvm::Type*}
std::any visitBlendExpr(RiddleParser::BlendExprContext *ctx) override;
std::any visitExprPtrParser(RiddleParser::ExprPtrParserContext *context) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Tools/BuildQueue.h"
#include "Visitors/GenVisitor.h"
using namespace std;
int main(int argv, char *argc[]) {
int main(const int argv, char *argc[]) {
if(argv < 2) return 0;
Riddle::buildQueue.parserFile(argc[1]);
Riddle::buildQueue.start();
Expand Down

0 comments on commit 4407456

Please sign in to comment.