NewLang Project
Yet another programm language
Loading...
Searching...
No Matches
system.cpp
Go to the documentation of this file.
1//#include "pch.h"
2
3#include <sys/sysinfo.h>
4#include <sys/time.h>
5
6#include "warning_push.h"
7#include <torch/torch.h>
8#include <ATen/ATen.h>
9#include "warning_pop.h"
10
11#include <fmt/core.h>
12
13#include "system.h"
14#include "runtime.h"
15
16using namespace newlang;
17using namespace runtime;
18
20ObjPtr CreateTensor(torch::Tensor tensor);
21
22bool Buildin::AddMethod(const char * name, ObjPtr obj) {
23 // if (find(name) != end()) {
24 // LOG_ERROR("Name '%s' already exist!", name);
25 // return false;
26 // }
27 // std::string fullname(m_file);
28 // fullname += "::";
29 // fullname += name;
30 //
31 // TermPtr proto = *const_cast<TermPtr *> (&obj->m_prototype);
32 // proto->m_text = fullname;
33 // proto->m_normalized = NormalizeName(fullname);
34 // // proto->m_obj = obj;
35 //
36 // insert({fullname,
37 // {proto, obj}});
38
39 LOG_RUNTIME("New method '%s'!", name); //fullname.c_str());
40 return true;
41}
42
43bool Buildin::CreateMethodNative(const char * proto, void * addr) {
44 TermPtr term = Parser::ParseTerm(proto, nullptr, false);
45
46 if (!term) {
47 LOG_ERROR("Fail parse '%s'!", proto);
48 return false;
49 }
50 ASSERT(0);
51 // return AddMethod(term->getText().c_str(), m_runtime->CreateNative(term, addr));
52
53}
54
55bool Buildin::CreateMethod(const char * proto, FunctionType & func, ObjType type) {
56 ASSERT(proto);
57
58 TermPtr term = Parser::ParseTerm(proto, nullptr, false);
59
60 if (!term) {
61 LOG_ERROR("Fail parse '%s'!", proto);
62 return false;
63 }
64 ASSERT(0);
65 // return AddMethod(term->getText().c_str(), m_runtime->CreateFunction(term, (void *) &func)); //, type
66}
67
68//bool Buildin::CreateProperty(const char * proto, ObjPtr obj) {
69// return true;
70//}
71
72/*
73 *
74 * :Base ::= :Class() {
75 *
76 * };
77 *
78 */
79CALSS_METHOD(Base, __assert_abort__) {
80
81 // ARG_TEST_COUNT(1);
82 std::string message;
83 for (int i = 1; i < in.size(); i++) {
84 if (in.at(i).second) {
85 message += in.at(i).second->toString();
86 } else {
87 message += "nullptr";
88 }
89 }
90
91 LOG_RUNTIME("Assert abort '%s'!", message.c_str());
92 return Obj::CreateNone();
93}
94
95CALSS_METHOD(Base, __len__) {
96 if (in.size() == 2 && in.at(1).second) {
97 return Obj::CreateValue(in.at(1).second->size());
98 }
99 LOG_RUNTIME("Unknown args '%s'!", in.toString().c_str());
100}
101
102CALSS_METHOD(Base, __timeit__) {
103 uint64_t us = std::chrono::duration_cast<std::chrono::microseconds>(
104 std::chrono::high_resolution_clock::now().time_since_epoch())
105 .count();
106
107 if (in.size() > 1 && in.at(1).second) {
108 uint64_t diff = us - in.at(1).second->GetValueAsInteger();
109 std::string caller = "Exec time";
110 if (in.size() > 2 && in.at(2).second) {
111 caller += " '";
112 caller += in.at(2).second->GetValueAsString();
113 caller += "'";
114 }
115
116 std::string result = fmt::format("{}: {}.{} seconds.", caller, (int) (diff / 1000000), (int) (diff % 1000000));
117
118 for (int i = 3; i < in.size(); i++) {
119 result += in.at(i).second->GetValueAsString();
120 }
121
122 LOG_INFO("%s", result.c_str());
124 }
125 return Obj::CreateValue(us);
126}
127
128CALSS_METHOD(Base, __thread_this_id__) {
129 return Obj::CreateValue(0);//reinterpret_cast<int64_t>(std::this_thread::get_id()));
130}
131
132CALSS_METHOD(Base, __thread_hardware_concurrency__) {
133 return Obj::CreateValue(std::thread::hardware_concurrency());
134}
135
136CALSS_METHOD(Base, __thread_get_id__) {
137 // return Obj::CreateValue(std::thread::us);
138 return Obj::CreateNone();
139}
140
141CALSS_METHOD(Base, __thread_start__) {
142 return Obj::CreateNone();
143}
144
145CALSS_METHOD(Base, __thread_join__) {
146 return Obj::CreateNone();
147}
148
149CALSS_METHOD(Base, __thread_joinable__) {
150 return Obj::CreateBool(false);
151}
152
153/*
154 *
155 * :System ::= :Class() {
156 *
157 * getname():String ::= {%
158 *
159 * char hostname[HOST_NAME_MAX];
160 * ::gethostname(hostname, sizeof (hostname));
161 *
162 * return Obj::CreateString(hostname);
163 *
164 * %}
165 *
166 * getenv(name:String):String ::= {%
167 * ARG_TEST_COUNT(1);
168 * ARG_TEST_TYPE(1, ObjType::String);
169 *
170 * return Obj::CreateString(::getenv(static_cast<const char *>($name)));
171 * %}
172 *
173 * };
174 *
175 */
177
178 char hostname[HOST_NAME_MAX];
179 gethostname(hostname, sizeof (hostname));
180
181 return Obj::CreateString(hostname);
182}
183
185
186 char username[LOGIN_NAME_MAX];
187 getlogin_r(username, sizeof (username));
188
189 return Obj::CreateString(username);
190}
191
192#define ARG_TEST_COUNT(cnt) if (in.size() != cnt + 1) { \
193 LOG_RUNTIME("Only %d argument expected!", cnt); \
194 }
195
196//#define ARG_POS_EXIST(pos) if (in.size() >= pos) { \
197// LOG_RUNTIME("Arguments %d missing!", pos); \
198// }
199
201
203 if (!in.at(1).second || !in.at(1).second->is_string_type()) {
204 LOG_RUNTIME("String type argument expected, not '%s'!", newlang::toString(in.at(1).second->m_var_type_current));
205 }
206
207 return Obj::CreateString(std::getenv(in.at(1).second->GetValueAsString().c_str()));
208}
209
211
212 struct utsname *buf = nullptr;
213 if (0 == uname(buf) && buf) {
214
216
217 result->push_back(Obj::CreateString(buf->sysname), "sysname");
218 result->push_back(Obj::CreateString(buf->nodename), "nodename");
219 result->push_back(Obj::CreateString(buf->release), "release");
220 result->push_back(Obj::CreateString(buf->version), "version");
221 result->push_back(Obj::CreateString(buf->machine), "machine");
222
223 return result;
224 }
225
226 LOG_RUNTIME("Fail call uname()");
227}
228
229CALSS_METHOD(System, getsysinfo) {
230
231 struct sysinfo *info = nullptr;
232 if (0 == sysinfo(info) && info) {
233
235
236 result->push_back(Obj::CreateValue(info->uptime), "uptime");
237 // torch::Tensor ten=torch::from_blob(info->loads, {3}).clone(); //torch::tensor({info->loads[0], info->loads[1], info->loads[2]});//torch::Tensor(info->loads, 3, at::ScalarType::Long);
238 result->push_back(CreateTensor(torch::from_blob(info->loads, {
239 3
240 })), "loads");
241 result->push_back(Obj::CreateValue(info->totalram), "totalram");
242 result->push_back(Obj::CreateValue(info->freeram), "freeram");
243 result->push_back(Obj::CreateValue(info->sharedram), "sharedram");
244 result->push_back(Obj::CreateValue(info->bufferram), "bufferram");
245 result->push_back(Obj::CreateValue(info->totalswap), "totalswap");
246 result->push_back(Obj::CreateValue(info->freeswap), "freeswap");
247 result->push_back(Obj::CreateValue(info->procs), "procs");
248 result->push_back(Obj::CreateValue(info->totalhigh), "totalhigh");
249 result->push_back(Obj::CreateValue(info->freehigh), "freehigh");
250 result->push_back(Obj::CreateValue(info->mem_unit), "mem_unit");
251
252 return result;
253 }
254
255 LOG_RUNTIME("Fail call sysinfo()");
256}
257
258CALSS_METHOD(System, getmillisec) {
259 struct timeval tp;
260 ::gettimeofday(&tp, NULL);
261 return Obj::CreateValue(tp.tv_sec * 1000 + tp.tv_usec / 1000);
262}
263
264CALSS_METHOD(System, gettimeofday) {
265
266 struct timeval tp;
267 ::gettimeofday(&tp, NULL);
268
270 result->push_back(Obj::CreateValue(tp.tv_sec), "tv_sec");
271 result->push_back(Obj::CreateValue(tp.tv_usec), "tv_usec");
272 return result;
273}
274
276 char dir[PATH_MAX];
277 ::getcwd(dir, PATH_MAX);
278 return Obj::CreateString(dir);
279}
280
281
282#if defined(WIN) && (_MSC_VER >= 1900)
283extern "C" __p__environ();
284#else
285extern "C" char ** environ;
286#endif
287
288CALSS_METHOD(System, getenviron) {
289
290 char ** env;
291#if defined(WIN) && (_MSC_VER >= 1900)
292 env = *__p__environ();
293#else
294 env = environ;
295#endif
296
297 std::vector<std::string> split;
299 for (; env && *env; ++env) {
300 split = RunTime::SplitString(*env, "=");
301 result->push_back(Obj::CreateString(&(*env)[split[0].size() + 1]), split[0]);
302 }
303 return result;
304}
305
307
309 if (!in.at(1).second || !in.at(1).second->is_string_type()) {
310 LOG_RUNTIME("String type argument expected, not '%s'!", newlang::toString(in.at(1).second->m_var_type_current));
311 }
312
313 // we use std::array rather than a C-style array to get all the
314 // C++ array convenience functions
315 std::array<char, 128> buffer;
316 std::string result;
317
318 // popen() receives the command and parameter "r" for read,
319 // since we want to read from a stream.
320 // by using unique_ptr, the pipe object is automatically cleaned
321 // from memory once we've read all the data from the pipe.
322 std::unique_ptr<FILE, decltype(&pclose) > pipe(popen(in.at(1).second->GetValueAsString().c_str(), "r"), pclose);
323
324 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
325 result += buffer.data();
326 }
327
329}
330
331//bool Buildin::CreateMacro(const char * text) {
332//
333// // try {
334// // MacroPtr macro;
335// // TermPtr m = Parser::ParseTerm(text, nulltr, false);
336// // } catch (...) {
337// // return false;
338// // }
339// // m_macro_list.push_back(text);
340// if (m_runtime) {
341// TermPtr m = Parser::ParseTerm(text, m_runtime->m_macro, false);
342// }
343// return true;
344//}
345
346Base::Base(RuntimePtr rt) : Buildin(rt, ObjType::Base, "::Base") {
347
348 bool dsl_enable = true;
349 if (rt) {
350 dsl_enable = rt->m_load_dsl;
351 }
352
353 // if (dsl_enable) {
354 //
355 // VERIFY(CreateMacro("@@ true @@ ::= 1"));
356 // VERIFY(CreateMacro("@@ yes @@ ::= 1"));
357 // VERIFY(CreateMacro("@@ false @@ ::= 0"));
358 // VERIFY(CreateMacro("@@ no @@ ::= 0"));
359 //
360 // VERIFY(CreateMacro("@@ if( ... ) @@ ::= @@ [ @$... ] --> @@"));
361 // VERIFY(CreateMacro("@@ elif( ... ) @@ ::= @@ ,[ @$... ] --> @@"));
362 // VERIFY(CreateMacro("@@ else @@ ::= @@ ,[...] --> @@"));
363 //
364 // VERIFY(CreateMacro("@@ while( ... ) @@ ::= @@ [ @$... ] <-> @@"));
365 // VERIFY(CreateMacro("@@ dowhile( ... ) @@ ::= @@ <-> [ @$... ] @@"));
366 // VERIFY(CreateMacro("@@ loop @@ ::= @@ [ 1 ] <-> @@"));
367 //
368 //
369 // VERIFY(CreateMacro("@@ break $label @@ ::= @@ @$label :: ++ @@"));
370 // VERIFY(CreateMacro("@@ continue $label @@ ::= @@ @$label :: -- @@"));
371 // VERIFY(CreateMacro("@@ return( result ) @@ ::= @@ @__FUNC_BLOCK__ ++ @$result ++ @@"));
372 // VERIFY(CreateMacro("@@ throw( result ) @@ ::= @@ -- @$result -- @@"));
373 //
374 // VERIFY(CreateMacro("@@ match( ... ) @@ ::= @@ [ @$... ] @__PRAGMA_EXPECTED__( @\\ =>, @\\ ==>, @\\ ===>, @\\ ~>, @\\ ~~>, @\\ ~~~> ) @@"));
375 // VERIFY(CreateMacro("@@ case( ... ) @@ ::= @@ [ @$... ] --> @@"));
376 // VERIFY(CreateMacro("@@ default @@ ::= @@ [...] --> @@"));
377 //
378 //
379 // VERIFY(CreateMacro("@@ this @@ ::= @@ $0 @@ ##< This object (self)"));
380 // VERIFY(CreateMacro("@@ self @@ ::= @@ $0 @@ ##< This object (self)"));
381 // VERIFY(CreateMacro("@@ super @@ ::= @@ $$ @@ ##< Super (parent) class or function"));
382 // VERIFY(CreateMacro("@@ latter @@ ::= @@ $^ @@ ##< Result of the last operation"));
383 //
384 //
385 // VERIFY(CreateMacro("@@ try @@ ::= @@ [ {* @__PRAGMA_EXPECTED__( @\\ { ) @@"));
386 // VERIFY(CreateMacro("@@ catch(...) @@ ::= @@ *} ] : < @$... > ~> @@"));
387 // VERIFY(CreateMacro("@@ forward @@ ::= @@ +- $^ -+ @@ ##< Forward latter result or exception"));
388 //
389 //
390 // VERIFY(CreateMacro("@@ iter( obj, ... ) @@ ::= @@ @$obj ? (@$...) @@"));
391 // VERIFY(CreateMacro("@@ next( obj, ... ) @@ ::= @@ @$obj ! (@$...) @@"));
392 // VERIFY(CreateMacro("@@ curr( obj ) @@ ::= @@ @$obj !? @@"));
393 // VERIFY(CreateMacro("@@ first( obj ) @@ ::= @@ @$obj !! @@"));
394 // VERIFY(CreateMacro("@@ all( obj ) @@ ::= @@ @$obj ?? @@"));
395 //
396 // VERIFY(CreateMacro("@@ and @@ ::= @@ && @@"));
397 // VERIFY(CreateMacro("@@ or @@ ::= @@ || @@"));
398 // VERIFY(CreateMacro("@@ xor @@ ::= @@ ^^ @@"));
399 // VERIFY(CreateMacro("@@ not(value) @@ ::= @@ (:Bool(@$value)==0) @@"));
400 //
401 // // VERIFY(CreateMacro("@@ root() @@ ::= @@ @# @\\\\ @@"));
402 // // VERIFY(CreateMacro("@@ module() @@ ::= @@ @# $\\\\ @@"));
403 //
404 // // VERIFY(CreateMacro("@@ namespace() @@ ::= @@ @# @:: @@"));
405 // VERIFY(CreateMacro("@@ module() @@ ::= @@ @$$ @@"));
406 // VERIFY(CreateMacro("@@ static @@ ::= @@ @:: @@"));
407 // VERIFY(CreateMacro("@@ package $name @@ ::= @@ @$$ = @# @$name @@"));
408 // VERIFY(CreateMacro("@@ declare( obj ) @@ ::= @@ @$obj ::= ... @@ ##< Forward declaration of the object"));
409 //
410 // VERIFY(CreateMacro("@@ using(...) @@ ::= @@ ... = @$... @@"));
411 //
412 // VERIFY(CreateMacro("@@ typedef(cnt) @@ ::= @@ @__PRAGMA_TYPE_DEFINE__(@$cnt) @@ ##< Disable warning when defining a type inside a namespace"));
413 //
414 // VERIFY(CreateMacro("@@ coroutine @@ ::= @@ __ANNOTATION_SET__(coroutine) @@"));
415 // VERIFY(CreateMacro("@@ co_yield $val @@ ::= @@ __ANNOTATION_CHECK__(coroutine) @__FUNC_BLOCK__ :: -- @$val -- @@"));
416 // VERIFY(CreateMacro("@@ co_await @@ ::= @@ __ANNOTATION_CHECK__(coroutine) @__FUNC_BLOCK__ :: +- @@"));
417 // VERIFY(CreateMacro("@@ co_return $val @@ ::= @@ __ANNOTATION_CHECK__(coroutine) @__FUNC_BLOCK__ :: ++ @$val ++ @@"));
418 //
419 // VERIFY(CreateMacro("@@ exit(code) @@ ::= @@ :: ++ @$code ++ @@"));
420 // VERIFY(CreateMacro("@@ abort() @@ ::= @@ :: -- @@"));
421 //
422 //
423 //
424 // }
425
426
427 bool assert_enable = true;
428 if (rt) {
429 assert_enable = rt->m_assert_enable;
430 }
431 // @assert(value, ...)
432 // @static_assert(value, ...)
433 // @verify(value, ...)
434 //
435 // @__PRAGMA_ASSERT__( is_runtime, is_always, value, val_string, ... )
436 //
437 // @@ static_assert(value, ...) @@ ::= @@ @__PRAGMA_ASSERT__(0, 0, value, @# value, @$... ) @@;
438 // @@ assert(value, ...) @@ ::= @@ @__PRAGMA_ASSERT__(1, 0, value, @# value, @$... ) @@;
439 // @@ verify(value, ...) @@ ::= @@ @__PRAGMA_ASSERT__(1, 1, value, @# value, @$... ) @@;
440
441 // @__PRAGMA_ASSERT__ replase to __pragma_assert__ in @ref Parser::PragmaEval
442 // @__PRAGMA_STATIC_ASSERT__
443 VERIFY(CreateMethod("__assert_abort__(...):None", __assert_abort__));
444
445 // VERIFY(CreateMacro("@@ static_assert(...) @@ ::= @@ @__PRAGMA_STATIC_ASSERT__(@$... ) @@"));
446 //
447 // if (assert_enable) {
448 // VERIFY(CreateMacro("@@ assert(value, ...) @@ ::= @@ [:Bool(@$value)==0]-->{ ::Base::__assert_abort__(@# @$value, @$value, @$... ) } @@"));
449 // VERIFY(CreateMacro("@@ verify(value, ...) @@ ::= @@ [:Bool(@$value)==0]-->{ ::Base::__assert_abort__(@# @$value, @$value, @$... ) } @@"));
450 // } else {
451 // VERIFY(CreateMacro("@@ assert(value, ...) @@ ::= @@ (_) @@"));
452 // VERIFY(CreateMacro("@@ verify(value, ...) @@ ::= @@ (@$value) @@"));
453 // }
454
455
456}
#define CALSS_METHOD(CLASS, NAME)
Definition builtin.h:43
static ObjPtr CreateString(const std::string_view str, Sync *sync=nullptr)
Definition object.h:1596
static ObjPtr CreateDict(Sync *sync=nullptr)
Definition object.h:1625
static ObjPtr CreateBool(bool value, Sync *sync=nullptr)
Definition object.h:1528
static std::enable_if< std::is_same< T, std::string >::value||std::is_same< T, constchar * >::value, ObjPtr >::type CreateValue(T value, Sync *sync=nullptr)
Definition object.h:1562
static ObjPtr CreateNone(Sync *sync=nullptr)
Definition object.h:1510
static size_t ParseTerm(TermPtr &term, const BlockType &buffer, const size_t skip=0, bool pragma_enable=true)
Definition parser.cpp:895
static std::vector< std::string > SplitString(const std::string_view str, const std::string_view delim)
Definition runtime.h:456
static newlang::ObjPtr __assert_abort__(newlang::Context *ctx, newlang::Obj &in)
Definition system.cpp:79
Base(RuntimePtr rt)
Definition system.cpp:346
bool CreateMethodNative(const char *proto, void *addr)
Definition system.cpp:43
bool AddMethod(const char *name, ObjPtr obj)
Definition system.cpp:22
bool CreateMethod(const char *proto, FunctionType &func, ObjType type=ObjType::Function)
Definition system.cpp:55
int result
Definition lexer.l:367
#define LOG_RUNTIME(format,...)
Definition logger.h:26
#define LOG_INFO(...)
Definition logger.h:120
#define VERIFY(exp)
Definition logger.h:65
#define ASSERT(condition)
Definition logger.h:60
#define LOG_ERROR(...)
Definition logger.h:122
Definition nlc.h:59
std::shared_ptr< Term > TermPtr
Definition variable.h:33
std::shared_ptr< Obj > ObjPtr
Definition variable.h:28
std::shared_ptr< RunTime > RuntimePtr
Definition types.h:242
ObjType
Definition types.h:524
const char * toString(TermID type)
Definition term.h:126
ObjPtr FunctionType(Context *ctx, Obj &in)
Definition types.h:248
ObjPtr CreateTensor(torch::Tensor tensor)
Definition object.cpp:52
ObjType getSummaryTensorType(Obj *obj, ObjType start)
Definition object.cpp:546
#define ARG_TEST_COUNT(cnt)
Definition system.cpp:192
ObjPtr CreateTensor(torch::Tensor tensor)
Definition object.cpp:52
char ** environ
Definition system.cpp:285