-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionCall.h
113 lines (90 loc) · 2.51 KB
/
FunctionCall.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
#ifndef FUNCTIONCALL_H
#define FUNCTIONCALL_H
#include <memory>
#include <vector>
#include <fstream>
#include <unistd.h>
#include "Expression.h"
class FunctionCall : public Expression
{
std::wstring funcName;
std::vector< std::shared_ptr<Expression> > params;
public:
FunctionCall(std::wstring funcName, std::vector< std::shared_ptr<Expression> > params) : funcName(funcName), params(params)
{
}
virtual picojson::value asJson(std::shared_ptr<Substitution> subs, bool forConditionSection) const
{
std::map<std::wstring, picojson::value> obj;
// TODO: gotta tidy this up into a table of intrinsics
if (funcName.compare(L"In::RefName") == 0)
{
if (params.size() != 1)
{
std::wcerr << "In::RefName only takes one parameter: a variable which ultimately references a resource" << std::endl;
exit(1);
}
if (params[0]->getForm() == SYMBOL_REFERENCE)
{
std::wstring str = params[0]->asJson(subs).get<picojson::value::object>()[L"Ref"].get<std::wstring>();
return picojson::value(str);
}
std::wcerr << "In::RefName variable does not reference a resource!" << std::endl;
exit(1);
}
if (funcName.compare(L"In::GetFile") == 0)
{
if (params.size() != 1)
{
std::wcerr << "In::GetFile only takes one parameter: a file name" << std::endl;
exit(1);
}
if (params[0]->getForm() == STRING_LITERAL)
{
std::wstring filename = params[0]->asJson(subs).get<std::wstring>();
std::string fn(filename.begin(), filename.end());
std::wifstream t(fn);
std::wstring content((std::istreambuf_iterator<wchar_t>(t)), std::istreambuf_iterator<wchar_t>());
return picojson::value(content);
}
std::wcerr << "In::GetFile variable takes a string (a filename)!" << std::endl;
exit(1);
}
// all other functions
if (params.size() > 1)
{
std::vector<picojson::value> parmArr;
for (const std::shared_ptr<Expression>& p : params)
{
parmArr.push_back(p->asJson(subs));
}
obj[funcName] = picojson::value(parmArr);
}
else
{
obj[funcName] = picojson::value((*params.begin())->asJson(subs));
}
return picojson::value(obj);
}
virtual ExpressionForm getForm() const
{
return FUNCTION_CALL;
}
virtual std::wstring getType(std::shared_ptr<Substitution> subs) const
{
if (funcName.compare(L"In::GetFile") == 0)
{
return L"string";
}
if (funcName.compare(L"Fn::Join") == 0)
{
return L"string";
}
if (funcName.compare(L"In::RefName") == 0)
{
return L"string";
}
return L"string";
}
};
#endif // MEMBERCALL_H