Skip to content

Commit

Permalink
添加了部分定义语句
Browse files Browse the repository at this point in the history
  • Loading branch information
wangziwenhk committed Oct 3, 2024
1 parent 6afe96d commit 58b7784
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 26 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,5 @@ add_executable(Riddle_Language ${SOURCE_FILES} ${AST_FILES})
target_compile_definitions(Riddle_Language PRIVATE ${PLATFORM_DEFINES})

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

target_link_libraries(Riddle_Language antlr4_shared)
2 changes: 1 addition & 1 deletion parser/RiddleLexer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleLexer.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleLexer.g4 by ANTLR 4.13.1


#include "RiddleLexer.h"
Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleLexer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleLexer.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleLexer.g4 by ANTLR 4.13.1

#pragma once

Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParser.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1


#include "RiddleParserListener.h"
Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParser.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1

#pragma once

Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParserBaseListener.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1


#include "RiddleParserBaseListener.h"
Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParserBaseListener.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1

#pragma once

Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParserBaseVisitor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1


#include "RiddleParserBaseVisitor.h"
Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParserBaseVisitor.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1

#pragma once

Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParserListener.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1


#include "RiddleParserListener.h"
Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParserListener.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1

#pragma once

Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParserVisitor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1


#include "RiddleParserVisitor.h"
Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleParserVisitor.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from C:/Users/wangz/CLionProjects/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleParser.g4 by ANTLR 4.13.1

#pragma once

Expand Down
75 changes: 75 additions & 0 deletions src/Types/Statements.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module;
#include "Tools/GenTools.h"
#include <llvm/IR/Value.h>
#include <utility>
export module Statements;
namespace Riddle {
/// @brief 所有语句的基本语句
class BaseStmt {
public:
/// 用于标识语句类型
enum class StmtTypeID {
VarDefineStmtID, // 变量定义
FuncDefineStmtID,// 函数定义
ForStmtID, // for 循环
WhileStmtID, // while 循环
BinaryOpStmtID, // 双元运算
UnaryOpStmtID, // 单元运算
LabelStmtID, // Label 表达式
ReturnStmtID, // 返回语句
IfStmtID, // If 语句
TryStmtID, // 错误处理
ObjStmtID, // 获取对象
ConstantStmtID, // 常量

NullStmtID, // 没有任何效果的语句
};

protected:
StmtTypeID StmtID;

public:
explicit BaseStmt(const StmtTypeID stmtTypeID): StmtID(stmtTypeID) {}

[[nodiscard]] inline StmtTypeID getTypeID() const {
return StmtID;
}
};

class VarDefineStmt : public BaseStmt {
std::string name;
std::string type;
/// 对于一个值,一定可以被解析为一个Statement
BaseStmt value;

public:
VarDefineStmt(std::string name, std::string type, const BaseStmt value): BaseStmt(StmtTypeID::VarDefineStmtID), name(std::move(name)), type(std::move(type)), value(value) {}

[[nodiscard]] inline std::string getName() const { return name; }
[[nodiscard]] inline std::string getType() const { return type; }
[[nodiscard]] inline BaseStmt getValue() const { return value; }
};

class FuncDefineStmt : public BaseStmt {
public:
class Arg {
std::string name;
std::string type;
std::string default_value;
};

protected:
std::string func_name;
std::string return_type;
std::vector<Arg> args;
std::vector<BaseStmt> body;

public:
FuncDefineStmt(std::string func_name, std::string return_type, std::vector<Arg> args = {}): BaseStmt(StmtTypeID::FuncDefineStmtID),
func_name(std::move(func_name)),
return_type(std::move(return_type)), args(std::move(args)) {}
void addStmt(const BaseStmt stmt) {
body.push_back(stmt);
}
};
}// namespace Riddle
17 changes: 14 additions & 3 deletions src/Visitors/StatVisitor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
module;
module Visitors.StatVisitor;
#include "StatVisitor.h"

namespace Riddle {
std::any StatVisitor::visitStatement_ed(RiddleParser::Statement_edContext *ctx) {
return visit(ctx->children[0]);
}

std::any StatVisitor::visitInteger(RiddleParser::IntegerContext *ctx) {
return ctx->value;
}
std::any StatVisitor::visitVarDefineStatement(RiddleParser::VarDefineStatementContext *ctx) {
const std::string name = ctx->name->getText();
return {};
}

}
}// namespace Riddle
16 changes: 16 additions & 0 deletions src/Visitors/StatVisitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef STATVISITOR_H
#define STATVISITOR_H

#include <RiddleParserBaseVisitor.h>

namespace Riddle {

class StatVisitor final : RiddleParserBaseVisitor{
std::any visitStatement_ed(RiddleParser::Statement_edContext *ctx) override;
std::any visitInteger(RiddleParser::IntegerContext *ctx) override;
std::any visitVarDefineStatement(RiddleParser::VarDefineStatementContext *ctx) override;
};

}// namespace Riddle

#endif//STATVISITOR_H
9 changes: 0 additions & 9 deletions src/Visitors/StatVisitor.ixx

This file was deleted.

0 comments on commit 58b7784

Please sign in to comment.