-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreter.h
172 lines (142 loc) · 4.39 KB
/
Interpreter.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Copyright 2010 by Benjamin J. Land (a.k.a. BenLand100)
*
* This file is part of CPascal.
*
* CPascal is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CPascal is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CPascal. If not, see <http://www.gnu.org/licenses/>.
*/
class Container;
class Program;
class Method;
class Interpreter;
class Frame;
#ifndef _INTERPRETER_H
#define _INTERPRETER_H
#include "lexer.h"
#include "Value.h"
#include "Expression.h"
#include "Exceptions.h"
#include "Variable.h"
#include <vector>
#include <map>
#define CONV_INTERNAL 0
#define CONV_C_STDCALL 1
#define CONV_FPC_STDCALL 2
typedef enum { METHOD, VARIABLE, ARGUMENT, CONSTANT, TYPE } local_type;
typedef struct {
local_type localtype;
union { Method *meth; Variable *var; Value *val; Type *type; };
int index;
} local;
class Container {
public:
friend class Frame;
const int name;
Block block;
virtual bool addMethod(Method* meth);
virtual bool addVariable(Variable* var);
virtual bool addConst(int name, Value* c);
virtual bool addType(int name, Type* type);
Method* getMethod(int name);
Variable* getVariable(int name);
Value* getConst(int name);
Type* getType(int name);
int getNameSlot(int name);
virtual Type* getSlotType(int slot);
//E.G. a generic container
Container(int name, Container *super_scope);
virtual ~Container();
protected:
local* resolve(int name);
Container *super_scope;
int slot_start; //length of super_scope at time of creation
int local_start;
std::map<int,local*> scope_map;
std::vector<Variable*> locals;
};
class Program : public Container {
public:
friend class Frame;
Program(int name, Container *const_scope);
~Program();
};
class Method : public Container {
public:
friend class Frame;
bool setResultType(Type *type);
bool addArgument(Variable *arg);
Variable* getArg(int idx);
unsigned int numArgs();
Type* getResultType();
int getResultSlot();
virtual Type* getSlotType(int slot);
void* address;
int mtype;
Meth* val_type;
Method(int name, Container *super_scope);
~Method();
protected:
Type* type; //DO NOT FREE TYPES (they don't leak, they are managed by the Type class);
int res_idx;
std::vector<Variable*> arguments;
};
class Interpreter : public Container {
friend class Frame;
public:
Interpreter(PreCompiler_Callback precomp, ErrorHandeler_Callback err);
~Interpreter();
bool run();
bool compile();
void setScript(char *ppg);
virtual void addMethod(void* addr, int conv, char* def);
virtual void addType(char* def);
private:
PreCompiler_Callback precomp;
ErrorHandeler_Callback err;
InterpEx* exception;
Program* prog;
char* ppg;
std::map<std::string,int> names;
void handle(InterpEx *ex);
void handle(InterpEx *ex, char* ppg);
};
class Frame {
friend class MethodValue;
friend class PointerValue;
public:
Frame();
Frame(Program *container);
Frame(Frame* frame, Method* container, Value **args);
~Frame();
Value* resolve(int symbol) throw(int,InterpEx*);
private:
int numslots;
int localstart;
int localcount;
Value** slots;
Frame* parent;
Container* container;
void init(Container* container) throw(int,InterpEx*);
};
extern "C" {
void interp_mem(MALLOC malloc, REALLOC realloc, FREE free) __attribute__((cdecl));
void* interp_init(PreCompiler_Callback precomp, ErrorHandeler_Callback err) __attribute__((cdecl));
void interp_meth(void* interp, void* addr, char* def) __attribute__((cdecl));
void interp_type(void* interp, char* def) __attribute__((cdecl));
void interp_set(void* interp, char *ppg) __attribute__((cdecl));
bool interp_run(void* interp) __attribute__((cdecl));
bool interp_comp(void* interp) __attribute__((cdecl));
void interp_free(void* interp) __attribute__((cdecl));
}
#endif /* _INTERPRETER_H */