-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.h
119 lines (108 loc) · 3.14 KB
/
Program.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once
#include "Forward.h"
#include "Scope.h"
#include "AST.h"
#include "Directory.h"
#include "Object.h"
#include "Terminal.h"
class Program // this is pretty much what Parser is supposed to output, and what Interpreter is supposed to take as input.
{
//Basically stores global functions, perhaps also static methods if we're feeling fancy.
HashTable<std::string, Function*> definedFunctions;
//Methods. Separate from definedFunctions so that they are not in globalscope at runtime.
//Stored as an unordered SET because their base name is not uniquely identifying; there can be a /foo/bar() and /fuck/bar() in the same program.
std::unordered_set<Function*> definedMethods;
Scopelet<Value> globals;
//THE
//
//THE ENTIRE OBJECT TREE (FLATTENED)
HashTable<std::string, ObjectType*> definedObjTypes;
public:
bool is_malformed = false;
Program()
{
//Construct all the native functions
//construct_natives();
}
Program(Function* f)
{
definedFunctions["/main"] = f;
construct_natives();
}
~Program()
{
//Delete object types
for (auto it : definedObjTypes)
{
delete it.second;
}
//Delete AST
for (Function* method : definedMethods)
{
delete method;
}
for (auto it : definedFunctions)
{
delete it.second;
}
}
Program(const Program&) = delete;
Program(Program&& deadprog)
:definedFunctions(deadprog.definedFunctions)
,definedMethods(deadprog.definedMethods)
,definedObjTypes(deadprog.definedObjTypes)
{
deadprog.definedFunctions.clear();
deadprog.definedMethods = {};
deadprog.definedObjTypes.clear();
}
Program& operator=(const Program&) = delete;
Program& operator=(Program&&) = delete;
HashTable<std::string, ObjectType*> construct_natives();
void construct_math_library();
void construct_string_library();
ObjectType* construct_table_library();
ObjectType* construct_file_library();
ObjectType* construct_error_library();
void dump()
{
for (auto it = definedFunctions.begin(); it != definedFunctions.end(); ++it)
{
std::cout << it.value()->dump(0);
}
for (auto it = definedMethods.begin(); it != definedMethods.end(); ++it)
{
std::cout << (*it)->dump(0);
}
}
// I wanna point out that this is distinct from Interpreter's version of this function; it's a raw call to a function's name, directory data and all, while Interpreter's resolves scope first.
Function* get_func(std::string name)
{
if (!definedFunctions.count(name))
return nullptr;
return definedFunctions[name];
}
void set_func(std::string name, Function* f)
{
if (name.find_first_of('/') != std::string::npos)
name = Directory::lastword(name);
#ifndef JOAO_SAFE
if (definedFunctions.count(name))
{
Terminal::SetColor(std::cerr,Terminal::Color::Yellow);
std::cerr << "Warning: " << name << " overridden with alternate definition!";
Terminal::SetColor(std::cerr,Terminal::Color::RESET);
}
#endif
definedFunctions[name] = f;
}
void set_meth(std::string name, Function* f)
{
if (name.find_first_of('/') != std::string::npos)
name = Directory::lastword(name);
definedMethods.insert(f);
}
//FIXME: I don't want Program to have any friends!! >:(
friend class Interpreter;
friend class Parser;
};