Skip to content

Commit

Permalink
refactor: huge code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Apr 22, 2024
1 parent 42258a1 commit 6520d36
Show file tree
Hide file tree
Showing 53 changed files with 454 additions and 890 deletions.
43 changes: 23 additions & 20 deletions src/bytecode/builtin_functions/AddFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@

#include "AddFunction.h"
#include "bytecode/instructions/Add.h"
#include "bytecode/instructions/AddRI.h"
#include "bytecode/instructions/AddRR.h"
#include "bytecode/instructions/Increment.h"
#include "exceptions/SyntaxError.h"

namespace Bytecode::BuiltinFunctions {
Expand All @@ -25,14 +21,14 @@ namespace Bytecode::BuiltinFunctions {
if (args[1]->type == SyntaxTreeNode::TokenNode &&
((TokenNode *) args[1])->getName() == "1") {
args[0]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::Increment());
instructions.push_back(new (Instruction){Instruction::Increment});
return;
}

if (args[0]->type == SyntaxTreeNode::TokenNode &&
((TokenNode *) args[0])->getName() == "1") {
args[1]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::Increment());
instructions.push_back(new (Instruction){Instruction::Increment});
return;
}

Expand All @@ -41,40 +37,47 @@ namespace Bytecode::BuiltinFunctions {
if (((TokenNode *) args[0])->token.type == Token::Symbol &&
((TokenNode *) args[1])->token.type == Token::Number &&
segment->find_variable(((TokenNode *) args[0])->getName()) != -1) {
instructions.push_back(
new Bytecode::AddRI(
segment->find_variable(((TokenNode *) args[0])->getName()),
((TokenNode *) args[1])->token.asNumber()));
instructions.push_back(new (Instruction){
Instruction::AddRI,
{.ri_params = {
segment->find_variable(((TokenNode *) args[0])->getName()),
StackObject(((TokenNode *) args[1])->token)}},
});
return;
}
if (((TokenNode *) args[0])->token.type == Token::Number &&
((TokenNode *) args[1])->token.type == Token::Symbol &&
segment->find_variable(((TokenNode *) args[1])->getName()) != -1) {
instructions.push_back(
new Bytecode::AddRI(
segment->find_variable(((TokenNode *) args[1])->getName()),
((TokenNode *) args[0])->token.asNumber()));
instructions.push_back(new (Instruction){
Instruction::AddRI,
{.ri_params = {
segment->find_variable(((TokenNode *) args[1])->getName()),
StackObject(((TokenNode *) args[0])->token)}},
});
return;
}
if (((TokenNode *) args[0])->token.type == Token::Symbol &&
((TokenNode *) args[1])->token.type == Token::Symbol &&
segment->find_variable(((TokenNode *) args[0])->getName()) != -1 &&
segment->find_variable(((TokenNode *) args[1])->getName()) != -1) {
instructions.push_back(
new Bytecode::AddRR(
segment->find_variable(((TokenNode *) args[0])->getName()),
segment->find_variable(((TokenNode *) args[1])->getName())));
instructions.push_back(new (Instruction){
Instruction::AddRR,
{.rr_params = {
segment->find_variable(((TokenNode *) args[0])->getName()),
segment->find_variable(((TokenNode *) args[1])->getName()),
}},
});
return;
}
}

args[0]->compile(segment, program, instructions);
args[1]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::Add());
instructions.push_back(new (Instruction){Instruction::Add});

for (int i = 2; i < args.size(); i++) {
args[i]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::Add());
instructions.push_back(new (Instruction){Instruction::Add});
}
}
}// namespace Bytecode::BuiltinFunctions
5 changes: 2 additions & 3 deletions src/bytecode/builtin_functions/AndFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "AndFunction.h"
#include "bytecode/instructions/And.h"
#include "exceptions/SyntaxError.h"

namespace Bytecode::BuiltinFunctions {
Expand All @@ -12,10 +11,10 @@ namespace Bytecode::BuiltinFunctions {
args[0]->compile(segment, program, instructions);
if (args.size() == 1) return;
args[1]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::And());
instructions.push_back(new (Instruction){Instruction::And});
for (int i = 2; i < args.size(); i++) {
args[i]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::And());
instructions.push_back(new (Instruction){Instruction::And});
}
}
}// namespace Bytecode::BuiltinFunctions
6 changes: 2 additions & 4 deletions src/bytecode/builtin_functions/DefineFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include "DefineFunction.h"
#include "bytecode/instructions/Return.h"
#include "bytecode/instructions/StoreGlobal.h"

namespace Bytecode::BuiltinFunctions {
void Define::compile(
Expand All @@ -19,14 +17,14 @@ namespace Bytecode::BuiltinFunctions {
return;
}
args[1]->compile(result, program, instructions);
instructions.push_back(new StoreGlobal(reg));
instructions.push_back(new (Instruction){Instruction::StoreGlobal, {.r_param = {reg}}});
} else if (args[0]->type == SyntaxTreeNode::Expression) {
auto segment = new Segment({});
program.declare_function(((Expression *) args[0])->getName(), segment);
for (auto argument: ((Expression *) args[0])->getArgs())
segment->declare_variable(((TokenNode *) argument)->getName());
args[1]->compile(segment, program, segment->instructions);
segment->instructions.push_back(new Bytecode::Return());
segment->instructions.push_back(new (Instruction){Instruction::Return});
}
}
}// namespace Bytecode::BuiltinFunctions
16 changes: 9 additions & 7 deletions src/bytecode/builtin_functions/DivideFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

#include "DivideFunction.h"
#include "bytecode/instructions/Divide.h"
#include "bytecode/instructions/LoadLiteral.h"
#include "exceptions/SyntaxError.h"

namespace Bytecode::BuiltinFunctions {
Expand All @@ -16,17 +14,21 @@ namespace Bytecode::BuiltinFunctions {
0,
0);
if (args.size() == 1) {
instructions.push_back(new LoadLiteral((double) 1));
args[0]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::Divide());
instructions.push_back(new (Instruction){
Instruction::LoadLiteral,
{.i_param = {StackObject((double) 1)}},
});
args[0]
->compile(segment, program, instructions);
instructions.push_back(new (Instruction){Instruction::Divide});
return;
}
args[0]->compile(segment, program, instructions);
args[1]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::Divide());
instructions.push_back(new (Instruction){Instruction::Divide});
for (int i = 2; i < args.size(); i++) {
args[i]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::Divide());
instructions.push_back(new (Instruction){Instruction::Divide});
}
}
}// namespace Bytecode::BuiltinFunctions
25 changes: 13 additions & 12 deletions src/bytecode/builtin_functions/EqualsFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#include "EqualsFunction.h"
#include "bytecode/instructions/Equals.h"
#include "bytecode/instructions/EqualsRI.h"
#include "bytecode/instructions/LoadLiteral.h"
#include "exceptions/SyntaxError.h"

namespace Bytecode::BuiltinFunctions {

void Equals::compile(
const std::vector<SyntaxTreeNode *> &args,
Program &program,
Expand All @@ -15,28 +11,33 @@ namespace Bytecode::BuiltinFunctions {
throw SyntaxError("Invalid number of arguments for function \"=\", Expected at least 1, got 0");
}
if (args.size() == 1) {
instructions.push_back(new LoadLiteral(new StackObject(true)));
instructions.push_back(new (Instruction){
Instruction::LoadLiteral,
{.i_param = {StackObject(true)}},
});
return;
}
if (args[0]->type == SyntaxTreeNode::TokenNode &&
args[1]->type == SyntaxTreeNode::TokenNode &&
segment->find_variable(((TokenNode *) args[0])->getName()) != -1) {
if (((TokenNode *) args[0])->token.type == Token::Symbol &&
((TokenNode *) args[1])->token.type == Token::Number) {
instructions.push_back(
new Bytecode::EqualsRI(
segment->find_variable(((TokenNode *) args[0])->getName()),
((TokenNode *) args[1])->token.asNumber()));
instructions.push_back(new (Instruction){
Instruction::EqualsRI,
{.ri_params = {
segment->find_variable(((TokenNode *) args[0])->getName()),
StackObject(((TokenNode *) args[1])->token),
}}});
return;
}
}

args[0]->compile(segment, program, instructions);
args[1]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::Equals());
instructions.push_back(new (Instruction){Instruction::Equals});
for (int i = 2; i < args.size(); i++) {
args[i]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::Equals());
instructions.push_back(new (Instruction){Instruction::Equals});
}
}
}
}// namespace Bytecode::BuiltinFunctions
22 changes: 12 additions & 10 deletions src/bytecode/builtin_functions/GreaterThanFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include "GreaterThanFunction.h"
#include "bytecode/instructions/GreaterThan.h"
#include "bytecode/instructions/GreaterThanRI.h"
#include "bytecode/instructions/LoadLiteral.h"
#include "exceptions/SyntaxError.h"

