5 using namespace Internal
;
8 using Value
= Interpreter::Value
;
10 // this function is dangerously close to turning really awfully hacky!
12 static std::string
no_such_index(Value
& self
, Value
& name
)
14 std::stringstream strm
;
15 strm
<< "cannot find index '" << name
<< "' for type <" << self
.typeName() << ">";
19 template<typename NodeType
>
20 static void getFunctionArgs(Interpreter
& ip
, NodeType
& node
, std::vector
<Value
>& args
)
22 for(size_t i
= 2; i
<node
.children
.size(); i
++)
24 args
.push_back(node
.children
[i
]->accept(ip
));
28 Interpreter::Value
Interpreter::operator()(MemberCall
<Iter
,Interpreter::Value
,FuncType
> &node
)
30 std::vector
<Value
> args
;
33 table
= node
.children
[0]->accept(*this);
34 name
= node
.children
[1]->accept(*this);
35 if(table
.type() == Value::Type::Table
)
39 else if((table
.type() == Value::Type::String
) && BUILTIN_EXISTS(string
, name
.string()))
41 getFunctionArgs(*this, node
, args
);
42 return BUILTIN_GET(string
, name
.string())(*this, table
, args
);
44 else if((table
.type() == Value::Type::Array
) && BUILTIN_EXISTS(array
, name
.string()))
46 getFunctionArgs(*this, node
, args
);
47 return BUILTIN_GET(array
, name
.string())(*this, table
,args
);
51 error(no_such_index(table
, name
), node
);
54 Value fun
= table
.table()->get(name
);
55 if(fun
.type() != Value::Type::Function
)
57 error("can not call non-function", node
);
60 auto function
= fun
.function();
61 getFunctionArgs(*this, node
, args
);
62 m_function_stack
.push_back(table
);
63 for(size_t i
= 2; i
<node
.children
.size(); ++i
)
65 m_function_stack
.push_back(args
[i
-2]);
67 FunctionScope
functionscope(this);
68 function
->children
[0]->accept(*this);
69 function
->children
[1]->accept(*this);
70 if(!m_function_stack
.empty())
72 Value result
= m_function_stack
.back();
73 m_function_stack
.clear();