Advertisement
sigmasoldier

Ether lib

Jan 2nd, 2024 (edited)
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 30.19 KB | Gaming | 0 0
  1.  
  2. package.preload["result"] = function()
  3. -- Generated by the Saturnus compiler 1.0
  4. -- WARNING! Changes may be discarded at any moment!
  5. local __saturnus_spread__ = table.unpack or unpack; -- Lua polyfill
  6. local trait, impl, abstract;
  7. do
  8.   local __destructure__ = require("std");
  9.   trait = __destructure__.trait;
  10.   impl = __destructure__.impl;
  11.   abstract = __destructure__.abstract;
  12. end
  13. local Result = {};
  14. Result.__meta__ = {};
  15. Result.__meta__.__call = function(self, struct)
  16.   return setmetatable(struct, self.prototype.__meta__);
  17. end;
  18. Result.prototype = {};
  19. Result.prototype.__proto__ = Result;
  20. Result.prototype.__meta__ = {};
  21. Result.prototype.__meta__.__index = Result.prototype;
  22. setmetatable(Result, Result.__meta__);
  23. Result.prototype.unwrap = function(self)
  24.   return 0;
  25. end;
  26. abstract()(Result.prototype.unwrap, "unwrap", Result, "Result", { is_static = false });
  27. Result.prototype.unwrap_err = function(self)
  28.   return 0;
  29. end;
  30. abstract()(Result.prototype.unwrap_err, "unwrap_err", Result, "Result", { is_static = false });
  31. Result.prototype.is_err = function(self)
  32.   return 0;
  33. end;
  34. abstract()(Result.prototype.is_err, "is_err", Result, "Result", { is_static = false });
  35. Result.prototype.is_ok = function(self)
  36.   return 0;
  37. end;
  38. abstract()(Result.prototype.is_ok, "is_ok", Result, "Result", { is_static = false });
  39. trait()(Result, "Result");
  40. local value_symbol = {};
  41. local Ok = {};
  42. Ok.__meta__ = {};
  43. Ok.__meta__.__call = function(self, struct)
  44.   return setmetatable(struct, self.prototype.__meta__);
  45. end;
  46. Ok.prototype = {};
  47. Ok.prototype.__proto__ = Ok;
  48. Ok.prototype.__meta__ = {};
  49. Ok.prototype.__meta__.__index = Ok.prototype;
  50. setmetatable(Ok, Ok.__meta__);
  51. Ok.new = function(value)
  52.   return Ok({[value_symbol] = value});
  53. end;
  54. Ok.prototype.unwrap = function(self)
  55.   return self[value_symbol];
  56. end;
  57. Ok.prototype.unwrap_err = function(self)
  58.   return error("Attempting to unwrap error from Ok value");
  59. end;
  60. Ok.prototype.is_err = function(self)
  61.   return false;
  62. end;
  63. Ok.prototype.is_ok = function(self)
  64.   return true;
  65. end;
  66. impl(Result)(Ok, "Ok");
  67. local Err = {};
  68. Err.__meta__ = {};
  69. Err.__meta__.__call = function(self, struct)
  70.   return setmetatable(struct, self.prototype.__meta__);
  71. end;
  72. Err.prototype = {};
  73. Err.prototype.__proto__ = Err;
  74. Err.prototype.__meta__ = {};
  75. Err.prototype.__meta__.__index = Err.prototype;
  76. setmetatable(Err, Err.__meta__);
  77. Err.new = function(value)
  78.   return Err({[value_symbol] = value});
  79. end;
  80. Err.prototype.unwrap = function(self)
  81.   local e = self[value_symbol];
  82.   print(e);
  83.   if type(e) == "table" and type(e.to_string) == "function" then
  84.     error("Unwrap error! " .. tostring(e.to_string(e)));
  85.   elseif e ~= nil then
  86.     error("Unwrap error! " .. tostring(e));
  87.   end
  88.   error("Attempting to unwrap an Err value");
  89. end;
  90. Err.prototype.unwrap_err = function(self)
  91.   return self[value_symbol];
  92. end;
  93. Err.prototype.is_err = function(self)
  94.   return true;
  95. end;
  96. Err.prototype.is_ok = function(self)
  97.   return false;
  98. end;
  99. impl(Result)(Err, "Err");
  100. return {Ok = Ok.new, Err = Err.new, Result = Result};
  101. end;
  102. package.preload["tokio"] = function()
  103. -- Generated by the Saturnus compiler 1.0
  104. -- WARNING! Changes may be discarded at any moment!
  105. local __saturnus_spread__ = table.unpack or unpack; -- Lua polyfill
  106. local Ok, Err;
  107. do
  108.   local __destructure__ = require("result");
  109.   Ok = __destructure__.Ok;
  110.   Err = __destructure__.Err;
  111. end
  112. local yield = coroutine.yield;
  113. local Promise = {};
  114. Promise.__meta__ = {};
  115. Promise.__meta__.__call = function(self, struct)
  116.   return setmetatable(struct, self.prototype.__meta__);
  117. end;
  118. Promise.prototype = {};
  119. Promise.prototype.__proto__ = Promise;
  120. Promise.prototype.__meta__ = {};
  121. Promise.prototype.__meta__.__index = Promise.prototype;
  122. setmetatable(Promise, Promise.__meta__);
  123. Promise.prototype.resolved = false;
  124. Promise.prototype.rejected = false;
  125. Promise.new = function(handler)
  126.   local promise = Promise({});
  127.   local resolve = function(value)
  128.     promise.resolved = true;
  129.     promise.value = value;
  130.   end;
  131.   local reject = function(value)
  132.     promise.rejected = true;
  133.     promise.error = value;
  134.   end;
  135.   handler(resolve, reject);
  136.   return promise;
  137. end;
  138. Promise.prototype.on_resolve = function(self, after)
  139.   return Promise.new(function(r)
  140.     local res = await(self);
  141.     r(after(res));
  142.   end);
  143. end;
  144. Promise.prototype.await = function(self)
  145.   while true do
  146.     if self.rejected then
  147.       return Err(self.error);
  148.     elseif self.resolved then
  149.       return Ok(self.value);
  150.     end
  151.     yield();
  152.   end
  153. end;
  154. local function count(tbl)
  155.   local i = 0;
  156.   for _ in pairs(tbl) do
  157.     i = i + 1;
  158.   end
  159.   return i;
  160. end
  161. local Ev = {};
  162. Ev.__meta__ = {};
  163. Ev.__meta__.__call = function(self, struct)
  164.   return setmetatable(struct, self.prototype.__meta__);
  165. end;
  166. Ev.prototype = {};
  167. Ev.prototype.__proto__ = Ev;
  168. Ev.prototype.__meta__ = {};
  169. Ev.prototype.__meta__.__index = Ev.prototype;
  170. setmetatable(Ev, Ev.__meta__);
  171. Ev.prototype.should_exit = false;
  172. Ev.new = function()
  173.   return Ev({listener_table = {}, coroutines = {}});
  174. end;
  175. Ev.prototype.exit = function(self, panic_reason)
  176.   self.panic_reason = panic_reason;
  177.   self.should_exit = true;
  178. end;
  179. Ev.prototype.on = function(self, event, handler)
  180.   if self.listener_table[event] == nil then
  181.     self.listener_table[event] = {};
  182.   end
  183.   self.listener_table[event][handler] = true;
  184.   return function()
  185.     return self:off(event, handler);
  186.   end;
  187. end;
  188. Ev.prototype.off = function(self, event, handler)
  189.   if self.listener_table[event] == nil then
  190.     return;
  191.   end
  192.   if not self.listener_table[event][handler] then
  193.     return;
  194.   end
  195.   self.listener_table[event][handler] = nil;
  196.   if count(self.listener_table[event]) <= 0 then
  197.     self.listener_table[event] = nil;
  198.   end
  199. end;
  200. Ev.prototype.once = function(self, event, handler)
  201.   local wrapper;
  202.   wrapper = function(...)
  203.     local args = {...};
  204.     self:off(event, wrapper);
  205.     handler(__saturnus_spread__(args));
  206.   end;
  207.   return self:on(event, wrapper);
  208. end;
  209. Ev.prototype.push = function(self, event, ...)
  210.   local args = {...};
  211.   os.queueEvent(event, __saturnus_spread__(args));
  212. end;
  213. Ev.prototype.run = function(self, target)
  214.   if type(target) == "function" then
  215.     local co = coroutine.create(target);
  216.     self.coroutines[co] = co;
  217.   end
  218.   while true do
  219.     if self.should_exit then
  220.       if self.panic_reason ~= nil then
  221.         error(self.panic_reason);
  222.       else
  223.         return nil;
  224.       end
  225.     end
  226.     local event_info = {os.pullEvent()};
  227.     if count(event_info) > 0 then
  228.       self:_fire_listeners(event_info[1], event_info);
  229.       self:_fire_listeners("*", event_info);
  230.     end
  231.     for co in pairs(self.coroutines) do
  232.       if coroutine.status(co) == "dead" then
  233.         self.coroutines[co] = nil;
  234.       else
  235.         local ok, err;
  236.         do
  237.           local __destructure__ = {coroutine.resume(co)};
  238.           ok = __destructure__[1];
  239.           err = __destructure__[2];
  240.         end
  241.         if not ok then
  242.           self:exit(err);
  243.         end
  244.       end
  245.     end
  246.   end
  247. end;
  248. Ev.prototype._fire_listeners = function(self, target, info)
  249.   if self.listener_table[target] ~= nil then
  250.     for handler in pairs(self.listener_table[target]) do
  251.       handler(__saturnus_spread__(info));
  252.     end
  253.   end
  254. end;
  255. Ev.prototype.launch = function(self, thread)
  256.   local co = coroutine.create(thread);
  257.   self.coroutines[co] = co;
  258. end;
  259. Ev.prototype.timeout = function(self, callback, time)
  260.   time = time == nil and 0 or time;
  261.   local clean;
  262.   local timer_id;
  263.   clean = self:on("timer", function(_, id)
  264.     if id == timer_id then
  265.       clean();
  266.       callback();
  267.     end
  268.   end);
  269.   timer_id = os.startTimer(time);
  270.   return clean;
  271. end;
  272. Ev.prototype.timeout_promise = function(self, time)
  273.   return Promise.new(function(r)
  274.     return self:timeout(r, time);
  275.   end);
  276. end;
  277. local ev = Ev.new();
  278. local function tokio(alt_ev)
  279.   return function(target)
  280.     alt_ev = alt_ev == nil and ev or alt_ev;
  281.     alt_ev:run(target);
  282.   end;
  283. end
  284. return {tokio = tokio, yield = yield, Promise = Promise, Ev = Ev, ev = ev};
  285. end;
  286. package.preload["tokio.result"] = function()
  287. -- Generated by the Saturnus compiler 1.0
  288. -- WARNING! Changes may be discarded at any moment!
  289. local __saturnus_spread__ = table.unpack or unpack; -- Lua polyfill
  290. local trait, impl, abstract;
  291. do
  292.   local __destructure__ = require("std");
  293.   trait = __destructure__.trait;
  294.   impl = __destructure__.impl;
  295.   abstract = __destructure__.abstract;
  296. end
  297. local Result = {};
  298. Result.__meta__ = {};
  299. Result.__meta__.__call = function(self, struct)
  300.   return setmetatable(struct, self.prototype.__meta__);
  301. end;
  302. Result.prototype = {};
  303. Result.prototype.__proto__ = Result;
  304. Result.prototype.__meta__ = {};
  305. Result.prototype.__meta__.__index = Result.prototype;
  306. setmetatable(Result, Result.__meta__);
  307. Result.prototype.unwrap = function(self)
  308.   return 0;
  309. end;
  310. abstract()(Result.prototype.unwrap, "unwrap", Result, "Result", { is_static = false });
  311. Result.prototype.unwrap_err = function(self)
  312.   return 0;
  313. end;
  314. abstract()(Result.prototype.unwrap_err, "unwrap_err", Result, "Result", { is_static = false });
  315. Result.prototype.is_err = function(self)
  316.   return 0;
  317. end;
  318. abstract()(Result.prototype.is_err, "is_err", Result, "Result", { is_static = false });
  319. Result.prototype.is_ok = function(self)
  320.   return 0;
  321. end;
  322. abstract()(Result.prototype.is_ok, "is_ok", Result, "Result", { is_static = false });
  323. trait()(Result, "Result");
  324. local value_symbol = {};
  325. local Ok = {};
  326. Ok.__meta__ = {};
  327. Ok.__meta__.__call = function(self, struct)
  328.   return setmetatable(struct, self.prototype.__meta__);
  329. end;
  330. Ok.prototype = {};
  331. Ok.prototype.__proto__ = Ok;
  332. Ok.prototype.__meta__ = {};
  333. Ok.prototype.__meta__.__index = Ok.prototype;
  334. setmetatable(Ok, Ok.__meta__);
  335. Ok.new = function(value)
  336.   return Ok({[value_symbol] = value});
  337. end;
  338. Ok.prototype.unwrap = function(self)
  339.   return self[value_symbol];
  340. end;
  341. Ok.prototype.unwrap_err = function(self)
  342.   return error("Attempting to unwrap error from Ok value");
  343. end;
  344. Ok.prototype.is_err = function(self)
  345.   return false;
  346. end;
  347. Ok.prototype.is_ok = function(self)
  348.   return true;
  349. end;
  350. impl(Result)(Ok, "Ok");
  351. local Err = {};
  352. Err.__meta__ = {};
  353. Err.__meta__.__call = function(self, struct)
  354.   return setmetatable(struct, self.prototype.__meta__);
  355. end;
  356. Err.prototype = {};
  357. Err.prototype.__proto__ = Err;
  358. Err.prototype.__meta__ = {};
  359. Err.prototype.__meta__.__index = Err.prototype;
  360. setmetatable(Err, Err.__meta__);
  361. Err.new = function(value)
  362.   return Err({[value_symbol] = value});
  363. end;
  364. Err.prototype.unwrap = function(self)
  365.   local e = self[value_symbol];
  366.   print(e);
  367.   if type(e) == "table" and type(e.to_string) == "function" then
  368.     error("Unwrap error! " .. tostring(e.to_string(e)));
  369.   elseif e ~= nil then
  370.     error("Unwrap error! " .. tostring(e));
  371.   end
  372.   error("Attempting to unwrap an Err value");
  373. end;
  374. Err.prototype.unwrap_err = function(self)
  375.   return self[value_symbol];
  376. end;
  377. Err.prototype.is_err = function(self)
  378.   return true;
  379. end;
  380. Err.prototype.is_ok = function(self)
  381.   return false;
  382. end;
  383. impl(Result)(Err, "Err");
  384. return {Ok = Ok.new, Err = Err.new, Result = Result};
  385. end;
  386. _G["__saturnus_module_std"] = function()
  387.   -- Generated by the Saturnus compiler 1.0
  388. -- WARNING! Changes may be discarded at any moment!
  389. local __saturnus_spread__ = table.unpack or unpack; -- Lua polyfill
  390. local function panic(message)
  391.   -- <extern "Lua"> --
  392.     error(message);
  393.   -- </extern> --
  394. end
  395. local private = {};
  396. local Vector = {};
  397. Vector.__meta__ = {};
  398. Vector.__meta__.__call = function(self, struct)
  399.   return setmetatable(struct, self.prototype.__meta__);
  400. end;
  401. Vector.prototype = {};
  402. Vector.prototype.__proto__ = Vector;
  403. Vector.prototype.__meta__ = {};
  404. Vector.prototype.__meta__.__index = Vector.prototype;
  405. setmetatable(Vector, Vector.__meta__);
  406. Vector.new = function(data)
  407.   return Vector({[private] = {data = data}});
  408. end;
  409. Vector.prototype.at = function(self, key)
  410.   return self[private].data[key + 1];
  411. end;
  412. Vector.prototype.set = function(self, key, value)
  413.   self[private].data[key] = value;
  414. end;
  415. Vector.prototype.pop = function(self)
  416.   if self.len() > 0 then
  417.     local value = self[private].data[#self[private].data];
  418.     self[private].data[#self[private].data] = nil;
  419.     return value;
  420.   end
  421.   return nil;
  422. end;
  423. Vector.prototype.push = function(self, value)
  424.   self[private].data[#self[private].data + 1] = value;
  425. end;
  426. Vector.prototype.len = function(self)
  427.   return #self[private].data;
  428. end;
  429. Vector.prototype.iter = function(self)
  430.   return Object.entries(self[private].data);
  431. end;
  432. local Object = {};
  433. Object.__meta__ = {};
  434. Object.__meta__.__call = function(self, struct)
  435.   return setmetatable(struct, self.prototype.__meta__);
  436. end;
  437. Object.prototype = {};
  438. Object.prototype.__proto__ = Object;
  439. Object.prototype.__meta__ = {};
  440. Object.prototype.__meta__.__index = Object.prototype;
  441. setmetatable(Object, Object.__meta__);
  442. Object.entries = function(tbl)
  443.   -- <extern "Lua"> --
  444.       do
  445.         local iter = pairs(tbl);
  446.         return function(_, next)
  447.           local k, v = iter(tbl, next and next._0);
  448.           if k ~= nil and v ~= nil then
  449.             return { _0 = k, _1 = v }, v;
  450.           end
  451.         end;
  452.       end
  453.     -- </extern> --
  454.   panic("Object.entries() not supported on this platform!");
  455. end;
  456. Object.keys = function(tbl)
  457.   -- <extern "Lua"> --
  458.       do
  459.         return pairs(tbl);
  460.       end
  461.     -- </extern> --
  462.   panic("Object.keys() not supported on this platform!");
  463. end;
  464. local Tuple = {};
  465. Tuple.__meta__ = {};
  466. Tuple.__meta__.__call = function(self, struct)
  467.   return setmetatable(struct, self.prototype.__meta__);
  468. end;
  469. Tuple.prototype = {};
  470. Tuple.prototype.__proto__ = Tuple;
  471. Tuple.prototype.__meta__ = {};
  472. Tuple.prototype.__meta__.__index = Tuple.prototype;
  473. setmetatable(Tuple, Tuple.__meta__);
  474. Tuple.new = function(data)
  475.   return Vector({[private] = {data = data}});
  476. end;
  477. Tuple.prototype.at = function(self, key)
  478.   return self[private].data[key];
  479. end;
  480. Tuple.prototype.set = function(self, key, value)
  481.   self[private].data[key] = value;
  482. end;
  483. Tuple.prototype.len = function(self)
  484.   return #self[private].data;
  485. end;
  486. Tuple.prototype.iter = function(self)
  487.   return Object.entries(self[private].data);
  488. end;
  489. local ext = (function()
  490.   local function split(sep)
  491.     return function(self)
  492.       sep = sep == nil and "%s" or sep;
  493.       local t = {};
  494.       for frag in self:gmatch("([^" .. sep .. "]+)") do
  495.         t[#t + 1] = frag;
  496.       end
  497.       return t;
  498.     end;
  499.   end
  500.   local function join(sep)
  501.     return function(self)
  502.       return table.concat(self, sep);
  503.     end;
  504.   end
  505.   local function map(f)
  506.     return function(self)
  507.       local t = {};
  508.       for __destructured_iterator_target__ in Object.entries(self) do
  509.         local k, v;
  510.         do
  511.           local __destructure__ = __destructured_iterator_target__;
  512.           k = __destructure__._0;
  513.           v = __destructure__._1;
  514.         end
  515.         t[k] = f(v, k, self);
  516.       end
  517.       return t;
  518.     end;
  519.   end
  520.   local function reduce(f, seed)
  521.     return function(self)
  522.       for __destructured_iterator_target__ in Object.entries(self) do
  523.         local k, v;
  524.         do
  525.           local __destructure__ = __destructured_iterator_target__;
  526.           k = __destructure__._0;
  527.           v = __destructure__._1;
  528.         end
  529.         seed = f(seed, v, k, self);
  530.       end
  531.       return seed;
  532.     end;
  533.   end
  534.   return {split = split, join = join, map = map, reduce = reduce};
  535. end)();
  536. local rtti = {};
  537. rtti.__meta__ = {};
  538. rtti.__meta__.__call = function(self, struct)
  539.   return setmetatable(struct, self.prototype.__meta__);
  540. end;
  541. rtti.prototype = {};
  542. rtti.prototype.__proto__ = rtti;
  543. rtti.prototype.__meta__ = {};
  544. rtti.prototype.__meta__.__index = rtti.prototype;
  545. setmetatable(rtti, rtti.__meta__);
  546. rtti.arguments = function(...)
  547.   local args = {...};
  548.   return function(target, name)
  549.   end;
  550. end;
  551. local makeTrait = function(name)
  552.   return function()
  553.     return panic("Trait " .. name .. " is not constructible!");
  554.   end;
  555. end;
  556. local function trait()
  557.   return function(target, name)
  558.     target.__meta__.__call = makeTrait(name);
  559.   end;
  560. end
  561. local function impl(trait)
  562.   return function(target, name)
  563.     local map = {};
  564.     for __destructured_iterator_target__ in Object.entries(target.prototype) do
  565.       local k, v;
  566.       do
  567.         local __destructure__ = __destructured_iterator_target__;
  568.         k = __destructure__._0;
  569.         v = __destructure__._1;
  570.       end
  571.       map[k] = v;
  572.     end
  573.     for __destructured_iterator_target__ in Object.entries(trait.prototype) do
  574.       local k, v;
  575.       do
  576.         local __destructure__ = __destructured_iterator_target__;
  577.         k = __destructure__._0;
  578.         v = __destructure__._1;
  579.       end
  580.       if map[k] == nil then
  581.         panic(name .. " must implement trait method " .. k .. "!");
  582.       end
  583.     end
  584.   end;
  585. end
  586. local function mixin(parent)
  587.   local index = parent.prototype.__meta__.__index;
  588.   if type(index) == "table" then
  589.     index = function(self, key)
  590.       return parent.prototype.__meta__.__index[key];
  591.     end;
  592.   end
  593.   return function(target, name)
  594.     local prev = target.prototype.__meta__.__index;
  595.     if type(prev) == "table" then
  596.       local prev_tbl = prev;
  597.       prev = function(self, key)
  598.         return prev_tbl[key];
  599.       end;
  600.     end
  601.     target.prototype.__meta__.__index = function(self, key)
  602.       return prev(self, key) or index(self, key);
  603.     end;
  604.   end;
  605. end
  606. local makePure = function(name)
  607.   return function()
  608.     return panic("Attempting to call abstract method " .. name .. "!");
  609.   end;
  610. end;
  611. local function abstract()
  612.   return function(_, name, host, label, meta)
  613.     if meta.is_static then
  614.       host[name] = makePure(label .. "::" .. name);
  615.     else
  616.       host.prototype[name] = makePure(label .. "." .. name);
  617.     end
  618.   end;
  619. end
  620. local function __saturnus_operator_pipe_greater(left, right)
  621.   return right(left);
  622. end
  623. local function __saturnus_operator_less_pipe(left, right)
  624.   return left(right);
  625. end
  626. local function __saturnus_operator_colon_colon(arr, elem)
  627.   -- <extern "Lua"> --
  628.     do
  629.       table.insert(arr, elem);
  630.       return arr;
  631.     end
  632.   -- </extern> --
  633.   panic("Operator :: not supported on this platform");
  634. end
  635. local function __saturnus_operator_colon_colon_colon(left, right)
  636.   -- <extern "Lua"> --
  637.     do
  638.       local tbl = {};
  639.       for _, v in pairs(left) do
  640.         tbl[#tbl + 1] = v;
  641.       end
  642.       for _, v in pairs(right) do
  643.         tbl[#tbl + 1] = v;
  644.       end
  645.       return tbl;
  646.     end
  647.   -- </extern> --
  648.   panic("Operator ::: not supported on this platform");
  649. end
  650. local function __saturnus_operator_plus_interrogation(left, right)
  651.   return left and (left + right);
  652. end
  653. local function __saturnus_operator_plus_interrogation_interrogation(left, right)
  654.   return left and (left + right) or right;
  655. end
  656. local function __saturnus_operator_plus_plus_interrogation(left, right)
  657.   return left and (left .. right);
  658. end
  659. local function __saturnus_operator_plus_plus_interrogation_interrogation(left, right)
  660.   return left and (left .. right) or right;
  661. end
  662. local function __saturnus_operator_minus_interrogation(left, right)
  663.   return left and (left - right);
  664. end
  665. local function __saturnus_operator_minus_interrogation_interrogation(left, right)
  666.   return left and (left - right) or right;
  667. end
  668. local function __saturnus_operator_times_interrogation(left, right)
  669.   return left and (left * right);
  670. end
  671. local function __saturnus_operator_times_interrogation_interrogation(left, right)
  672.   return left and (left * right) or right;
  673. end
  674. local function __saturnus_operator_slash_interrogation(left, right)
  675.   return left and (left / right);
  676. end
  677. local function __saturnus_operator_slash_interrogation_interrogation(left, right)
  678.   return left and (left / right) or right;
  679. end
  680. local function forward_iterator(from, to, step)
  681.   local i = from;
  682.   return function()
  683.     if i > to then
  684.       return nil;
  685.     end
  686.     local c = i;
  687.     i = i + step;
  688.     return c;
  689.   end;
  690. end
  691. local function backward_iterator(from, to, step)
  692.   local i = from;
  693.   return function()
  694.     if i < to then
  695.       return nil;
  696.     end
  697.     local c = i;
  698.     i = i - step;
  699.     return c;
  700.   end;
  701. end
  702. local function __saturnus_operator_dot_dot(from, target)
  703.   local to, step;
  704.   do
  705.     local __destructure__ = (function()
  706.       if type(target) == "table" then
  707.         return target;
  708.       end
  709.       return {_0 = target, _1 = 1};
  710.     end)();
  711.     to = __destructure__._0;
  712.     step = __destructure__._1;
  713.   end
  714.   if from < to then
  715.     return forward_iterator(from, to, step);
  716.   end
  717.   return backward_iterator(from, to, step);
  718. end
  719. local function __saturnus_operator_minus_greater_greater(self, method)
  720.   return method(self);
  721. end
  722. return {Object = Object, Tuple = Tuple, Vector = Vector, abstract = abstract, trait = trait, impl = impl, mixin = mixin, panic = panic, rtti = rtti, operators = {__saturnus_operator_colon_colon_colon = __saturnus_operator_colon_colon_colon, __saturnus_operator_dot_dot = __saturnus_operator_dot_dot, __saturnus_operator_pipe_greater = __saturnus_operator_pipe_greater, __saturnus_operator_less_pipe = __saturnus_operator_less_pipe, __saturnus_operator_plus_interrogation = __saturnus_operator_plus_interrogation, __saturnus_operator_plus_interrogation_interrogation = __saturnus_operator_plus_interrogation_interrogation, __saturnus_operator_plus_plus_interrogation = __saturnus_operator_plus_plus_interrogation, __saturnus_operator_plus_plus_interrogation_interrogation = __saturnus_operator_plus_plus_interrogation_interrogation, __saturnus_operator_minus_interrogation = __saturnus_operator_minus_interrogation, __saturnus_operator_minus_interrogation_interrogation = __saturnus_operator_minus_interrogation_interrogation, __saturnus_operator_times_interrogation = __saturnus_operator_times_interrogation, __saturnus_operator_times_interrogation_interrogation = __saturnus_operator_times_interrogation_interrogation, __saturnus_operator_slash_interrogation = __saturnus_operator_slash_interrogation, __saturnus_operator_slash_interrogation_interrogation = __saturnus_operator_slash_interrogation_interrogation, __saturnus_operator_colon_colon = __saturnus_operator_colon_colon, __saturnus_operator_minus_greater_greater = __saturnus_operator_minus_greater_greater}, ext = ext};
  723. end;
  724. if jit then
  725.   package.loaded["std"] = _G["__saturnus_module_std"]();
  726. else
  727.   package.preload["std"] = _G["__saturnus_module_std"];
  728. end
  729. -- Generated by the Saturnus compiler 1.0
  730. -- WARNING! Changes may be discarded at any moment!
  731. local __saturnus_spread__ = table.unpack or unpack; -- Lua polyfill
  732. local ev, Promise;
  733. do
  734.   local __destructure__ = require("tokio");
  735.   ev = __destructure__.ev;
  736.   Promise = __destructure__.Promise;
  737. end
  738. local Ok, Err;
  739. do
  740.   local __destructure__ = require("result");
  741.   Ok = __destructure__.Ok;
  742.   Err = __destructure__.Err;
  743. end
  744. local Object;
  745. do
  746.   local __destructure__ = require("std");
  747.   Object = __destructure__.Object;
  748. end
  749. local NoModemError = {};
  750. NoModemError.__meta__ = {};
  751. NoModemError.__meta__.__call = function(self, struct)
  752.   return setmetatable(struct, self.prototype.__meta__);
  753. end;
  754. NoModemError.prototype = {};
  755. NoModemError.prototype.__proto__ = NoModemError;
  756. NoModemError.prototype.__meta__ = {};
  757. NoModemError.prototype.__meta__.__index = NoModemError.prototype;
  758. setmetatable(NoModemError, NoModemError.__meta__);
  759. NoModemError.prototype.to_string = function(self)
  760.   return "No modems attached to this computer!";
  761. end;
  762. local ProtocolAlreadyExistsError = {};
  763. ProtocolAlreadyExistsError.__meta__ = {};
  764. ProtocolAlreadyExistsError.__meta__.__call = function(self, struct)
  765.   return setmetatable(struct, self.prototype.__meta__);
  766. end;
  767. ProtocolAlreadyExistsError.prototype = {};
  768. ProtocolAlreadyExistsError.prototype.__proto__ = ProtocolAlreadyExistsError;
  769. ProtocolAlreadyExistsError.prototype.__meta__ = {};
  770. ProtocolAlreadyExistsError.prototype.__meta__.__index = ProtocolAlreadyExistsError.prototype;
  771. setmetatable(ProtocolAlreadyExistsError, ProtocolAlreadyExistsError.__meta__);
  772. ProtocolAlreadyExistsError.new = function(proto)
  773.   return ProtocolAlreadyExistsError({proto = proto});
  774. end;
  775. ProtocolAlreadyExistsError.prototype.to_string = function(self)
  776.   return "Protocol " .. tostring(self.proto) .. " already registered!";
  777. end;
  778. local ReservedHostNameError = {};
  779. ReservedHostNameError.__meta__ = {};
  780. ReservedHostNameError.__meta__.__call = function(self, struct)
  781.   return setmetatable(struct, self.prototype.__meta__);
  782. end;
  783. ReservedHostNameError.prototype = {};
  784. ReservedHostNameError.prototype.__proto__ = ReservedHostNameError;
  785. ReservedHostNameError.prototype.__meta__ = {};
  786. ReservedHostNameError.prototype.__meta__.__index = ReservedHostNameError.prototype;
  787. setmetatable(ReservedHostNameError, ReservedHostNameError.__meta__);
  788. ReservedHostNameError.new = function(name)
  789.   return ReservedHostNameError({name = name});
  790. end;
  791. ReservedHostNameError.prototype.to_string = function(self)
  792.   return "Host name " .. tostring(self.name) .. " is reserved! Please, choose another.";
  793. end;
  794. local ProtocolDoesNotExistError = {};
  795. ProtocolDoesNotExistError.__meta__ = {};
  796. ProtocolDoesNotExistError.__meta__.__call = function(self, struct)
  797.   return setmetatable(struct, self.prototype.__meta__);
  798. end;
  799. ProtocolDoesNotExistError.prototype = {};
  800. ProtocolDoesNotExistError.prototype.__proto__ = ProtocolDoesNotExistError;
  801. ProtocolDoesNotExistError.prototype.__meta__ = {};
  802. ProtocolDoesNotExistError.prototype.__meta__.__index = ProtocolDoesNotExistError.prototype;
  803. setmetatable(ProtocolDoesNotExistError, ProtocolDoesNotExistError.__meta__);
  804. ProtocolDoesNotExistError.new = function(name)
  805.   return ProtocolDoesNotExistError({name = name});
  806. end;
  807. ProtocolDoesNotExistError.prototype.to_string = function(self)
  808.   return "Protocol " .. tostring(self.name) .. " does not exist on this machine!";
  809. end;
  810. local function find_modem()
  811.   local modem = peripheral.find("modem");
  812.   if modem == nil then
  813.     return Err(NoModemError({}));
  814.   end
  815.   return Ok(modem);
  816. end
  817. local function propagate(what)
  818.   local ok, value;
  819.   do
  820.     local __destructure__ = {pcall(what)};
  821.     ok = __destructure__[1];
  822.     value = __destructure__[2];
  823.   end
  824.   if ok then
  825.     return Ok(value);
  826.   end
  827.   return Err(value);
  828. end
  829. local Ether = {};
  830. Ether.__meta__ = {};
  831. Ether.__meta__.__call = function(self, struct)
  832.   return setmetatable(struct, self.prototype.__meta__);
  833. end;
  834. Ether.prototype = {};
  835. Ether.prototype.__proto__ = Ether;
  836. Ether.prototype.__meta__ = {};
  837. Ether.prototype.__meta__.__index = Ether.prototype;
  838. setmetatable(Ether, Ether.__meta__);
  839. Ether.add_protocol = function(name, options)
  840.   return propagate(function(it, ...)
  841.     local rest = {...};
  842.     local modem = find_modem():unwrap();
  843.     modem.open(Ether.BCAST_PORT);
  844.     options = options == nil and {} or options;
  845.     if Ether.protocols[name] ~= nil then
  846.       return Err(ProtocolAlreadyExistsError.new(name));
  847.     end
  848.     options.private = options.private == nil and false or options.private;
  849.     options.channel = options.channel == nil and Ether.DEFAULT_COM or options.channel;
  850.     modem.open(options.channel);
  851.     Ether.protocols[name] = options;
  852.     return Ok(nil);
  853.   end);
  854. end;
  855. Ether.host = function(name)
  856.   return propagate(function(it, ...)
  857.     local rest = {...};
  858.     local modem = find_modem():unwrap();
  859.     modem.open(Ether.BCAST_PORT);
  860.     if name == "localhost" then
  861.       return Err(ReservedHostNameError.new(name));
  862.     end
  863.     Ether.hostname = name;
  864.     return Ok(ev:on("modem_message", function(_, side, c, rc, message, distance)
  865.       if c == Ether.BCAST_PORT then
  866.         if message.body.request == "lookup" then
  867.           Ether.broadcast({response = "lookup", name = Ether.hostname, protocols = Ether.protocols});
  868.         end
  869.       end
  870.     end));
  871.   end);
  872. end;
  873. Ether.lookup = function(timeout)
  874.   return Promise.new(function(r)
  875.     local hosts = {};
  876.     local clear = ev:on("modem_message", function(_, side, c, rc, message, distance)
  877.       if c == Ether.BCAST_PORT and message.body.response == "lookup" then
  878.         hosts[message.body.name] = message.body.protocols;
  879.       end
  880.     end);
  881.     local res = Ether.broadcast({request = "lookup"});
  882.     if res.is_err() then
  883.       r(res);
  884.     else
  885.       ev:timeout(function()
  886.         clear();
  887.         r(hosts);
  888.       end, timeout == nil and Ether.DEFAULT_LOOKUP_TIMEOUT or timeout);
  889.     end
  890.   end);
  891. end;
  892. Ether.broadcast = function(body)
  893.   return propagate(function(it, ...)
  894.     local rest = {...};
  895.     local modem = find_modem():unwrap();
  896.     local sender_mac = Ether.MAC_ADDRESS;
  897.     local sender = Ether.hostname;
  898.     modem.transmit(Ether.BCAST_PORT, Ether.BCAST_PORT, {sender = sender, sender_mac = sender_mac, body = body});
  899.     return Ok(nil);
  900.   end);
  901. end;
  902. Ether.send = function(target, protocol, body)
  903.   return propagate(function(it, ...)
  904.     local rest = {...};
  905.     local modem = find_modem():unwrap();
  906.     local proto = Ether.protocols[protocol];
  907.     if proto == nil then
  908.       return Err(ProtocolDoesNotExistError.new(protocol));
  909.     end
  910.     local sender_mac = Ether.MAC_ADDRESS;
  911.     local sender = Ether.hostname;
  912.     local payload = {sender = sender, sender_mac = sender_mac, protocol = protocol, target = target, body = body};
  913.     if type(proto.rewrite_transmit) == "function" then
  914.       payload = proto.rewrite_transmit(payload);
  915.     end
  916.     modem.transmit(proto.channel, proto.channel, payload);
  917.     return Ok(nil);
  918.   end);
  919. end;
  920. Ether.on = function(protocol_name, callback)
  921.   return ev:on("modem_message", function(_, side, c, rc, message, distance)
  922.     if protocol_name ~= message.protocol then
  923.       return;
  924.     end
  925.     local protocol = Ether.protocols[message.protocol];
  926.     if protocol == nil then
  927.       return;
  928.     end
  929.     if protocol.channel ~= c then
  930.       return;
  931.     end
  932.     if type(protocol.rewrite_receive) == "function" then
  933.       message = protocol.rewrite_receive(message);
  934.     end
  935.     if message.target ~= Ether.hostname and message.target ~= "*" then
  936.       return;
  937.     end
  938.     callback(message.body, message);
  939.   end);
  940. end;
  941. Ether.ev = ev;
  942. Ether.BCAST_PORT = 255;
  943. Ether.DEFAULT_COM = 254;
  944. Ether.protocols = {};
  945. Ether.DEFAULT_LOOKUP_TIMEOUT = 2;
  946. Ether.MAC_ADDRESS = os.getComputerID();
  947. return {Ether = Ether, NoModemError = NoModemError, ProtocolAlreadyExistsError = ProtocolAlreadyExistsError, ReservedHostNameError = ReservedHostNameError, ProtocolDoesNotExistError = ProtocolDoesNotExistError, find_modem = find_modem};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement