Skip to content

Commit

Permalink
refactor: random stuff idk
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Apr 21, 2024
1 parent 14b2b63 commit 42258a1
Show file tree
Hide file tree
Showing 27 changed files with 43 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/bytecode/compiler/Optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bool Bytecode::Optimizer::is_tail_recursive(const Bytecode::Segment &segment, si
void Bytecode::Optimizer::optimize_tail_calls(Segment &segment) {
auto &instructions = segment.instructions;
const size_t number_of_args = instructions[instructions.size() - 2]
->params.ri_params.intermediate.asLambda();
->params.ri_params.intermediate.data.reg;
const size_t old_number_of_instructions = instructions.size() - 1;
instructions.pop_back();// Remove the Return instruction
instructions.pop_back();// Remove the Call instruction
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/Add.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Bytecode {
object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"+\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() + object2.asNumber());
vm.program_stack.push(object1.data.number + object2.data.number);
}
[[nodiscard]] std::string toString() const override { return "Add"; }
};
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/AddRI.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Bytecode {
if (object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"+\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() + object2.asNumber());
vm.program_stack.push(object1.data.number + object2.data.number);
}
[[nodiscard]] std::string toString() const override {
return "AddRI $r" + std::to_string(params.ri_params.reg) +
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/AddRR.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Bytecode {
object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"+\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() + object2.asNumber());
vm.program_stack.push(object1.data.number + object2.data.number);
}
[[nodiscard]] std::string toString() const override {
return "AddRR $r" + std::to_string(params.rr_params.reg1) +
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/And.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Bytecode {
object2.type != ObjectType::Boolean) {
throw SyntaxError("Invalid argument type for function \"and\", Expected boolean, got string");
}
vm.program_stack.push(object1.asBoolean() && object2.asBoolean());
vm.program_stack.push(object1.data.boolean && object2.data.boolean);
}
[[nodiscard]] std::string toString() const override {
return "And";
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/Call.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Bytecode {
void execute(Bytecode::VM &vm) override {
vm.call_stack.newStackFrame(
params.ri_params.reg,
params.ri_params.intermediate.asLambda(),
params.ri_params.intermediate.data.reg,
&vm.program_stack);
}
[[nodiscard]] std::string toString() const override {
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/CallLambda.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Bytecode {
void execute(Bytecode::VM &vm) override {
const auto lambda = vm.program_stack.pop();
vm.call_stack.newStackFrame(
lambda.asLambda(),
lambda.data.reg,
params.r_param.reg,
&vm.program_stack);
}
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/CondJumpIfNot.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Bytecode {
}
void execute(Bytecode::VM &vm) override {
if (const auto cond = vm.program_stack.pop();
!cond.asBoolean())
!cond.data.boolean)
vm.call_stack.stackTop->current_line = params.r_param.reg - 1;
}
[[nodiscard]] std::string toString() const override {
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/Decrement.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Bytecode {
public:
Decrement() { type = InstructionType::Decrement; };
void execute(Bytecode::VM &vm) override {
vm.program_stack.push(vm.program_stack.pop().asNumber() - 1);
vm.program_stack.push(vm.program_stack.pop().data.number - 1);
}
[[nodiscard]] std::string toString() const override { return "Decrement"; }
};
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/DecrementR.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Bytecode {
if (number.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"-\", Expected number, got string");
}
vm.program_stack.push(number.asNumber() - 1);
vm.program_stack.push(number.data.number - 1);
}
[[nodiscard]] std::string toString() const override {
return "DecrementR $r" + std::to_string(params.r_param.reg);
Expand Down
4 changes: 2 additions & 2 deletions src/bytecode/instructions/Divide.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ namespace Bytecode {
object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"/\", Expected number, got string");
}
if (object2.asNumber() == 0) {
if (object2.data.number == 0) {
throw SyntaxError("Division by zero", 0, 0);
}
vm.program_stack.push(object1.asNumber() / object2.asNumber());
vm.program_stack.push(object1.data.number / object2.data.number);
}
[[nodiscard]] std::string toString() const override { return "Divide"; }
};
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/Equals.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Bytecode {
object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"=\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() == object2.asNumber());
vm.program_stack.push(object1.data.number == object2.data.number);
}
[[nodiscard]] std::string toString() const override { return "Equals"; }
};
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/EqualsRI.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Bytecode {
if (object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"=\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() == object2.asNumber());
vm.program_stack.push(object1.data.number == object2.data.number);
}
[[nodiscard]] std::string toString() const override {
return "EqualsRI $r" + std::to_string(params.ri_params.reg) +
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/GreaterThan.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Bytecode {
object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \">\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() > object2.asNumber());
vm.program_stack.push(object1.data.number > object2.data.number);
}
[[nodiscard]] std::string toString() const override { return "GreaterThan"; }
};
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/GreaterThanRI.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Bytecode {
if (object1.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \">\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() > object2.asNumber());
vm.program_stack.push(object1.data.number > object2.data.number);
}
[[nodiscard]] std::string toString() const override {
return "GreaterThanRI $r" + std::to_string(params.ri_params.reg) +
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/Increment.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Bytecode {
public:
Increment() { type = InstructionType::Increment; };
void execute(Bytecode::VM &vm) override {
vm.program_stack.push(vm.program_stack.pop().asNumber() + 1);
vm.program_stack.push(vm.program_stack.pop().data.number + 1);
}
[[nodiscard]] std::string toString() const override { return "Increment"; }
};
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/LessThan.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Bytecode {
object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"<\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() < object2.asNumber());
vm.program_stack.push(object1.data.number < object2.data.number);
}
[[nodiscard]] std::string toString() const override { return "LessThan"; }
};
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/LessThanRI.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Bytecode {
if (object1.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"<\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() < object2.asNumber());
vm.program_stack.push(object1.data.number < object2.data.number);
}
[[nodiscard]] std::string toString() const override {
return "LessThanRI $r" + std::to_string(params.ri_params.reg) +
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/Multiply.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Bytecode {
object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"*\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() * object2.asNumber());
vm.program_stack.push(object1.data.number * object2.data.number);
}
[[nodiscard]] std::string toString() const override { return "Multiply"; }
};
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/MultiplyRI.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Bytecode {
if (object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"*\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() * object2.asNumber());
vm.program_stack.push(object1.data.number * object2.data.number);
}
[[nodiscard]] std::string toString() const override {
return "MultiplyRI $r" + std::to_string(params.ri_params.reg) +
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/Not.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Bytecode {
if (object.type != ObjectType::Boolean)
vm.program_stack.push(false);
else
vm.program_stack.push(!object.asBoolean());
vm.program_stack.push(!object.data.boolean);
}
[[nodiscard]] std::string toString() const override { return "Not"; }
};
Expand Down
4 changes: 2 additions & 2 deletions src/bytecode/instructions/Or.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
namespace Bytecode {
class Or final : public Instruction {
public:
Or() {type = InstructionType::Or;}
Or() { type = InstructionType::Or; }
void execute(Bytecode::VM &vm) override {
const auto object2 = vm.program_stack.pop();
const auto object1 = vm.program_stack.pop();
if (object1.type != ObjectType::Boolean ||
object2.type != ObjectType::Boolean) {
throw SyntaxError("Invalid argument type for function \"or\", Expected boolean, got string");
}
vm.program_stack.push(object1.asBoolean() || object2.asBoolean());
vm.program_stack.push(object1.data.boolean || object2.data.boolean);
}
[[nodiscard]] std::string toString() const override {
return "Or";
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/instructions/Subtract.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Bytecode {
object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"-\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() - object2.asNumber());
vm.program_stack.push(object1.data.number - object2.data.number);
}
[[nodiscard]] std::string toString() const override { return "Subtract"; }
};
Expand Down
7 changes: 3 additions & 4 deletions src/bytecode/instructions/SubtractRI.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ namespace Bytecode {
type = InstructionType::SubtractRI;
}
void execute(VM &vm) override {
const auto object2 =params.ri_params.intermediate;
const auto object2 = params.ri_params.intermediate;
const auto object1 = vm.call_stack.getLocal(params.ri_params.reg);
if (object1.type != ObjectType::Number ||
object2.type != ObjectType::Number) {
if (object1.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"-\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() - object2.asNumber());
vm.program_stack.push(object1.data.number - object2.data.number);
}
[[nodiscard]] std::string toString() const override {
return "SubtractRI $r" + std::to_string(params.ri_params.reg) +
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/vm/CallStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace Bytecode {
return local_registers[local_registers_used - stackTop->args + reg];
}

void setLocal(const size_t reg, const StackObject sObject) const {
void setLocal(const size_t reg, const StackObject &sObject) const {
local_registers[local_registers_used - stackTop->args + reg] = sObject;
}

Expand Down
8 changes: 4 additions & 4 deletions src/bytecode/vm/Stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ namespace Bytecode {
void push(const StackObject &object) {
switch (object.type) {
case ObjectType::Boolean:
push(object.asBoolean());
push(object.data.boolean);
break;
case ObjectType::Number:
push(object.asNumber());
push(object.data.number);
break;
case ObjectType::String:
push(object.asString());
push(std::string(object.data.string));
break;
case ObjectType::Lambda:
push(object.asLambda());
push(object.data.reg);
break;
}
}
Expand Down
42 changes: 10 additions & 32 deletions src/bytecode/vm/StackObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,24 @@ namespace Bytecode {
Lambda,
};

class StackObject {
struct StackObject {
uint8_t type;
union Data {
double number;
size_t reg;
char *string;
bool boolean;
};
Data data;

public:
uint8_t type;
} data;

explicit StackObject() = default;
explicit StackObject(const size_t segment)
: type(Lambda) { data.reg = segment; };
: type(Lambda), data({.reg = segment}){};
explicit StackObject(const double number)
: data({number}), type(Number){};
explicit StackObject(const bool boolean)
: type(Boolean) { data.boolean = boolean; };
: type(Boolean), data({.boolean = boolean}) {};
explicit StackObject(const std::string &str)
: type(String) {
// TODO: This is a dumb workaround, fix it later
data.string = strdup(str.c_str());
};
: type(String), data({.string = strdup(str.c_str())}){};
explicit StackObject(const Token &token) {
switch (token.type) {
case Token::Number:
Expand All @@ -63,38 +57,22 @@ namespace Bytecode {
}
}

[[nodiscard]] std::string asString() const {
return data.string;
}

[[nodiscard]] double asNumber() const {
return data.number;
}

[[nodiscard]] bool asBoolean() const {
return data.boolean;
}

[[nodiscard]] size_t asLambda() const {
return data.reg;
}

[[nodiscard]] std::string toString() const {
switch (type) {
case Boolean:
return asBoolean() ? std::string("#true")
: std::string("#false");
return data.boolean ? std::string("#true")
: std::string("#false");
case Number: {
std::ostringstream s;
s.precision(std::numeric_limits<double>::digits10);
s << std::fixed << asNumber();
s << std::fixed << data.number;
std::string str = s.str();
str.erase(str.find_last_not_of('0') + 1);
str.erase(str.find_last_not_of('.') + 1);
return str;
}
case String: {
std::string string = asString();
std::string string = data.string;
return string[0] == '"' && string.back() == '"'
? string.substr(1, string.size() - 2)
: string;
Expand Down

0 comments on commit 42258a1

Please sign in to comment.