grand renaming to 'aqua' (well, 'Aqua' actually)
[aqualang.git] / src / stdlib.cpp
blob6cc03d44ecfab64e9eabb3bef06e598e055986a9
2 #include "private.h"
3 #include "builtin.h"
5 namespace Aqua
7 namespace Stdlib
9 void fn_print(Interpreter&, std::vector<Interpreter::Value> &stack)
11 for(size_t i = 1; i<stack.size(); ++i)
13 std::cout << stack[i];
15 stack.clear();
18 void fn_println(Interpreter& ip, std::vector<Interpreter::Value> &stack)
20 fn_print(ip, stack);
21 std::cout << std::endl;
22 stack.clear();
25 void fn_readline(Interpreter&, std::vector<Interpreter::Value> &stack)
27 stack.clear();
28 std::string str;
29 std::getline(std::cin, str);
30 stack.push_back(Interpreter::Value(str));
33 void fn_children(Interpreter&, std::vector<Interpreter::Value> &stack)
35 using Value = Interpreter::Value;
36 if(stack.empty())
38 return;
40 auto arg = stack[1];
41 Value::Type t = arg.type();
42 stack.clear();
43 if(t == Value::Type::Tree || t == Value::Type::Function)
45 auto result = std::make_shared<Array<Interpreter::Value>>();
46 for(size_t i = 0, s = arg.pointer()->size(); i<s; ++i)
48 result->add(Value(Value::Type::Tree, (*arg.pointer())[i]));
50 stack.push_back(result);
54 void fn_tokens(Interpreter&, std::vector<Interpreter::Value> &stack)
56 using Value = Interpreter::Value;
57 if(stack.empty())
59 return;
61 auto arg = stack[1];
62 Value::Type t = arg.type();
63 stack.clear();
64 if(t == Value::Type::Tree || t == Value::Type::Function)
66 auto result = std::make_shared<Array<Interpreter::Value>>();
67 auto node = arg.pointer();
68 for(auto i = node->begin; i!=node->end; ++i)
70 result->add(Value(std::string(i->begin, i->end)));
72 stack.push_back(result);
76 void fn_expand_node(Interpreter&, std::vector<Interpreter::Value> &stack)
78 using Value = Interpreter::Value;
79 if(stack.empty())
81 return;
83 auto arg = stack[1];
84 Value::Type t = arg.type();
85 stack.clear();
86 if(t == Value::Type::Tree || t == Value::Type::Function)
88 auto result = std::make_shared<Array<Interpreter::Value>>();
89 auto &node = *arg.pointer();
90 auto token = node.begin;
91 for(size_t i = 0, s = arg.pointer()->size(); i<s; ++i)
93 for(; token!=node[i]->begin; ++token)
95 result->add(Value(std::string(token->begin, token->end)));
97 result->add(Value(Value::Type::Tree, node[i]));
98 token = node[i]->end;
100 for(; token!=node.end; ++token)
102 result->add(Value(std::string(token->begin, token->end)));
104 stack.push_back(result);
108 void fn_tostring(Interpreter&, std::vector<Interpreter::Value> &stack)
110 using Value = Interpreter::Value;
111 if(stack.empty())
113 return;
115 auto arg = stack[1];
116 stack.clear();
117 switch(arg.type())
119 case Value::Type::Tree:
120 stack.emplace_back(std::string(arg.tree()->begin->begin, (arg.tree()->end-1)->end));
121 return;
122 case Value::Type::Function:
123 stack.emplace_back(std::string(arg.function()->begin->begin, (arg.function()->end-1)->end));
124 return;
125 case Value::Type::Number:
126 stack.emplace_back(std::to_string(arg.number()));
127 return;
128 case Value::Type::String:
129 stack.push_back(arg);
130 return;
131 default:
132 stack.clear();
133 return;
137 void fn_tonumber(Interpreter&, std::vector<Interpreter::Value> &stack)
139 using Value = Interpreter::Value;
140 if(stack.empty())
142 return;
144 auto arg = stack[1];
145 stack.clear();
146 if(arg.type() == Value::Type::String)
148 Value::NumberType result = std::strtod(arg.string().c_str(), nullptr);
149 if(result != HUGE_VAL)
151 stack.emplace_back(result);