-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.cpp
112 lines (103 loc) · 3.08 KB
/
Function.cpp
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
#include "NewBie_Lang.hpp"
#include "NewBie.hpp"
using namespace zyd2001::NewBie;
using namespace std;
ObjectContainer zyd2001::NewBie::NormalFunction::call(Runner &runner, object_t * o, const Args & args, const ParameterList & plist)
{
for (int i = 0; i < args.size(); i++)
{
if (!plist[i].ref)
runner.addVariable(plist[i].identifier, plist[i].type, args[i], plist[i].cons);
else
runner.addRefVariable(plist[i].identifier, args[i]);
}
if (o != runner.call_stack_func.top().second)
{
useNewBieFunc(shared_from_this(), o);
runner.execute(s);
return runner.returnVal();
}
else
{
runner.execute(s);
return runner.returnVal();
}
}
ObjectContainer zyd2001::NewBie::NativeFunction::call(Runner &runner, object_t * o, const Args & args, const ParameterList & plist)
{
//if (plist[0].identifier != for_functionCall)
// for (int i = 0; i < args.size(); i++)
// {
// bool result = runner.inter->typeCheck(plist[i].type, args[i]->get());
// if (!result)
// throw exception();
// }
if (o != runner.call_stack_func.top().second)
{
useNewBieFunc(shared_from_this(), o);
return native_func(runner, o, args);
}
else
return native_func(runner, o, args);
}
Identifier zyd2001::NewBie::function_t::getName()
{
return name;
}
ObjectContainer zyd2001::NewBie::function_t::call(Runner & runner, object_t * o, const Args & args)
{
newNewBieStack();
newNewBieScope();
ParameterList &plist = runner.argsToParams(args);
if (can_overload)
{
auto f = overload_map.find(plist);
if (f != overload_map.end())
return f->second->call(runner, o, args, f->first);
else
throw exception();
}
else
{
auto f = overload_map.begin(); //need check
return f->second->call(runner, o, args, f->first);
}
}
ObjectContainer zyd2001::NewBie::function_t::call(Runner & runner, object_t * o, const ArgumentList & alist)
{
return call(runner, o, runner.resolveArgumentList(alist));
}
ObjectContainer zyd2001::NewBie::function_t::call_f(Runner & runner, object_t * o, const Args & args)
{
auto & stack_top = runner.call_stack_func.top();
stack_top.second = o;
ParameterList &plist = runner.argsToParams(args);
if (can_overload)
{
auto f = overload_map.find(plist);
if (f != overload_map.end())
{
stack_top.first = f->second;
return f->second->call(runner, o, args, f->first);
}
else
throw exception();
}
else
{
auto f = overload_map.begin(); //need check
stack_top.first = f->second;
return f->second->call(runner, o, args, f->first);
}
}
zyd2001::NewBie::function_t::function_t(Identifier name, init_vec & vec) : name(name)
{
if (vec.empty())
throw exception();
else if (vec.size() == 1)
can_overload = false;
else
can_overload = true;
for (auto & item : vec)
overload_map.emplace(item);
}