-
Notifications
You must be signed in to change notification settings - Fork 17
/
Function.h
130 lines (98 loc) · 3.48 KB
/
Function.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
#pragma once
#include <vector>
#include "Instruction.h"
#include "Value.h"
class State;
class Closure;
class Stack;
class InstructionValue : public Value
{
public:
InstructionValue();
InstructionValue(InstructionSet* is) : InstructionValue() { _insSet = is; }
virtual std::string Name() const { return "instruction_val"; }
virtual int Type() const { return TYPE_INSTRUCTVAL; }
virtual std::size_t GetHash() const { return std::hash<const InstructionValue *>()(this); }
virtual bool IsEqual(const Value *other) const { return this == other; }
public:
InstructionSet* getInstructionSet() { return _insSet; }
void setInstructionSet(InstructionSet* s){ _insSet = s; }
void clearInsSet();
void setParent(InstructionValue* v) { _parentInsVal = v; }
InstructionValue* getParent() { return _parentInsVal; }
void setFor(bool b) { _hasFor = b; }
bool getFor() { return _hasFor; }
void setBreaked(bool b) { _breaked = b; }
bool getBreaked() { return _breaked; }
private:
InstructionSet* _insSet;
InstructionValue* _parentInsVal;
bool _hasFor;
bool _breaked;
};
class Function : public InstructionValue
{
public:
Function();
~Function();
public:
virtual std::string Name() const { return "function"; }
virtual int Type() const { return TYPE_FUNCTION; }
virtual std::size_t GetHash() const { return std::hash<const Function *>()(this);}
virtual bool IsEqual(const Value *other) const { return this == other; }
Closure* generateClosure(State* s);
};
class Closure : public Value
{
public:
Closure(State*);
Function * getPrototype() const { return _prototype; }
void setPrototype(Function *prototype) { _prototype = prototype; }
State* getState() { return _state; }
virtual std::string Name() const { return "closure"; }
virtual int Type() const { return TYPE_CLOSURE; }
virtual std::size_t GetHash() const { return _prototype->GetHash(); }
virtual bool IsEqual(const Value *other) const { return this == other; }
void setParentClosure(Closure* c);
Closure* getParentClosure() { return _parentClosure; }
Table* getTopTable();
Table* getLevelTable(unsigned int i);
int findUpTables(Value* key, Value** val, Table** table);
void initClosure();
void clearClosure();
void balanceStack();
Closure* clone();
void addBlockTable();
void removeBlockTable();
void setActRetNum(int n) { _actRetNum = n; }
int getRealRetNum() { return _actRetNum; }
void setNeedRetNum(int n) { _needRetNum = n; }
int getNeedRetNum() { return _needRetNum; }
void setActParamNum(int n) { _actParamNum = n; }
int getActParamNum() { return _actParamNum; }
private:
int findInNestTables(Value* key, Value** val);
private:
int _needRetNum; //函数调用者需要的返回值个数,例如f()为0个,f() + 1 就为1个
int _actRetNum; //函数执行时确定的返回值个数,这个也是动态的,比如在if语句中
int _actParamNum; //函数执行时实际传入的参数个数
State* _state;
Function* _prototype;
Closure* _parentClosure;
typedef std::vector<Table *> NestTables;
NestTables _nest_tables; //一个函数中可能有多个嵌套block
Table* _upTables;
};
class NativeFunc : public Value
{
public:
typedef int(*Fun)(State*, void*);
NativeFunc(Fun f) { _func = f; }
virtual std::string Name() const { return "native_function"; }
virtual int Type() const { return TYPE_NATIVE_FUNCTION; }
virtual std::size_t GetHash() const { return std::hash<const NativeFunc *>()(this); }
virtual bool IsEqual(const Value *other) const { return this == other; }
int doCall(State* s, void* p) { return _func(s, p); }
private:
Fun _func;
};