implemented rudimentary IO.std{out,err,in} (reusing File.* functions. fun!)
[aqualang.git] / src / ops.member.cpp
blobf702ac33fd87858f80e859e9b6ed2c10c3a61cd7
2 #include <sstream>
3 #include "private.h"
4 #include "builtin.h"
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() << ">";
16 return strm.str();
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;
31 Value table;
32 Value name;
33 table = node.children[0]->accept(*this);
34 name = node.children[1]->accept(*this);
35 if(table.type() == Value::Type::Table)
37 // nop
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);
49 else
51 error(no_such_index(table, name), node);
52 return Value();
54 Value fun = table.table()->get(name);
55 if(fun.type() != Value::Type::Function)
57 error("can not call non-function", node);
58 return Value();
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();
74 return result;
76 else
78 return Value();