namespace Bytecode::BuiltinFunctions {
Expand All @@ -14,28 +11,33 @@ namespace Bytecode::BuiltinFunctions {
throw SyntaxError("Invalid number of arguments for function \">\", Expected at least 1, got 0");
}
if (args.size() == 1) {
instructions.push_back(new LoadLiteral(new StackObject(true)));
instructions.push_back(new (Instruction){
Instruction::LoadLiteral,
{.i_param = {StackObject(true)}},
});
return;
}
if (args[0]->type == SyntaxTreeNode::TokenNode &&
args[1]->type == SyntaxTreeNode::TokenNode &&
segment->find_variable(((TokenNode *) args[0])->getName()) != -1) {
if (((TokenNode *) args[0])->token.type == Token::Symbol &&
((TokenNode *) args[1])->token.type == Token::Number) {
instructions.push_back(
new Bytecode::GreaterThanRI(
segment->find_variable(((TokenNode *) args[0])->getName()),
((TokenNode *) args[1])->token.asNumber()));
instructions.push_back(new (Instruction){
Instruction::GreaterThanRI,
{.ri_params = {
segment->find_variable(((TokenNode *) args[0])->getName()),
StackObject(((TokenNode *) args[1])->token)}},
});
return;
}
}

args[0]->compile(segment, program, instructions);
args[1]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::GreaterThan());
instructions.push_back(new (Instruction){Instruction::GreaterThan});
for (int i = 2; i < args.size(); i++) {
args[i]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::GreaterThan());
instructions.push_back(new (Instruction){Instruction::GreaterThan});
}
}
}// namespace Bytecode::BuiltinFunctions
17 changes: 9 additions & 8 deletions src/bytecode/builtin_functions/IfFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include "IfFunction.h"
#include "bytecode/instructions/CondJumpIfNot.h"
#include "bytecode/instructions/Jump.h"

namespace Bytecode::BuiltinFunctions {
void If::compile(
Expand All @@ -15,15 +13,18 @@ namespace Bytecode::BuiltinFunctions {
args[1]->compile(result, program, condition_success);
if (args.size() == 3)
args[2]->compile(result, program, condition_failure);

result->instructions.push_back(
new CondJumpIfNot(
result->instructions.size() + condition_success.size() +
(condition_failure.empty() ? 1 : 2)));
result->instructions.push_back(new (Instruction){
Instruction::CondJumpIfNot,
{.r_param = {result->instructions.size() + condition_success.size() +
(condition_failure.empty() ? 1 : 2)}},
});
for (const auto instruction: condition_success)
instructions.push_back(instruction);
if (!condition_failure.empty())
instructions.push_back(new Jump(instructions.size() + condition_failure.size() + 1));
instructions.push_back(new (Instruction) {
Instruction::Jump,
{.r_param = {instructions.size() + condition_failure.size() + 1}},
});

for (const auto instruction: condition_failure)
instructions.push_back(instruction);
Expand Down
26 changes: 14 additions & 12 deletions src/bytecode/builtin_functions/LessThanFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
#include "LessThanFunction.h"
#include "bytecode/instructions/LessThan.h"
#include "bytecode/instructions/LessThanRI.h"
#include "bytecode/instructions/LoadLiteral.h"
#include "exceptions/SyntaxError.h"

namespace Bytecode::BuiltinFunctions {
void LessThan::compile(
const std::vector<SyntaxTreeNode *> &args,
Program &program,
std::vector<Instruction *> &instructions,
Segment *segment) {
Segment *segment) {
if (args.empty()) {
throw SyntaxError("Invalid number of arguments for function \"<\", Expected at least 1, got 0");
}
if (args.size() == 1) {
instructions.push_back(new LoadLiteral(new StackObject(true)));
instructions.push_back(new (Instruction){
Instruction::LoadLiteral,
{.i_param = {StackObject(true)}},
});
return;
}
if (args[0]->type == SyntaxTreeNode::TokenNode &&
args[1]->type == SyntaxTreeNode::TokenNode &&
segment->find_variable(((TokenNode *) args[0])->getName()) != -1) {
if (((TokenNode *) args[0])->token.type == Token::Symbol &&
((TokenNode *) args[1])->token.type == Token::Number) {
instructions.push_back(
new Bytecode::LessThanRI(
segment->find_variable(((TokenNode *) args[0])->getName()),
((TokenNode *) args[1])->token.asNumber()));
instructions.push_back(new (Instruction){
Instruction::LessThanRI,
{.ri_params = {
segment->find_variable(((TokenNode *) args[0])->getName()),
StackObject(((TokenNode *) args[1])->token)}},
});
return;
}
}

args[0]->compile(segment, program, instructions);
args[1]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::LessThan());
instructions.push_back(new (Instruction){Instruction::LessThan});
for (int i = 2; i < args.size(); i++) {
args[i]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::LessThan());
instructions.push_back(new (Instruction){Instruction::LessThan});
}
}
}
}// namespace Bytecode::BuiltinFunctions
Loading

0 comments on commit 6520d36

Please sign in to comment.