Replaced create_environment with constructor
This commit is contained in:
parent
f84b631124
commit
4c2afd779d
5 changed files with 4 additions and 9 deletions
|
@ -35,7 +35,7 @@ void interpret_file(char* fileName) {
|
||||||
|
|
||||||
// Interpret the AST
|
// Interpret the AST
|
||||||
// print_tree(result, 0); // For debugging
|
// print_tree(result, 0); // For debugging
|
||||||
struct Environment* env = create_environment();
|
struct Environment* env = new Environment();
|
||||||
eval_statement(result, env);
|
eval_statement(result, env);
|
||||||
delete_environment(env);
|
delete_environment(env);
|
||||||
delete_tree(result);
|
delete_tree(result);
|
||||||
|
|
|
@ -136,7 +136,7 @@ struct Value* eval_expression(struct Node* node, struct Environment* env) {
|
||||||
case CALLFUNC:
|
case CALLFUNC:
|
||||||
check_num_nodes(node, 2, "cannot have more than two nodes for a function call.");
|
check_num_nodes(node, 2, "cannot have more than two nodes for a function call.");
|
||||||
tempNode = get_expression(get_value(find_variable(env, node->children[0]->id)));
|
tempNode = get_expression(get_value(find_variable(env, node->children[0]->id)));
|
||||||
local_env = create_environment();
|
local_env = new Environment();
|
||||||
add_variable(local_env,
|
add_variable(local_env,
|
||||||
make_variable(tempNode->children[0]->id, // Get the name of the variable needed for the lambda expression
|
make_variable(tempNode->children[0]->id, // Get the name of the variable needed for the lambda expression
|
||||||
eval_expression(node->children[1], env)));
|
eval_expression(node->children[1], env)));
|
||||||
|
|
|
@ -48,7 +48,7 @@ void start_shell() {
|
||||||
printf("Welcome to SLOTH Version 0.0.1\n");
|
printf("Welcome to SLOTH Version 0.0.1\n");
|
||||||
printf("Press CTRL+C to Exit\n");
|
printf("Press CTRL+C to Exit\n");
|
||||||
|
|
||||||
struct Environment* env = create_environment();
|
struct Environment* env = new Environment();
|
||||||
while (1) {
|
while (1) {
|
||||||
// Read line from user and input it into the history
|
// Read line from user and input it into the history
|
||||||
char* input = readline("sloth> ");
|
char* input = readline("sloth> ");
|
||||||
|
|
|
@ -4,11 +4,6 @@
|
||||||
#include "environment.hpp"
|
#include "environment.hpp"
|
||||||
#include "variable.hpp"
|
#include "variable.hpp"
|
||||||
|
|
||||||
struct Environment* create_environment(void) {
|
|
||||||
struct Environment* env = new Environment();
|
|
||||||
return env;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Variable* find_variable(struct Environment* env, std::string id) {
|
struct Variable* find_variable(struct Environment* env, std::string id) {
|
||||||
auto result = std::find_if(env->vars.begin(), env->vars.end(),
|
auto result = std::find_if(env->vars.begin(), env->vars.end(),
|
||||||
[id](const Variable* element) {
|
[id](const Variable* element) {
|
||||||
|
|
|
@ -6,10 +6,10 @@
|
||||||
|
|
||||||
struct Environment {
|
struct Environment {
|
||||||
std::vector<struct Variable*> vars;
|
std::vector<struct Variable*> vars;
|
||||||
|
Environment() { }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Variable Lookup Functions
|
// Variable Lookup Functions
|
||||||
struct Environment* create_environment(void);
|
|
||||||
void delete_environment(struct Environment* env);
|
void delete_environment(struct Environment* env);
|
||||||
struct Variable* find_variable(struct Environment* env, std::string id);
|
struct Variable* find_variable(struct Environment* env, std::string id);
|
||||||
void add_variable(struct Environment* env, struct Variable* var);
|
void add_variable(struct Environment* env, struct Variable* var);
|
||||||
|
|
Reference in a new issue