Add functions and lists

This commit is contained in:
2025-11-15 13:52:23 +11:00
parent 18fadc18ed
commit c9c00e219d
4 changed files with 283 additions and 8 deletions

View File

@@ -7,10 +7,12 @@
#include <complex>
#include <sstream>
#include <map>
#include <memory>
#include "../libs/linenoise.hpp"
enum class ValueTypes {
Identifier, Int, Double, String, List, Nil
Identifier, Int, Double, String, List, Function, Type, Nil
};
enum class ParserErrorType {
@@ -104,8 +106,19 @@ public:
[[nodiscard]] const std::string& getSourceCode() const { return sourceCode; }
};
class Instruction;
class Function {
public:
ValueTypes returnType = ValueTypes::Nil;
std::vector<std::pair<std::string, ValueTypes>> arguments;
std::vector<Instruction> code;
Function(ValueTypes returnType, std::vector<std::pair<std::string, ValueTypes>> args, std::vector<Instruction> code) : returnType(returnType), arguments(std::move(args)), code(std::move(code)) {}
Function() = default;
};
class Value {
std::variant<int, double, std::string, void*, std::vector<Value>> value;
std::variant<int, double, std::string, void*, std::vector<Value>, Function, ValueTypes> value;
public:
ValueTypes type;
std::optional<int> getInt() {
@@ -132,6 +145,18 @@ class Value {
}
return {};
}
std::optional<Function> getFunction() {
if (std::holds_alternative<Function>(value)) {
return std::get<Function>(value);
}
return {};
}
std::optional<ValueTypes> getType() {
if (std::holds_alternative<ValueTypes>(value)) {
return std::get<ValueTypes>(value);
}
return {};
}
void print() {
switch (type) {
case ValueTypes::String: {
@@ -158,6 +183,15 @@ class Value {
listElement.print();
}
}
case ValueTypes::Function: {
std::cout << "<function>";
break;
}
case ValueTypes::Type: {
std::cout << "<type>";
break;
}
default:
case ValueTypes::Nil: {
std::cout << "\033[2;3;96m" << "nil" << "\033[0m";
}
@@ -211,6 +245,8 @@ class Value {
explicit Value(double in) : value(in), type(ValueTypes::Double) {}
explicit Value(std::string in) : value(in), type(ValueTypes::String) {}
explicit Value(std::vector<Value> in) : value(in), type(ValueTypes::List) {}
explicit Value(Function in) : value(in), type(ValueTypes::Function) {}
explicit Value(ValueTypes in) : value(in), type(ValueTypes::Type) {}
Value(std::string in, bool isIdentifier) : value(in), type(isIdentifier ? ValueTypes::Identifier : ValueTypes::String) {}
};
@@ -258,6 +294,26 @@ class Parser {
return Value();
}
// Also check the types
if (token == "int") {
return Value(ValueTypes::Int);
}
if (token == "double") {
return Value(ValueTypes::Double);
}
if (token == "string") {
return Value(ValueTypes::String);
}
if (token == "list") {
return Value(ValueTypes::List);
}
if (token == "function") {
return Value(ValueTypes::Function);
}
if (token == "type") {
return Value(ValueTypes::Type);
}
// Check if it's a string literal
if (token.front() == '"' && token.back() == '"') {
return Value(token.substr(1, token.length() - 2));
@@ -350,7 +406,7 @@ class Parser {
continue;
}
switch (c) {
case '(': case ')': {
case '(': case ')': case '[': case ']': {
if (!buf.empty()) {
split.push_back(buf);
buf.clear();
@@ -395,12 +451,13 @@ class Parser {
class Interpreter {
public:
std::vector<Instruction> instructions = {};
Value returnValue;
std::map<std::string, Value> environment = {{"pippleVersion", Value("0.0.1")}};
Value interpretInstruction(Instruction& instruction) {
// Preprocess identifiers
for (auto& arg : instruction.args) {
if (std::holds_alternative<Value>(arg) && instruction.instruction != "let" && instruction.instruction != "set") {
if (std::holds_alternative<Value>(arg) && instruction.instruction != "let" && instruction.instruction != "set" && instruction.instruction != "function") {
if (std::get<Value>(arg).type == ValueTypes::Identifier) {
std::string id = std::get<Value>(arg).getString().value();
if (!environment.contains(id)) {
@@ -468,6 +525,64 @@ class Interpreter {
}
return Value();
}
if (instruction.instruction == "function") {
// (function returntype [valtype argname valtype argname] (code) (morecode))
// returnType
if (std::holds_alternative<Instruction>(instruction.args[0])) {
instruction.args[0] = interpretInstruction(std::get<Instruction>(instruction.args[0]));
}
auto returnTypeValue = std::get<Value>(instruction.args[0]);
if (returnTypeValue.type != ValueTypes::Type) {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
ValueTypes returnType = returnTypeValue.getType().value();
std::vector<std::pair<std::string, ValueTypes>> args;
// [valtype argname valtype argname]
int argAmount = 0;
if (std::holds_alternative<Value>(instruction.args[1]) && std::get<Value>(instruction.args[1]).type == ValueTypes::Identifier && std::get<Value>(instruction.args[1]).getString().value() != "[") {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
for (int i = 2; i < instruction.args.size(); i += 2) {
if (std::holds_alternative<Instruction>(instruction.args[i])) {
instruction.args[i] = interpretInstruction(std::get<Instruction>(instruction.args[i]));
}
// this is safe to do because we just evaluated the instruction
Value argTypeVal = std::get<Value>(instruction.args[i]);
if (argTypeVal.type != ValueTypes::Type) {
if (argTypeVal.type == ValueTypes::Identifier && argTypeVal.getString().value() == "]") {
break;
}
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
ValueTypes argType = argTypeVal.getType().value();
// identifiers cannot be returned by anything
if (std::holds_alternative<Instruction>(instruction.args[i + 1])) {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
Value argNameVal = std::get<Value>(instruction.args[i + 1]);
if (argNameVal.type != ValueTypes::Identifier) {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
std::string argName = argNameVal.getString().value();
args.emplace_back(argName, argType);
argAmount += 2;
}
// (code) (morecode)
std::vector<Instruction> body;
for (int i = argAmount + 3; i < instruction.args.size(); i++) {
if (std::holds_alternative<Instruction>(instruction.args[i])) {
body.push_back(std::get<Instruction>(instruction.args[i]));
} else {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
}
return Value(Function(returnType, args, body));
}
// Preprocess instructions inside instructions
for (auto &arg : instruction.args) {
if (std::holds_alternative<Instruction>(arg)) {
@@ -476,6 +591,37 @@ class Interpreter {
}
// Instructions that don't require preprocessing go below this line
// It is safe to use std::get<Value>(...) on all arguments
// First, search for custom functions
if (environment.contains(instruction.instruction)) {
if (environment[instruction.instruction].type == ValueTypes::Function) {
Function function = environment[instruction.instruction].getFunction().value();
if (function.arguments.size() != instruction.args.size()) {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
Interpreter fnInterpreter;
fnInterpreter.environment = this->environment;
for (int i = 0; i < instruction.args.size(); i++) {
Value argTypeVal = std::get<Value>(instruction.args[i]);
if (argTypeVal.type != function.arguments[i].second) {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
fnInterpreter.environment[function.arguments[i].first] = std::get<Value>(instruction.args[i]);
}
Value fnReturnValue;
for (Instruction& inst : function.code) {
if (inst.instruction == "return") {
return fnInterpreter.interpretInstruction(inst);
}
fnReturnValue = fnInterpreter.interpretInstruction(inst);
}
return fnReturnValue;
} else {
return environment[instruction.instruction];
}
return Value();
}
if (instruction.instruction == "+" || instruction.instruction == "add") {
double result = 0;
for (const auto& arg : instruction.args) {
@@ -629,11 +775,81 @@ class Interpreter {
}
return Value(1);
}
if (instruction.instruction == "<" || instruction.instruction == "lesser") {
if (instruction.args.size() != 2) {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
auto left = std::get<Value>(instruction.args[0]);
auto right = std::get<Value>(instruction.args[1]);
double leftVal = 0, rightVal = 0;
if (left.type == ValueTypes::Int) {
leftVal = left.getInt().value();
} else if (left.type == ValueTypes::Double) {
leftVal = left.getDouble().value();
} else {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
if (right.type == ValueTypes::Int) {
rightVal = right.getInt().value();
} else if (right.type == ValueTypes::Double) {
rightVal = right.getDouble().value();
} else {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
return leftVal < rightVal ? Value(1) : Value();
}
if (instruction.instruction == ">" || instruction.instruction == "greater") {
if (instruction.args.size() != 2) {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
auto left = std::get<Value>(instruction.args[0]);
auto right = std::get<Value>(instruction.args[1]);
double leftVal = 0, rightVal = 0;
if (left.type == ValueTypes::Int) {
leftVal = left.getInt().value();
} else if (left.type == ValueTypes::Double) {
leftVal = left.getDouble().value();
} else {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
if (right.type == ValueTypes::Int) {
rightVal = right.getInt().value();
} else if (right.type == ValueTypes::Double) {
rightVal = right.getDouble().value();
} else {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
return leftVal > rightVal ? Value(1) : Value();
}
if (instruction.instruction == "input") {
std::string input;
linenoise::Readline("", input);
return Value(input);
}
if (instruction.instruction == "return") {
if (instruction.args.empty()) {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
return std::get<Value>(instruction.args[0]);
}
if (instruction.instruction == "list") {
std::vector<Value> list;
for (const auto& item : instruction.args) {
list.push_back(std::get<Value>(item));
}
return Value(list);
}
if (instruction.instruction == "let") {
if (instruction.args.size() < 2) {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
@@ -697,7 +913,17 @@ class Interpreter {
}
explicit Interpreter(std::vector<Instruction> in) : instructions(std::move(in)) {
for (Instruction &instruction : instructions) {
interpretInstruction(instruction);
if (instruction.instruction == "return") {
if (instruction.args.empty()) {
throw InterpretingError(InterpreterErrorType::IncorrectTokenType);
}
if (std::holds_alternative<Instruction>(instruction.args[0])) {
instruction.args[0] = interpretInstruction(std::get<Instruction>(instruction.args[0]));
}
returnValue = std::get<Value>(instruction.args[0]);
return;
}
returnValue = interpretInstruction(instruction);
}
}
Interpreter() = default;
@@ -787,4 +1013,4 @@ int main(int argc, char** argv) {
}
}
return 0;
}
}