NewLang Project
Yet another programm language
Loading...
Searching...
No Matches
builtin.cpp
Go to the documentation of this file.
1//#include "pch.h"
2
3//#include <syntax_help_en.cpp>
4
5#include "builtin.h"
6#include "runtime.h"
7
8
9using namespace newlang;
10
11namespace newlang {
12
13// NEWLANG_TRANSPARENT(min) {
14// if(in.size() < 2) {
15// LOG_RUNTIME("Empty argument list parameter!");
16// }
17// ObjPtr out = in.at(1).second;
18// for (int i = 2; i < in.size(); i++) {
19// if(*in.at(i).second < out) {
20// out = in.at(i).second;
21// }
22// }
23// return out;
24// }
25//
26// NEWLANG_TRANSPARENT(max) {
27// if(in.size() < 2) {
28// LOG_RUNTIME("Empty argument list parameter!");
29// }
30// ObjPtr out = in.at(1).second;
31// for (int i = 2; i < in.size(); i++) {
32// if(*in.at(i).second > out) {
33// out = in.at(i).second;
34// }
35// }
36// return out;
37// }
38
40 if(in.size() != 2) {
41 LOG_RUNTIME("Bad argument count parameter!");
42 }
43 return in[1].second->Clone(nullptr);
44 }
45
47 if(in.size() != 2) {
48 LOG_RUNTIME("Bad argument count parameter!");
49 }
50 return in[1].second->MakeConst();
51 }
52
54 if(in.size() != 2) {
55 LOG_RUNTIME("Bad argument count parameter!");
56 }
57 return in[1].second->MakeMutable();
58 }
59
60// NEWLANG_FUNCTION(import) {
61// if(!ctx) {
62// LOG_RUNTIME("No access to context!");
63// }
64// return ctx->CreateNative(in.at(1).second->GetValueAsString().c_str(),
65// in["module"]->GetValueAsString().c_str(), in["lazzy"]->GetValueAsBoolean());
66// }
67//
68// NEWLANG_FUNCTION(eval) {
69// if(!ctx) {
70// LOG_RUNTIME("No access to context!");
71// }
72// return ctx->ExecStr(in.at(1).second->GetValueAsString().c_str(), &in, true);
73// }
74//
75// NEWLANG_FUNCTION(exec) {
76// if(!ctx) {
77// LOG_RUNTIME("No access to context!");
78// }
79// return ctx->ExecFile(in.at(1).second->GetValueAsString().c_str(), &in, true);
80// }
81
82// NEWLANG_TRANSPARENT(help) {
83//
84// std::string help_str;
85// for (int i = 0; i < newlang_syntax_help_en_size; i++) {
86// help_str.append(newlang_syntax_help_en_arr[i]);
87// help_str.append("\n\r");
88// }
89// return Obj::CreateString(help_str);
90// }
91
92
93#undef NEWLANG_FUNCTION
94#undef NEWLANG_TRANSPARENT
95
96}
97
98//bool BuiltInTorchDirect::CheckDirect(CompileInfo &ci, TermPtr &term, std::string &output) {
99//
100// if(term->size() == 0 && m_tensor_noarg.find(term->getText()) != m_tensor_noarg.end()) {
101// output += "->asTensor_()." + term->getText() + "(), " + output;
102//
103// return true;
104// } else if(term->size() == 1) {
105//
106// if((*term)[0].second->IsScalar() && m_tensor_scalar.find(term->getText()) != m_tensor_scalar.end()) {
107// output += "->asTensor_()." + term->getText() + "(" + (*term)[0].second->getText() + "), " + output;
108// return true;
109//
110// } else if((*term)[0].second->getTermID() == TermID::TERM) {
111// std::string temp;
112// NewLang::GetImpl(ci, (*term)[0].second, temp);
113// output += "->asTensor_()." + term->getText() + "(" + temp + "->asTensor_()), " + output;
114// return true;
115// }
116// }
117// return false;
118//}
119
121
122 // Tensor& operator+=(const Tensor & other);
123 // Tensor& operator+=(Scalar other);
124 // Tensor& operator-=(const Tensor & other);
125 // Tensor& operator-=(Scalar other);
126 // Tensor& operator*=(const Tensor & other);
127 // Tensor& operator*=(Scalar other);
128 // Tensor& operator/=(const Tensor & other);
129 // Tensor& operator/=(Scalar other);
130 // Tensor& operator&=(const Tensor & other);
131 // Tensor& operator|=(const Tensor & other);
132 // Tensor& operator^=(const Tensor & other);
133 //
134 // at::Tensor copysign(const at::Tensor & other) const;
135 // at::Tensor & copysign_(const at::Tensor & other) const;
136 // at::Tensor copysign(const at::Scalar & other) const;
137 // at::Tensor & copysign_(const at::Scalar & other) const;
138 // at::Tensor logical_xor(const at::Tensor & other) const;
139 // at::Tensor & logical_xor_(const at::Tensor & other) const;
140 // at::Tensor logical_and(const at::Tensor & other) const;
141 // at::Tensor & logical_and_(const at::Tensor & other) const;
142 // at::Tensor logical_or(const at::Tensor & other) const;
143 // at::Tensor & logical_or_(const at::Tensor & other) const;
144 // at::Tensor bmm(const at::Tensor & mat2) const;
145 // at::Tensor clamp_max(const at::Scalar & max) const;
146 // at::Tensor clamp_max(const at::Tensor & max) const;
147 // at::Tensor & clamp_max_(const at::Scalar & max) const;
148 // at::Tensor & clamp_max_(const at::Tensor & max) const;
149 // at::Tensor clamp_min(const at::Scalar & min) const;
150 // at::Tensor clamp_min(const at::Tensor & min) const;
151 // at::Tensor & clamp_min_(const at::Scalar & min) const;
152 // at::Tensor & clamp_min_(const at::Tensor & min) const;
153 // at::Tensor div(const at::Tensor & other) const;
154 // at::Tensor & div_(const at::Tensor & other) const;
155 // at::Tensor div(const at::Scalar & other) const;
156 // at::Tensor & div_(const at::Scalar & other) const;
157 // at::Tensor divide(const at::Tensor & other) const;
158 // at::Tensor & divide_(const at::Tensor & other) const;
159 // at::Tensor divide(const at::Scalar & other) const;
160 // at::Tensor & divide_(const at::Scalar & other) const;
161 // at::Tensor true_divide(const at::Tensor & other) const;
162 // at::Tensor & true_divide_(const at::Tensor & other) const;
163 // at::Tensor true_divide(const at::Scalar & other) const;
164 // at::Tensor & true_divide_(const at::Scalar & other) const;
165 // at::Tensor & fill_(const at::Scalar & value) const;
166 // at::Tensor & fill_(const at::Tensor & value) const;
167 // at::Tensor floor_divide(const at::Tensor & other) const;
168 // at::Tensor & floor_divide_(const at::Tensor & other) const;
169 // at::Tensor floor_divide(const at::Scalar & other) const;
170 // at::Tensor & floor_divide_(const at::Scalar & other) const;
171 // at::Tensor gcd(const at::Tensor & other) const;
172 // at::Tensor & gcd_(const at::Tensor & other) const;
173 // at::Tensor lcm(const at::Tensor & other) const;
174 // at::Tensor & lcm_(const at::Tensor & other) const;
175 // at::Tensor ldexp(const at::Tensor & other) const;
176 // at::Tensor & ldexp_(const at::Tensor & other) const;
177 // at::Tensor logaddexp(const at::Tensor & other) const;
178 // at::Tensor logaddexp2(const at::Tensor & other) const;
179 // at::Tensor xlogy(const at::Tensor & other) const;
180 // at::Tensor xlogy(const at::Scalar & other) const;
181 // at::Tensor & xlogy_(const at::Tensor & other) const;
182 // at::Tensor & xlogy_(const at::Scalar & other) const;
183 // at::Tensor matmul(const at::Tensor & other) const;
184 // at::Tensor mm(const at::Tensor & mat2) const;
185 // at::Tensor mul(const at::Tensor & other) const;
186 // at::Tensor & mul_(const at::Tensor & other) const;
187 // at::Tensor mul(const at::Scalar & other) const;
188 // at::Tensor & mul_(const at::Scalar & other) const;
189 // at::Tensor multiply(const at::Tensor & other) const;
190 // at::Tensor & multiply_(const at::Tensor & other) const;
191 // at::Tensor multiply(const at::Scalar & other) const;
192 // at::Tensor & multiply_(const at::Scalar & other) const;
193 // at::Tensor mv(const at::Tensor & vec) const;
194 // at::Tensor reshape_as(const at::Tensor & other) const;
195 // at::Tensor smm(const at::Tensor & mat2) const;
196 // at::Tensor flip(at::IntArrayRef dims) const;
197 // at::Tensor type_as(const at::Tensor & other) const;
198 // at::Tensor heaviside(const at::Tensor & values) const;
199 // at::Tensor & heaviside_(const at::Tensor & values) const;
200 // at::Tensor & _coalesced_(bool coalesced) const;
201 // at::Tensor & eq_(const at::Scalar & other) const;
202 // at::Tensor & eq_(const at::Tensor & other) const;
203 // at::Tensor bitwise_and(const at::Scalar & other) const;
204 // at::Tensor bitwise_and(const at::Tensor & other) const;
205 // at::Tensor & bitwise_and_(const at::Scalar & other) const;
206 // at::Tensor & bitwise_and_(const at::Tensor & other) const;
207 // at::Tensor __and__(const at::Scalar & other) const;
208 // at::Tensor __and__(const at::Tensor & other) const;
209 // at::Tensor & __iand__(const at::Scalar & other) const;
210 // at::Tensor & __iand__(const at::Tensor & other) const;
211 // at::Tensor bitwise_or(const at::Scalar & other) const;
212 // at::Tensor bitwise_or(const at::Tensor & other) const;
213 // at::Tensor & bitwise_or_(const at::Scalar & other) const;
214 // at::Tensor & bitwise_or_(const at::Tensor & other) const;
215 // at::Tensor __or__(const at::Scalar & other) const;
216 // at::Tensor __or__(const at::Tensor & other) const;
217 // at::Tensor & __ior__(const at::Scalar & other) const;
218 // at::Tensor & __ior__(const at::Tensor & other) const;
219 // at::Tensor bitwise_xor(const at::Scalar & other) const;
220 // at::Tensor bitwise_xor(const at::Tensor & other) const;
221 // at::Tensor & bitwise_xor_(const at::Scalar & other) const;
222 // at::Tensor & bitwise_xor_(const at::Tensor & other) const;
223 // at::Tensor __xor__(const at::Scalar & other) const;
224 // at::Tensor __xor__(const at::Tensor & other) const;
225 // at::Tensor & __ixor__(const at::Scalar & other) const;
226 // at::Tensor & __ixor__(const at::Tensor & other) const;
227 // at::Tensor __lshift__(const at::Scalar & other) const;
228 // at::Tensor __lshift__(const at::Tensor & other) const;
229 // at::Tensor & __ilshift__(const at::Scalar & other) const;
230 // at::Tensor & __ilshift__(const at::Tensor & other) const;
231 // at::Tensor bitwise_left_shift(const at::Tensor & other) const;
232 // at::Tensor & bitwise_left_shift_(const at::Tensor & other) const;
233 // at::Tensor bitwise_left_shift(const at::Scalar & other) const;
234 // at::Tensor & bitwise_left_shift_(const at::Scalar & other) const;
235 // at::Tensor __rshift__(const at::Scalar & other) const;
236 // at::Tensor __rshift__(const at::Tensor & other) const;
237 // at::Tensor & __irshift__(const at::Scalar & other) const;
238 // at::Tensor & __irshift__(const at::Tensor & other) const;
239 // at::Tensor bitwise_right_shift(const at::Tensor & other) const;
240 // at::Tensor & bitwise_right_shift_(const at::Tensor & other) const;
241 // at::Tensor bitwise_right_shift(const at::Scalar & other) const;
242 // at::Tensor & bitwise_right_shift_(const at::Scalar & other) const;
243 // at::Tensor & tril_(int64_t diagonal=0) const;
244 // at::Tensor & triu_(int64_t diagonal=0) const;
245 // at::Tensor ne(const at::Scalar & other) const;
246 // at::Tensor ne(const at::Tensor & other) const;
247 // at::Tensor & ne_(const at::Scalar & other) const;
248 // at::Tensor & ne_(const at::Tensor & other) const;
249 // at::Tensor not_equal(const at::Scalar & other) const;
250 // at::Tensor not_equal(const at::Tensor & other) const;
251 // at::Tensor & not_equal_(const at::Scalar & other) const;
252 // at::Tensor & not_equal_(const at::Tensor & other) const;
253 // at::Tensor eq(const at::Scalar & other) const;
254 // at::Tensor eq(const at::Tensor & other) const;
255 // at::Tensor ge(const at::Scalar & other) const;
256 // at::Tensor ge(const at::Tensor & other) const;
257 // at::Tensor & ge_(const at::Scalar & other) const;
258 // at::Tensor & ge_(const at::Tensor & other) const;
259 // at::Tensor greater_equal(const at::Scalar & other) const;
260 // at::Tensor greater_equal(const at::Tensor & other) const;
261 // at::Tensor & greater_equal_(const at::Scalar & other) const;
262 // at::Tensor & greater_equal_(const at::Tensor & other) const;
263 // at::Tensor le(const at::Scalar & other) const;
264 // at::Tensor le(const at::Tensor & other) const;
265 // at::Tensor & le_(const at::Scalar & other) const;
266 // at::Tensor & le_(const at::Tensor & other) const;
267 // at::Tensor less_equal(const at::Scalar & other) const;
268 // at::Tensor less_equal(const at::Tensor & other) const;
269 // at::Tensor & less_equal_(const at::Scalar & other) const;
270 // at::Tensor & less_equal_(const at::Tensor & other) const;
271 // at::Tensor gt(const at::Scalar & other) const;
272 // at::Tensor gt(const at::Tensor & other) const;
273 // at::Tensor & gt_(const at::Scalar & other) const;
274 // at::Tensor & gt_(const at::Tensor & other) const;
275 // at::Tensor greater(const at::Scalar & other) const;
276 // at::Tensor greater(const at::Tensor & other) const;
277 // at::Tensor & greater_(const at::Scalar & other) const;
278 // at::Tensor & greater_(const at::Tensor & other) const;
279 // at::Tensor lt(const at::Scalar & other) const;
280 // at::Tensor lt(const at::Tensor & other) const;
281 // at::Tensor & lt_(const at::Scalar & other) const;
282 // at::Tensor & lt_(const at::Tensor & other) const;
283 // at::Tensor less(const at::Scalar & other) const;
284 // at::Tensor less(const at::Tensor & other) const;
285 // at::Tensor & less_(const at::Scalar & other) const;
286 // at::Tensor & less_(const at::Tensor & other) const;
287 // at::Tensor take(const at::Tensor & index) const;
288 // at::Tensor masked_select(const at::Tensor & mask) const;
289 // at::Tensor swapaxes(int64_t axis0, int64_t axis1) const;
290 // at::Tensor & swapaxes_(int64_t axis0, int64_t axis1) const;
291 // at::Tensor swapdims(int64_t dim0, int64_t dim1) const;
292 // at::Tensor & swapdims_(int64_t dim0, int64_t dim1) const;
293 // at::Tensor polygamma(int64_t n) const;
294 // at::Tensor & polygamma_(int64_t n) const;
295 // at::Tensor & atan2_(const at::Tensor & other) const;
296 // at::Tensor atan2(const at::Tensor & other) const;
297 // at::Tensor fmod(const at::Scalar & other) const;
298 // at::Tensor & fmod_(const at::Scalar & other) const;
299 // at::Tensor fmod(const at::Tensor & other) const;
300 // at::Tensor & fmod_(const at::Tensor & other) const;
301 // at::Tensor hypot(const at::Tensor & other) const;
302 // at::Tensor & hypot_(const at::Tensor & other) const;
303 // at::Tensor igamma(const at::Tensor & other) const;
304 // at::Tensor & igamma_(const at::Tensor & other) const;
305 // at::Tensor igammac(const at::Tensor & other) const;
306 // at::Tensor & igammac_(const at::Tensor & other) const;
307 // at::Tensor nextafter(const at::Tensor & other) const;
308 // at::Tensor & nextafter_(const at::Tensor & other) const;
309 // at::Tensor remainder(const at::Scalar & other) const;
310 // at::Tensor & remainder_(const at::Tensor & other) const;
311 // at::Tensor fmin(const at::Tensor & other) const;
312 // at::Tensor fmax(const at::Tensor & other) const;
313 // at::Tensor maximum(const at::Tensor & other) const;
314 // at::Tensor max(const at::Tensor & other) const;
315 // at::Tensor minimum(const at::Tensor & other) const;
316 // at::Tensor min(const at::Tensor & other) const;
317 // bool equal(const at::Tensor & other) const;
318 // at::Tensor pow(const at::Tensor & exponent) const;
319 // at::Tensor & pow_(const at::Scalar & exponent) const;
320 // at::Tensor float_power(const at::Scalar & exponent) const;
321 // at::Tensor & float_power_(const at::Scalar & exponent) const;
322
323 // at::Tensor abs() const;
324 // at::Tensor & abs_() const;
325 // at::Tensor absolute() const;
326 // at::Tensor & absolute_() const;
327 // at::Tensor angle() const;
328 // at::Tensor sgn() const;
329 // at::Tensor & sgn_() const;
330 // at::Tensor _conj() const;
331 // at::Tensor __dispatch_conj() const;
332 // at::Tensor _conj_physical() const;
333 // at::Tensor conj_physical() const;
334 // at::Tensor & conj_physical_() const;
335 // at::Tensor resolve_conj() const;
336 // at::Tensor resolve_neg() const;
337 // at::Tensor _neg_view() const;
338 // at::Tensor acos() const;
339 // at::Tensor & acos_() const;
340 // at::Tensor arccos() const;
341 // at::Tensor & arccos_() const;
342 // at::Tensor acosh() const;
343 // at::Tensor & acosh_() const;
344 // at::Tensor arccosh() const;
345 // at::Tensor & arccosh_() const;
346 // at::Tensor asinh() const;
347 // at::Tensor & asinh_() const;
348 // at::Tensor arcsinh() const;
349 // at::Tensor & arcsinh_() const;
350 // at::Tensor atanh() const;
351 // at::Tensor & atanh_() const;
352 // at::Tensor arctanh() const;
353 // at::Tensor & arctanh_() const;
354 // at::Tensor asin() const;
355 // at::Tensor & asin_() const;
356 // at::Tensor arcsin() const;
357 // at::Tensor & arcsin_() const;
358 // at::Tensor atan() const;
359 // at::Tensor & atan_() const;
360 // at::Tensor arctan() const;
361 // at::Tensor & arctan_() const;
362 // at::Tensor bitwise_not() const;
363 // at::Tensor & bitwise_not_() const;
364 // at::Tensor logical_not() const;
365 // at::Tensor & logical_not_() const;
366 // at::Tensor ceil() const;
367 // at::Tensor & ceil_() const;
368 // at::Tensor cos() const;
369 // at::Tensor & cos_() const;
370 // at::Tensor cosh() const;
371 // at::Tensor & cosh_() const;
372 // at::Tensor erf() const;
373 // at::Tensor & erf_() const;
374 // at::Tensor erfc() const;
375 // at::Tensor & erfc_() const;
376 // at::Tensor exp() const;
377 // at::Tensor & exp_() const;
378 // at::Tensor exp2() const;
379 // at::Tensor & exp2_() const;
380 // at::Tensor expm1() const;
381 // at::Tensor & expm1_() const;
382 // at::Tensor floor() const;
383 // at::Tensor & floor_() const;
384 // at::Tensor frac() const;
385 // at::Tensor & frac_() const;
386 // at::Tensor inverse() const;
387 // at::Tensor isnan() const;
388 // at::Tensor isreal() const;
389 // at::Tensor log() const;
390 // at::Tensor & log_() const;
391 // at::Tensor log10() const;
392 // at::Tensor & log10_() const;
393 // at::Tensor log1p() const;
394 // at::Tensor & log1p_() const;
395 // at::Tensor log2() const;
396 // at::Tensor & log2_() const;
397 // at::Tensor logdet() const;
398 // at::Tensor matrix_exp() const;
399 // at::Tensor median() const;
400 // at::Tensor nanmedian() const;
401 // at::Tensor numpy_T() const;
402 // at::Tensor rad2deg() const;
403 // at::Tensor & rad2deg_() const;
404 // at::Tensor deg2rad() const;
405 // at::Tensor & deg2rad_() const;
406 // at::Tensor ravel() const;
407 m_tensor_noarg = {"any", "all", "msort", "max", "min", "signbit", "sign_", "sign", "i0_", "i0", "erfinv_", "erfinv",
408 "lgamma", "lgamma_", "nonzero", "digamma", "digamma_", "set_", "col_indices", "crow_indices", "values", "_values", "_indices", "indices",
409 "coalesce", "zero_", "positive", "fix_", "fix", "trunc_", "trunc", "flipud", "fliplr", "tanh_", "tanh", "tan_", "tan",
410 "square_", "square", "sqrt_", "sqrt", "detach_", "detach", "sinh_", "sinh", "sinc_", "sinc", "sin_", "sin", "sigmoid_", "sigmoid",
411 "rsqrt_", "rsqrt", "relu_", "relu", "round_", "round", "negative_", "negative", "neg_", "neg", "reciprocal_", "reciprocal"};
412
413 m_tensor_scalar = {"=", "~", "+", "-", "*", "/", "+=", "-=", "*=", "/=",
414 "copysign", "copysign_", "clamp_max", "clamp_max_", "clamp_min_", "clamp_min", "div_", "div", "fill_", "fill"};
415
416 m_tensor_funcs = {"=", "~", "+", "-", "*", "/", "+=", "-=", "*=", "/=", "&=", "|=", "^=",
417 "copysign", "copysign_", "logical_xor_", "logical_xor", "logical_and_", "logical_and", "logical_or_", "logical_or",
418 "clamp_max", "clamp_max_", "clamp_min_", "clamp_min", "div_", "div", "fill_", "fill"};
419
420}
421
422
#define NEWLANG_FUNCTION(NAME)
Definition builtin.h:36
std::set< std::string > m_tensor_scalar
Definition builtin.h:89
std::set< std::string > m_tensor_noarg
Definition builtin.h:87
std::set< std::string > m_tensor_funcs
Definition builtin.h:88
#define LOG_RUNTIME(format,...)
Definition logger.h:26
Definition nlc.h:59
newlang::ObjPtr clone(newlang::Context *ctx, newlang::Obj &in)
Definition builtin.cpp:39
newlang::ObjPtr mutable_(newlang::Context *ctx, newlang::Obj &in)
Definition builtin.cpp:53
newlang::ObjPtr const_(newlang::Context *ctx, newlang::Obj &in)
Definition builtin.cpp:46