grand renaming to 'aqua' (well, 'Aqua' actually)
[aqualang.git] / src / ops.import.cpp
blob1b44c067e36e906f035d37ef1981aadf65c9acd5
2 #include "private.h"
4 namespace Aqua
6 using namespace Internal;
8 template<typename Node>
9 static Interpreter::Value handle_import(Interpreter& ip, Node& node, Interpreter::Value& expr)
11 bool failed;
12 std::stringstream errstrm;
13 auto modpath = ip.getModulePath();
14 failed = true;
15 for(auto path: modpath)
17 std::stringstream filepath;
18 std::fstream fh;
19 filepath << path << "/" << expr.string() << ".aqm";
20 fh.open(filepath.str(), std::ios::in);
21 if(fh.good())
23 std::string filedata(
24 (std::istreambuf_iterator<char>(fh)),
25 std::istreambuf_iterator<char>()
27 failed = false;
28 return ip.eval(filedata);
31 if(failed)
33 errstrm << "could not import '" << expr.string() << "' (path: [";
34 for(auto iter=modpath.begin(); iter!=modpath.end(); iter++)
36 errstrm << "\"" << *iter << "\"";
37 if((iter + 1) != modpath.end())
39 errstrm << ", ";
42 errstrm << "]";
43 ip.error(errstrm.str(), node);
45 return Interpreter::Value();
48 template<typename Node>
49 static Interpreter::Value import_main(Interpreter& ip, Node& node, Interpreter::Value& expr)
51 std::stringstream errstrm;
52 switch(expr.type())
54 case Interpreter::Value::Type::String:
55 return handle_import(ip, node, expr);
56 /*case Interpreter::Value::Type::Array:
57 for(auto& val: expr.array()->get())
59 return import_main(ip);
62 default:
63 break;
65 errstrm << "'import' expects a string, got " << expr.typeName() << "instead";
66 ip.error(errstrm.str(), node);
67 return Interpreter::Value();
70 Interpreter::Value Interpreter::operator()(DoImport<Iter,Interpreter::Value,FuncType> &node)
72 Interpreter::Value expr;
73 expr = node.children[0]->accept(*this);
74 std::cerr << "import: args = " << expr << std::endl;
75 return import_main(*this, node, expr);
76 return Interpreter::Value();