'standardizing' awful macro usage
[aqualang.git] / src / stdlib.cpp
blob3b1fe4cd00525ce2696579323ba9a49ce1901759
2 #include "private.h"
3 #include "builtin.h"
5 namespace Stdlib
7 void fn_print(Interpreter&, std::vector<Interpreter::Value> &stack)
9 for(size_t i = 1; i<stack.size(); ++i)
11 std::cout << stack[i];
13 stack.clear();
16 void fn_println(Interpreter& ip, std::vector<Interpreter::Value> &stack)
18 fn_print(ip, stack);
19 std::cout << std::endl;
20 stack.clear();
23 void fn_readline(Interpreter&, std::vector<Interpreter::Value> &stack)
25 stack.clear();
26 std::string str;
27 std::getline(std::cin, str);
28 stack.push_back(Interpreter::Value(str));
31 void fn_children(Interpreter&, std::vector<Interpreter::Value> &stack)
33 using Value = Interpreter::Value;
34 if(stack.empty())
36 return;
38 auto arg = stack[1];
39 Value::Type t = arg.type();
40 stack.clear();
41 if(t == Value::Type::Tree || t == Value::Type::Function)
43 auto result = std::make_shared<Array<Interpreter::Value>>();
44 for(size_t i = 0, s = arg.pointer()->size(); i<s; ++i)
46 result->add(Value(Value::Type::Tree, (*arg.pointer())[i]));
48 stack.push_back(result);
52 void fn_tokens(Interpreter&, std::vector<Interpreter::Value> &stack)
54 using Value = Interpreter::Value;
55 if(stack.empty())
57 return;
59 auto arg = stack[1];
60 Value::Type t = arg.type();
61 stack.clear();
62 if(t == Value::Type::Tree || t == Value::Type::Function)
64 auto result = std::make_shared<Array<Interpreter::Value>>();
65 auto node = arg.pointer();
66 for(auto i = node->begin; i!=node->end; ++i)
68 result->add(Value(std::string(i->begin, i->end)));
70 stack.push_back(result);
74 void fn_expand_node(Interpreter&, std::vector<Interpreter::Value> &stack)
76 using Value = Interpreter::Value;
77 if(stack.empty())
79 return;
81 auto arg = stack[1];
82 Value::Type t = arg.type();
83 stack.clear();
84 if(t == Value::Type::Tree || t == Value::Type::Function)
86 auto result = std::make_shared<Array<Interpreter::Value>>();
87 auto &node = *arg.pointer();
88 auto token = node.begin;
89 for(size_t i = 0, s = arg.pointer()->size(); i<s; ++i)
91 for(; token!=node[i]->begin; ++token)
93 result->add(Value(std::string(token->begin, token->end)));
95 result->add(Value(Value::Type::Tree, node[i]));
96 token = node[i]->end;
98 for(; token!=node.end; ++token)
100 result->add(Value(std::string(token->begin, token->end)));
102 stack.push_back(result);
106 void fn_tostring(Interpreter&, std::vector<Interpreter::Value> &stack)
108 using Value = Interpreter::Value;
109 if(stack.empty())
111 return;
113 auto arg = stack[1];
114 stack.clear();
115 switch(arg.type())
117 case Value::Type::Tree:
118 stack.emplace_back(std::string(arg.tree()->begin->begin, (arg.tree()->end-1)->end));
119 return;
120 case Value::Type::Function:
121 stack.emplace_back(std::string(arg.function()->begin->begin, (arg.function()->end-1)->end));
122 return;
123 case Value::Type::Number:
124 stack.emplace_back(std::to_string(arg.number()));
125 return;
126 case Value::Type::String:
127 stack.push_back(arg);
128 return;
129 default:
130 stack.clear();
131 return;
135 void fn_tonumber(Interpreter&, std::vector<Interpreter::Value> &stack)
137 using Value = Interpreter::Value;
138 if(stack.empty())
140 return;
142 auto arg = stack[1];
143 stack.clear();
144 if(arg.type() == Value::Type::String)
146 Value::NumberType result = std::strtod(arg.string().c_str(), nullptr);
147 if(result != HUGE_VAL)
149 stack.emplace_back(result);