Skip to content

Commit

Permalink
Refactor and provide tokenize CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
XChy committed Aug 5, 2023
1 parent 9631f62 commit c4f8850
Show file tree
Hide file tree
Showing 28 changed files with 310 additions and 301 deletions.
1 change: 0 additions & 1 deletion .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ jobs:
- uses: actions/checkout@v3
- name: Install LLVM
run: |
sudo apt remove llvm
sudo apt install llvm-15
- name: CMake Configure
Expand Down
24 changes: 12 additions & 12 deletions LLVMIR/BinaryOp/ArithmeticOpImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace LLVMCodeGen {
ValueAndType AssignImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = generator(op->left());
auto [rhs, rhs_type] = generator(op->right());
auto [lhs, lhs_type] = generator(op->lhs());
auto [rhs, rhs_type] = generator(op->rhs());
if (!lhs_type || !rhs_type) return {nullptr, nullptr};
// Only the reference can be assigned to value
if (lhs_type->category != Type::Reference || lhs_type->isObject()) {
Expand All @@ -32,8 +32,8 @@ ValueAndType AssignImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType AddImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = deReference(generator(op->left()), helper);
auto [rhs, rhs_type] = deReference(generator(op->right()), helper);
auto [lhs, lhs_type] = deReference(generator(op->lhs()), helper);
auto [rhs, rhs_type] = deReference(generator(op->rhs()), helper);

if (!lhs_type || !rhs_type) return {nullptr, nullptr};
if (!(lhs_type->isNumber() && rhs_type->isNumber())) {
Expand All @@ -56,8 +56,8 @@ ValueAndType AddImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType SubImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = deReference(generator(op->left()), helper);
auto [rhs, rhs_type] = deReference(generator(op->right()), helper);
auto [lhs, lhs_type] = deReference(generator(op->lhs()), helper);
auto [rhs, rhs_type] = deReference(generator(op->rhs()), helper);
if (!lhs_type || !rhs_type) return {nullptr, nullptr};

if (!(lhs_type->isNumber() && rhs_type->isNumber())) {
Expand All @@ -77,8 +77,8 @@ ValueAndType SubImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType MulImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = deReference(generator(op->left()), helper);
auto [rhs, rhs_type] = deReference(generator(op->right()), helper);
auto [lhs, lhs_type] = deReference(generator(op->lhs()), helper);
auto [rhs, rhs_type] = deReference(generator(op->rhs()), helper);
if (!lhs_type || !rhs_type) return {nullptr, nullptr};

if (!(lhs_type->isNumber() && rhs_type->isNumber())) {
Expand All @@ -98,8 +98,8 @@ ValueAndType MulImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType DivImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = deReference(generator(op->left()), helper);
auto [rhs, rhs_type] = deReference(generator(op->right()), helper);
auto [lhs, lhs_type] = deReference(generator(op->lhs()), helper);
auto [rhs, rhs_type] = deReference(generator(op->rhs()), helper);
if (!lhs_type || !rhs_type) return {nullptr, nullptr};

if (!(lhs_type->isNumber() && rhs_type->isNumber())) {
Expand All @@ -123,8 +123,8 @@ ValueAndType DivImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType ModImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = deReference(generator(op->left()), helper);
auto [rhs, rhs_type] = deReference(generator(op->right()), helper);
auto [lhs, lhs_type] = deReference(generator(op->lhs()), helper);
auto [rhs, rhs_type] = deReference(generator(op->rhs()), helper);
if (!lhs_type || !rhs_type) return {nullptr, nullptr};

if (!(lhs_type->isNumber() && rhs_type->isNumber())) {
Expand Down
6 changes: 3 additions & 3 deletions LLVMIR/BinaryOp/BinaryOpProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ CodeGenProxy<BinaryOperatorNode>::CodeGenProxy()
ValueAndType CodeGenProxy<BinaryOperatorNode>::codeGen(
BinaryOperatorNode* ast, CodeGenContext* helper, const Generator& generator)
{
auto iter = processors.find(ast->operatorStr());
auto iter = processors.find(ast->opStr());
if (iter != processors.end())
return processors[ast->operatorStr()](ast, helper, generator);
return processors[ast->opStr()](ast, helper, generator);
else {
helper->error("Not support the operator '{}' yet", ast->operatorStr());
helper->error("Not support the operator '{}' yet", ast->opStr());
return {nullptr, nullptr};
}
}
32 changes: 16 additions & 16 deletions LLVMIR/BinaryOp/LogicalOpImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace LLVMCodeGen {
ValueAndType EqualImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = deReference(generator(op->left()), helper);
auto [rhs, rhs_type] = deReference(generator(op->right()), helper);
auto [lhs, lhs_type] = deReference(generator(op->lhs()), helper);
auto [rhs, rhs_type] = deReference(generator(op->rhs()), helper);

passErrorIfNot(lhs_type);
passErrorIfNot(rhs_type);
Expand Down Expand Up @@ -38,8 +38,8 @@ ValueAndType EqualImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType NotEqualImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = deReference(generator(op->left()), helper);
auto [rhs, rhs_type] = deReference(generator(op->right()), helper);
auto [lhs, lhs_type] = deReference(generator(op->lhs()), helper);
auto [rhs, rhs_type] = deReference(generator(op->rhs()), helper);

passErrorIfNot(lhs_type);
passErrorIfNot(rhs_type);
Expand All @@ -66,8 +66,8 @@ ValueAndType NotEqualImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType GreaterImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = deReference(generator(op->left()), helper);
auto [rhs, rhs_type] = deReference(generator(op->right()), helper);
auto [lhs, lhs_type] = deReference(generator(op->lhs()), helper);
auto [rhs, rhs_type] = deReference(generator(op->rhs()), helper);

passErrorIfNot(lhs_type);
passErrorIfNot(rhs_type);
Expand Down Expand Up @@ -98,8 +98,8 @@ ValueAndType GreaterImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType LessImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = deReference(generator(op->left()), helper);
auto [rhs, rhs_type] = deReference(generator(op->right()), helper);
auto [lhs, lhs_type] = deReference(generator(op->lhs()), helper);
auto [rhs, rhs_type] = deReference(generator(op->rhs()), helper);

passErrorIfNot(lhs_type);
passErrorIfNot(rhs_type);
Expand Down Expand Up @@ -130,8 +130,8 @@ ValueAndType LessImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType GreaterOrEqualImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = deReference(generator(op->left()), helper);
auto [rhs, rhs_type] = deReference(generator(op->right()), helper);
auto [lhs, lhs_type] = deReference(generator(op->lhs()), helper);
auto [rhs, rhs_type] = deReference(generator(op->rhs()), helper);

passErrorIfNot(lhs_type);
passErrorIfNot(rhs_type);
Expand Down Expand Up @@ -162,8 +162,8 @@ ValueAndType GreaterOrEqualImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType LessOrEqualImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = deReference(generator(op->left()), helper);
auto [rhs, rhs_type] = deReference(generator(op->right()), helper);
auto [lhs, lhs_type] = deReference(generator(op->lhs()), helper);
auto [rhs, rhs_type] = deReference(generator(op->rhs()), helper);

passErrorIfNot(lhs_type);
passErrorIfNot(rhs_type);
Expand Down Expand Up @@ -194,8 +194,8 @@ ValueAndType LessOrEqualImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType LogicalAndImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = generator(op->left());
auto [rhs, rhs_type] = generator(op->right());
auto [lhs, lhs_type] = generator(op->lhs());
auto [rhs, rhs_type] = generator(op->rhs());

passErrorIfNot(lhs_type);
passErrorIfNot(rhs_type);
Expand All @@ -214,8 +214,8 @@ ValueAndType LogicalAndImpl(BinaryOperatorNode* op, CodeGenContext* helper,
ValueAndType LogicalOrImpl(BinaryOperatorNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [lhs, lhs_type] = generator(op->left());
auto [rhs, rhs_type] = generator(op->right());
auto [lhs, lhs_type] = generator(op->lhs());
auto [rhs, rhs_type] = generator(op->rhs());

passErrorIfNot(lhs_type);
passErrorIfNot(rhs_type);
Expand Down
6 changes: 3 additions & 3 deletions LLVMIR/CodeGenProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ ValueAndType CodeGenProxy<IntegerNode>::codeGen(IntegerNode* ast,
return {val, XSharp::getI64Type()};
}

ValueAndType CodeGenProxy<DecimalFractionNode>::codeGen(
DecimalFractionNode* ast, CodeGenContext* helper,
ValueAndType CodeGenProxy<FPNode>::codeGen(
FPNode* ast, CodeGenContext* helper,
const Generator& generator)
{
using llvm::APFloat;
Expand Down Expand Up @@ -120,7 +120,7 @@ ValueAndType CodeGenProxy<BlockNode>::codeGen(BlockNode* ast,
return {nullptr, XSharp::getVoidType()};
}

ValueAndType CodeGenProxy<VariableExprNode>::codeGen(VariableExprNode* ast,
ValueAndType CodeGenProxy<VarExprNode>::codeGen(VarExprNode* ast,
CodeGenContext* helper,
const Generator& generator)
{
Expand Down
8 changes: 4 additions & 4 deletions LLVMIR/CodeGenProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ class CodeGenProxy<IntegerNode>
};

template <>
class CodeGenProxy<DecimalFractionNode>
class CodeGenProxy<FPNode>
{
public:
ValueAndType codeGen(DecimalFractionNode* ast, CodeGenContext* helper,
ValueAndType codeGen(FPNode* ast, CodeGenContext* helper,
const Generator& generator);
};

Expand Down Expand Up @@ -119,10 +119,10 @@ class CodeGenProxy<BlockNode>
};

template <>
class CodeGenProxy<VariableExprNode>
class CodeGenProxy<VarExprNode>
{
public:
ValueAndType codeGen(VariableExprNode* ast, CodeGenContext* helper,
ValueAndType codeGen(VarExprNode* ast, CodeGenContext* helper,
const Generator& generator);
};

Expand Down
10 changes: 5 additions & 5 deletions LLVMIR/CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ CodeGenerator::CodeGenerator()
addProxy<CharNode>();
addProxy<StringNode>();
addProxy<BooleanNode>();
addProxy<DecimalFractionNode>();
addProxy<VariableExprNode>();
addProxy<VariableNode>();
addProxy<FPNode>();
addProxy<VarExprNode>();
addProxy<VarDeclNode>();
addProxy<FunctionNode>();
addProxy<FunctionCallNode>();
addProxy<CallNode>();
addProxy<BlockNode>();
addProxy<IfNode>();
addProxy<WhileNode>();
addProxy<ContinueNode>();
addProxy<BreakNode>();
addProxy<ReturnNode>();
addProxy<BinaryOperatorNode>();
addProxy<UnaryOperatorNode>();
addProxy<UnaryOpNode>();
addProxy<ClassNode>();
addProxy<MemberExprNode>();
addProxy<MemberMethodNode>();
Expand Down
6 changes: 3 additions & 3 deletions LLVMIR/FuncProxies/CallProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using namespace XSharp;
using namespace XSharp::LLVMCodeGen;

ValueAndType CodeGenProxy<FunctionCallNode>::codeGen(FunctionCallNode* ast,
ValueAndType CodeGenProxy<CallNode>::codeGen(CallNode* ast,
CodeGenContext* helper,
const Generator& generator)
{
Expand All @@ -25,8 +25,8 @@ ValueAndType CodeGenProxy<FunctionCallNode>::codeGen(FunctionCallNode* ast,
using llvm::ConstantInt;
using llvm::Function;

if (ast->callee()->is<VariableExprNode>()) {
XString calleeName = ast->callee()->to<VariableExprNode>()->name();
if (ast->callee()->is<VarExprNode>()) {
XString calleeName = ast->callee()->to<VarExprNode>()->name();

std::vector<llvm::Value*> argumentValues;
std::vector<Type*> argumentTypes;
Expand Down
4 changes: 2 additions & 2 deletions LLVMIR/FuncProxies/CallProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace XSharp {
namespace LLVMCodeGen {

template <>
class CodeGenProxy<FunctionCallNode>
class CodeGenProxy<CallNode>
{
public:
ValueAndType codeGen(FunctionCallNode* ast, CodeGenContext* helper,
ValueAndType codeGen(CallNode* ast, CodeGenContext* helper,
const Generator& generator);
};

Expand Down
6 changes: 3 additions & 3 deletions LLVMIR/UnaryOP/UnaryOpImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace XSharp::LLVMCodeGen {

ValueAndType PositiveImpl(UnaryOperatorNode* op, CodeGenContext* helper,
ValueAndType PositiveImpl(UnaryOpNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [operand, operand_type] =
Expand All @@ -28,7 +28,7 @@ ValueAndType PositiveImpl(UnaryOperatorNode* op, CodeGenContext* helper,
return {operand, operand_type};
}

ValueAndType NegativeImpl(UnaryOperatorNode* op, CodeGenContext* helper,
ValueAndType NegativeImpl(UnaryOpNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [operand, operand_type] =
Expand All @@ -47,7 +47,7 @@ ValueAndType NegativeImpl(UnaryOperatorNode* op, CodeGenContext* helper,
return {result_val, operand_type};
}

ValueAndType NotImpl(UnaryOperatorNode* op, CodeGenContext* helper,
ValueAndType NotImpl(UnaryOpNode* op, CodeGenContext* helper,
const Generator& generator)
{
auto [operand, operand_type] =
Expand Down
8 changes: 4 additions & 4 deletions LLVMIR/UnaryOP/UnaryOpImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

namespace XSharp::LLVMCodeGen {

typedef ValueAndType UnaryOpImpl(UnaryOperatorNode*, CodeGenContext*,
typedef ValueAndType UnaryOpImpl(UnaryOpNode*, CodeGenContext*,
const Generator&);

ValueAndType PositiveImpl(UnaryOperatorNode*, CodeGenContext* helper,
ValueAndType PositiveImpl(UnaryOpNode*, CodeGenContext* helper,
const Generator& generator);
ValueAndType NegativeImpl(UnaryOperatorNode*, CodeGenContext* helper,
ValueAndType NegativeImpl(UnaryOpNode*, CodeGenContext* helper,
const Generator& generator);
ValueAndType NotImpl(UnaryOperatorNode*, CodeGenContext* helper,
ValueAndType NotImpl(UnaryOpNode*, CodeGenContext* helper,
const Generator& generator);
} // namespace XSharp::LLVMCodeGen
12 changes: 6 additions & 6 deletions LLVMIR/UnaryOP/UnaryOpProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
using namespace XSharp;
using namespace XSharp::LLVMCodeGen;

CodeGenProxy<UnaryOperatorNode>::CodeGenProxy()
CodeGenProxy<UnaryOpNode>::CodeGenProxy()
{
processors["+"] = PositiveImpl;
processors["-"] = NegativeImpl;
processors["!"] = NotImpl;
}

ValueAndType CodeGenProxy<UnaryOperatorNode>::codeGen(
UnaryOperatorNode* ast, CodeGenContext* helper, const Generator& generator)
ValueAndType CodeGenProxy<UnaryOpNode>::codeGen(
UnaryOpNode* ast, CodeGenContext* helper, const Generator& generator)
{
if (processors.contains(ast->operatorStr()))
return processors[ast->operatorStr()](ast, helper, generator);
if (processors.contains(ast->opStr()))
return processors[ast->opStr()](ast, helper, generator);
else {
helper->error("Not support the unary operator {}", ast->operatorStr());
helper->error("Not support the unary operator {}", ast->opStr());
return {nullptr, nullptr};
}
}
4 changes: 2 additions & 2 deletions LLVMIR/UnaryOP/UnaryOpProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace XSharp::LLVMCodeGen {

template <>
class CodeGenProxy<UnaryOperatorNode>
class CodeGenProxy<UnaryOpNode>
{
public:
CodeGenProxy();
ValueAndType codeGen(UnaryOperatorNode* ast, CodeGenContext* helper,
ValueAndType codeGen(UnaryOpNode* ast, CodeGenContext* helper,
const Generator& generator);

private:
Expand Down
Loading

0 comments on commit c4f8850

Please sign in to comment.