Everywhere: Initial implementation of step3
This commit is contained in:
+17
-120
@@ -6,146 +6,43 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint> // int64_t
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "error.h"
|
||||
#include "ruc/format/color.h"
|
||||
#include "ruc/singleton.h"
|
||||
|
||||
#include "ast.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace blaze {
|
||||
|
||||
class Environment;
|
||||
typedef std::shared_ptr<Environment> EnvironmentPtr;
|
||||
|
||||
class Environment {
|
||||
public:
|
||||
Environment() = default;
|
||||
Environment(EnvironmentPtr outer);
|
||||
virtual ~Environment() = default;
|
||||
|
||||
ASTNodePtr lookup(const std::string& symbol)
|
||||
{
|
||||
m_current_key = symbol;
|
||||
return m_values.find(symbol) != m_values.end() ? m_values[symbol] : nullptr;
|
||||
}
|
||||
bool exists(const std::string& symbol);
|
||||
ASTNodePtr set(const std::string& symbol, ASTNodePtr value);
|
||||
ASTNodePtr get(const std::string& symbol);
|
||||
|
||||
protected:
|
||||
std::string m_current_key;
|
||||
std::unordered_map<std::string, ASTNodePtr> m_values;
|
||||
EnvironmentPtr m_outer { nullptr };
|
||||
};
|
||||
|
||||
class GlobalEnvironment final : public Environment {
|
||||
public:
|
||||
GlobalEnvironment()
|
||||
{
|
||||
// TODO: Add more native functions
|
||||
// TODO: Move the functions to their own file
|
||||
auto add = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
int64_t result = 0;
|
||||
|
||||
for (auto node : nodes) {
|
||||
if (!is<Number>(node.get())) {
|
||||
Error::the().addError(format("wrong type argument: number-or-marker-p, '{}'", node));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
result += static_pointer_cast<Number>(node)->number();
|
||||
}
|
||||
|
||||
return makePtr<Number>(result);
|
||||
};
|
||||
|
||||
auto sub = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
int64_t result = 0;
|
||||
|
||||
if (nodes.size() == 0) {
|
||||
return makePtr<Number>(0);
|
||||
}
|
||||
|
||||
for (auto node : nodes) {
|
||||
if (!is<Number>(node.get())) {
|
||||
Error::the().addError(format("wrong type argument: number-or-marker-p, '{}'", node));
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Start with the first number
|
||||
result += static_pointer_cast<Number>(nodes[0])->number();
|
||||
|
||||
// Skip the first node
|
||||
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) {
|
||||
result -= static_pointer_cast<Number>(*it)->number();
|
||||
}
|
||||
|
||||
return makePtr<Number>(result);
|
||||
};
|
||||
|
||||
auto mul = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
int64_t result = 1;
|
||||
|
||||
for (auto node : nodes) {
|
||||
if (!is<Number>(node.get())) {
|
||||
Error::the().addError(format("wrong type argument: number-or-marker-p, '{}'", node));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
result *= static_pointer_cast<Number>(node)->number();
|
||||
}
|
||||
|
||||
return makePtr<Number>(result);
|
||||
};
|
||||
|
||||
auto div = [this](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
double result = 0;
|
||||
|
||||
if (nodes.size() == 0) {
|
||||
Error::the().addError(format("wrong number of arguments: {}, 0", m_current_key));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (auto node : nodes) {
|
||||
if (!is<Number>(node.get())) {
|
||||
Error::the().addError(format("wrong type argument: number-or-marker-p, '{}'", node));
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Start with the first number
|
||||
result += static_pointer_cast<Number>(nodes[0])->number();
|
||||
|
||||
// Skip the first node
|
||||
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) {
|
||||
result /= static_pointer_cast<Number>(*it)->number();
|
||||
}
|
||||
|
||||
return makePtr<Number>((int64_t)result);
|
||||
};
|
||||
|
||||
m_values.emplace("+", makePtr<Function>(add));
|
||||
m_values.emplace("-", makePtr<Function>(sub));
|
||||
m_values.emplace("*", makePtr<Function>(mul));
|
||||
m_values.emplace("/", makePtr<Function>(div));
|
||||
}
|
||||
GlobalEnvironment();
|
||||
virtual ~GlobalEnvironment() = default;
|
||||
|
||||
private:
|
||||
// TODO: Add more native functions
|
||||
void add();
|
||||
void sub();
|
||||
void mul();
|
||||
void div();
|
||||
};
|
||||
|
||||
} // namespace blaze
|
||||
|
||||
// associative data structure that maps symbols (the keys) to values
|
||||
|
||||
// values = anything, including other symbols.
|
||||
// an environment is like a hash table
|
||||
|
||||
// value can map to:
|
||||
// list
|
||||
// vector
|
||||
// hash-map
|
||||
// symbol
|
||||
// number
|
||||
// string
|
||||
// function
|
||||
|
||||
Reference in New Issue
Block a user