-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcharon-runtime.lua
More file actions
887 lines (782 loc) · 20.1 KB
/
charon-runtime.lua
File metadata and controls
887 lines (782 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
--[[
MIT License
Copyright (c) 2020 Pablo Blanco Celdrán
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
--[[
Charon language standard library runtime.
]]
local charon = {}
local function throw(msg, ...)
error(tostring(msg) .. "\n" .. debug.traceback(), ...);
end
------- Types References -------
-- Unit type
local Unit = setmetatable({}, {
__name = 'Unit',
__tostring = function() return 'Unit'; end,
__concat = strcat,
__call = function() throw 'Attempting to call unit value!'; end
})
charon.True = true
charon.False = false
-- Charon symbol type, like :hello or :world
local Symbol = {
__name = 'Symbol',
__tostring = function(self) return ':' .. self.value; end,
__concat = strcat,
__call = function(self) throw('Attempting to call symbol ' .. tostring(self) .. '!'); end
};
local symbols = {};
local Atom = {
__name = 'Atom',
__tostring = function(self)
return 'atom{' .. tostring(self.value) .. '}';
end,
__concat = strcat,
__call = function(self) throw('Attempting to call atom ' .. tostring(self) .. '!'); end
}
local List = {
__name = 'List',
__tostring = function(self)
local list = ''
for i=1, (#self - 1) do
list = list .. tostring(self[i]) .. ' ';
end
return '[ ' .. list .. tostring(self[#self]) .. ' ]'
end,
__eq = function(self, b)
if type(b) == 'table' and getmetatable(b) == List then
for k, v in pairs(b) do
if self[k] ~= v then
return false;
end
end
return true;
end
return self == b;
end,
__concat = strcat,
__call = function(self) throw('Attempting to call list ' .. tostring(self) .. '!'); end
}
-- Table metatable
local Table = {
__name = 'Table',
__tostring = function(self)
local paired = '';
for k, v in pairs(self) do
paired = paired .. tostring(k) .. ' ' .. tostring(v) .. ' ';
end
return '{ ' .. paired .. '}';
end,
__concat = strcat,
__call = function(self) throw('Attempting to call table ' .. tostring(self) .. '!'); end
}
------- Runtime Functions -------
local function strcat(this, other) return tostring(this) .. other; end
-- Simple private streaming function.
-- Future runtime versions will come with streaming APIs.
local function stream_of(str)
local i = 1;
return {
next = function(self)
local poked = self:peek();
i = i + 1;
return poked;
end,
peek = function(self)
return str:sub(i, i);
end
};
end
local crd_parse_any;
local function crd_parse_table(stream)
stream:next();
local data = charon.table{};
local odd = false;
local key = nil;
while stream:peek() ~= '}' do
if odd then
data[key] = crd_parse_any(stream);
else
key = crd_parse_any(stream);
end
odd = not odd;
end
return data;
end
local function crd_parse_list(stream)
stream:next();
local data = charon.list{};
while stream:peek() ~= ']' do
data[#data + 1] = crd_parse_any(stream);
end
return data;
end
local function crd_parse_string(stream)
stream:next();
local escaped = false;
local str = '';
while stream:peek() ~= '"' and not escaped do
local next = stream:next();
if next == '\\' then
escaped = true;
else
str = str .. next;
end
end
return str;
end
crd_parse_any = function(stream)
local token = stream:peek();
if token == '{' then
return crd_parse_table(stream);
elseif token == '[' then
return crd_parse_list(stream);
elseif token == '"' then
return crd_parse_string(stream);
elseif token:match('[\n \t\r]') then
stream:next();
elseif token == nil then
throw('Unexpected end of input');
else
throw('Unexpected token ' .. tostring(token));
end
end
--[[
This function decodes CRD data on runtime.
]]
function charon.crd_decode(str)
local stream = stream_of(str);
return crd_parse_any(stream);
end
--[[
This function decodes a CRD file on runtime.
]]
function charon.crd_decode_file(str)
assert(type(str) == 'string', 'Expected string, instead saw ' .. charon.type(str));
local file = io.open(str, 'r');
local data = nil;
if file ~= nil then
data = file:read('*all');
file:close();
else
return Unit;
end
return charon.decode_crd(data);
end
-- Symbol constructor.
function charon.symbol(value)
if symbols[value] ~= nil then return symbols[value]; end
local symbol = setmetatable({ value = value }, Symbol);
symbols[value] = symbol;
return symbol;
end
--[[
Type function.
Returns a type annotation tag, which can be used for runtime checks.
]]
function charon.type(value)
if value.__name == 'Unit' then
return charon.symbol'unit';
end
local type = type(value);
if type == 'number' then
return charon.symbol'number';
elseif type == 'string' then
return charon.symbol'string';
elseif type == 'boolean' then
return charon.symbol'boolean';
elseif type == 'table' then
local meta = getmetatable(value);
if meta.__name == 'List' then
return charon.symbol'list';
elseif meta.__name == 'Table' then
return charon.symbol'table';
elseif meta.__name == 'Symbol' then
return charon.symbol'symbol';
elseif meta.__name == 'Atom' then
return charon.symbol'atom';
elseif meta.__name == 'Unit' then
return charon.symbol'unit';
else
return charon.symbol'object';
end
elseif type == 'nil' then
return charon.symbol'nothing';
else
throw'Unexpected type found.';
end
end
-- True if value was of type string.
function charon.is_string(value)
return charon.type(value) == charon.symbol'string';
end
-- True if value was of type number.
function charon.is_number(value)
return charon.type(value) == charon.symbol'number';
end
-- True if value was of type boolean.
function charon.is_boolean(value)
return charon.type(value) == charon.symbol'boolean';
end
-- True if value was of type list.
function charon.is_list(value)
return charon.type(value) == charon.symbol'list';
end
-- True if value was of type table.
function charon.is_table(value)
return charon.type(value) == charon.symbol'table';
end
-- True if value was of type symbol.
function charon.is_symbol(value)
return charon.type(value) == charon.symbol'symbol';
end
-- True if value was of type atom.
function charon.is_atom(value)
return charon.type(value) == charon.symbol'atom';
end
-- True if value was of type object.
function charon.is_object(value)
return charon.type(value) == charon.symbol'object';
end
function charon.is(value, symbol)
return charon.type(value) == symbol;
end
function charon.some(value)
return value ~= nil and value ~= Unit;
end
function charon.in_args(value, ...)
for _, v in pairs{...} do
if value == v then
return true;
end
end
return false;
end
function charon.is_nothing(value)
return value == nil;
end
function charon.is_unit(value)
return value == Unit;
end
function charon.isnt_nothing(value)
return value ~= nil;
end
function charon.isnt_unit(value)
return value ~= Unit;
end
function charon.atom(value)
return setmetatable({ value = value }, Atom);
end
function charon.list(tbl)
return setmetatable(tbl, List)
end
local EQ = function(a, b) return a == b; end
function charon.list_find(tbl, what, finder)
finder = finder or EQ;
for _, v in pairs(tbl) do
if finder(v, what) then
return v;
end
end
return Unit;
end
function charon.list_has(tbl, element, finder)
finder = finder or EQ;
for _, v in pairs(tbl) do
if finder(v, element) then
return true;
end
end
return false;
end
function charon.list_get(key, tbl)
assert(getmetatable(tbl).__name == 'List', "list/get only accepts lists.");
assert(type(key) == 'number', "list/get key can only be numeric.");
local field = tbl[key];
if field == nil then return Unit; end
return field;
end
function charon.list_merge(left, right)
assert(getmetatable(left).__name == 'List', "list/merge only accepts lists.");
assert(getmetatable(right).__name == 'List', "list/merge only accepts lists.");
local vec = charon.list{};
for _, v in pairs(left) do
vec[#vec + 1] = v;
end
for _, v in pairs(right) do
vec[#vec + 1] = v;
end
return tbl;
end
function charon.list_len(left)
assert(getmetatable(left).__name == 'List', "list/add only accepts lists.");
return #left;
end
function charon.list_reduce_indexed(vec, fn, value)
assert(getmetatable(vec).__name == 'List', "list/reduce-indexed only accepts lists.");
local start = 1;
if value == nil then
start = 2;
value = vec[1];
end
for i=start, #vec do
value = fn(value, vec[i], i);
end
return value;
end
function charon.list_reduce(vec, fn, value)
assert(getmetatable(vec).__name == 'List', "list/reduce only accepts lists.");
local start = 1;
if value == nil then
start = 2;
value = vec[1];
end
for i=start, #vec do
value = fn(value, vec[i]);
end
return value;
end
function charon.list_append(left, ...)
assert(getmetatable(left).__name == 'List', "list/append only accepts lists.");
local vec = charon.list{};
for _, v in pairs(left) do
vec[#vec + 1] = v;
end
for _, v in pairs{...} do
vec[#vec + 1] = v;
end
return vec;
end
function charon.list_prepend(left, ...)
assert(getmetatable(left).__name == 'List', "list/prepend only accepts lists.");
local vec = charon.list{};
for _, v in pairs{...} do
vec[#vec + 1] = v;
end
for _, v in pairs(left) do
vec[#vec + 1] = v;
end
return vec;
end
function charon.list_drop(left, n)
assert(getmetatable(left).__name == 'List', "list/drop only accepts lists.");
assert(type(n) == 'number', "list/drop second argument must be a number.");
local vec = charon.list{};
local min = math.min(#left, n);
for i=1, min do
vec[i] = left[i];
end
return vec;
end
function charon.list_drop_left(left, n)
assert(getmetatable(left).__name == 'List', "list/drop-left only accepts lists.");
assert(type(n) == 'number', "list/drop-left second argument must be a number.");
local vec = charon.list{};
local min = math.min(#left, n);
for i=min, #left do
vec[i] = left[i];
end
return vec;
end
function charon.list_map(tbl, mapper)
assert(getmetatable(tbl).__name == 'List', "list/map only accepts lists.");
local vec = charon.list{};
for k, v in pairs(tbl) do
vec[#vec + 1] = mapper(v, k);
end
return vec;
end
function charon.list_filter(tbl, filter)
assert(getmetatable(tbl).__name == 'List', "list/map only accepts lists.");
local vec = charon.list{};
for k, v in pairs(tbl) do
if filter(v, k) then
vec[#vec + 1] = v;
end
end
return vec;
end
function charon.list_each(tbl, consumer)
assert(getmetatable(tbl).__name == 'List', "list/each only accepts lists.");
for k, v in pairs(tbl) do
consumer(v, k);
end
return Unit;
end
function charon.list_join(tbl, separator)
if separator == nil then separator = ','; end
assert(getmetatable(tbl).__name == 'List', "list/join only accepts lists.");
return table.concat(tbl, separator)
end
function charon.symbol_value(sym)
assert(getmetatable(sym).__name == 'Symbol', "symbol/value only accepts symbols.");
return sym.value;
end
function charon.table(tbl)
return setmetatable(tbl, Table)
end
function charon.table_merge(left, right)
assert(getmetatable(left).__name == 'Table', "table/merge only accepts tables.");
assert(getmetatable(right).__name == 'Table', "table/merge only accepts tables.");
local tbl = charon.table{};
for k, v in pairs(left) do
tbl[k] = v;
end
for k, v in pairs(right) do
tbl[k] = v;
end
return tbl;
end
function charon.table_map(tbl, mapper)
assert(getmetatable(tbl).__name == 'Table', "table/map only accepts tables.");
assert(type(mapper) == 'function' or type(getmetatable(mapper).__call) == 'function',
"table/map's second argument must be a function or be callable!");
local new = charon.table{};
for k, v in pairs(tbl) do
local pair = mapper(v, k);
new[pair[1]] = pair[2];
end
return new;
end
function charon.table_items(tbl)
assert(getmetatable(tbl).__name == 'Table', "table/items only accepts tables.");
local new = charon.list{};
for k, v in pairs(tbl) do
new[#new + 1] = charon.list{k, v};
end
return new;
end
function charon.table_remove(tbl, ...)
assert(getmetatable(tbl).__name == 'Table', "table/remove only accepts tables.");
local keys = {};
for _, key in pairs{...} do
keys[key] = key;
end
local out = charon.table{};
for k, v in pairs(tbl) do
if keys[k] ~= nil then
out[k] = v;
end
end
return out;
end
function charon.table_is_empty(tbl)
assert(getmetatable(tbl).__name == 'Table', "table/empty? only accepts tables.");
local next = next;
return next(tbl) == nil;
end
function charon.println(...)
print(...);
end
function charon.print(...)
for _, v in pairs{...} do
io.write(v);
end
end
function charon.atom_get(atom)
assert(getmetatable(atom).__name == 'Atom', "atom/get only accepts atoms.")
return atom.value;
end
function charon.atom_set(atom, value)
assert(getmetatable(atom).__name == 'Atom', "atom/reset! only accepts atoms.")
atom.value = value;
return Unit;
end
function charon.atom_apply(atom, func, ...)
atom.value = func(atom.value, ...);
return atom.value;
end
function charon.table_get(key, tbl)
assert(getmetatable(tbl).__name == 'Table', "table/get only accepts tables.");
local field = tbl[key];
if field == nil then return Unit; end
return field;
end
function charon.object_new_raw(proto)
local tbl = {};
for k, v in pairs(proto) do
if type(v) == 'table' then
tbl[k] = charon.object_new_raw(v);
else
tbl[k] = v;
end
end
return tbl;
end
function charon.object_new(proto)
local tbl = {};
for k, v in pairs(proto) do
local key = k;
if type(key) == 'table' and getmetatable(key) == Symbol then
key = key.value;
end
if type(v) == 'table' and getmetatable(v) == Table then
tbl[key] = charon.object_new(v);
else
tbl[key] = v;
end
end
return tbl;
end
function charon.object_get(object, key)
assert(object ~= nil, "Cannot get '" .. tostring(key) .. "' from nothing!");
assert(object ~= Unit, "Cannot get '" .. tostring(key) .. "' from unit!");
local field;
if getmetatable(key).__name == 'Symbol' then
field = object[key.value];
else
field = object[key];
end
if field == nil then return Unit; end
return field;
end
function charon.object_get_raw(object, key)
return object[key]
end
function charon.object_set_raw(object, key, value)
object[key] = value
end
function charon.object_set(object, key, value)
if getmetatable(key) == Table then
for k, v in pairs(key) do
if getmetatable(k) == Symbol then
object[k.value] = v;
else
object[k] = v;
end
end
else
if getmetatable(key) == Symbol then
object[key.value] = value;
else
object[key] = value;
end
end
end
function charon.call(fn, ...)
assert(fn ~= Unit, 'Unit is not callable!');
return fn(...);
end
function charon.apply(fn, args)
assert(fn ~= Unit, 'Unit is not callable!');
return fn(table.unpack(args));
end
function charon.opaque_call(fn)
if fn == Unit then
throw('Unit is not callable!');
end
fn();
return Unit;
end
function charon.file_open(file, mode)
return io.open(file, mode) or Unit;
end
function charon.file_close(file)
io.close(file);
return Unit;
end
function charon.file_write(file, what)
file:write(what);
return Unit;
end
function charon.file_read(file)
return file:read(what);
end
--[[
String find delegate, converts multireturn to Charon list, as multiple values
are not supported by Charon.
]]
function charon.string_find(str, ...)
return charon.list{str:find(...)};
end
--[[
String match delegate, converts multireturn to Charon list, as multiple values
are not supported by Charon.
]]
function charon.string_match(str, ...)
return charon.list{str:match(...)};
end
function charon.compose(a, b)
return function(...)
return b(a(...));
end
end
function charon.or_coalesce(test, val)
if test == nil or test == Unit then
return val;
end
return test;
end
function charon.str(...)
local out = '';
for _, v in pairs{...} do
out = out .. tostring(v);
end
return out;
end
function charon.range(from, to, inc)
assert(type(from) == 'number' and type(to) == 'number'
, 'Range function expects numeric input only!')
assert(inc == nil or type(inc) == 'number'
, 'Range\'s third argument can only be a number or not provided. Saw ' .. tostring(inc) .. ' instead.')
if inc == nil then inc = 1; end
local p = charon.list{};
local j = 1;
if from > to then
for i=from, to, -inc do
p[j] = i;
j = j + 1;
end
elseif from == to then
return p;
else
for i=from, to, inc do
p[j] = i;
j = j + 1;
end
end
return p;
end
-- Functions
-- Mapping for macro +
function charon.plus(result, ...)
for _, v in pairs{...} do
result = result + v;
end
return result;
end
-- Mapping for macro -
function charon.minus(result, ...)
for _, v in pairs{...} do
result = result - v;
end
return result;
end
-- Mapping for macro /
function charon.div(result, ...)
for _, v in pairs{...} do
result = result / v;
end
return result;
end
-- Mapping for macro *
function charon.mul(result, ...)
for _, v in pairs{...} do
result = result * v;
end
return result;
end
-- Mapping for macro ^
function charon.pow(result, ...)
for _, v in pairs{...} do
result = result ^ v;
end
return result;
end
-- Mapping for macro =
function charon.eq(l, r, ...)
local result = l == r;
for _, il in pairs{...} do
for _, ir in pairs{...} do
result = result and il == ir;
end
end
end
-- Mapping for macro <>
function charon.neq(l, r, ...)
local result = l ~= r;
for _, il in pairs{...} do
for _, ir in pairs{...} do
result = result and il ~= ir;
end
end
return result;
end
-- Mapping for macro >
function charon.gt(l, r, ...)
local result = l > r;
for _, il in pairs{...} do
for _, ir in pairs{...} do
result = result and il > ir;
end
end
return result;
end
-- Mapping for macro <
function charon.lt(l, r, ...)
local result = l < r;
for _, il in pairs{...} do
for _, ir in pairs{...} do
result = result and il < ir;
end
end
return result;
end
-- Mapping for macro >=
function charon.gteq(l, r, ...)
local result = l >= r;
for _, il in pairs{...} do
for _, ir in pairs{...} do
result = result and il >= ir;
end
end
return result;
end
-- Mapping for macro <=
function charon.lteq(l, r, ...)
local result = l <= r;
for _, il in pairs{...} do
for _, ir in pairs{...} do
result = result and il <= ir;
end
end
return result;
end
-- Mapping for macro and
function charon._and(result, ...)
for _, v in pairs{...} do
result = result and v;
end
return result;
end
-- Mapping for macro or
function charon._or(result, ...)
for _, v in pairs{...} do
result = result or v;
end
return result;
end
-- Mapping for macro not
function charon._not(value)
return not value;
end
-- Mapping for macro nand
function charon.nand(...)
return not charon._and(...);
end
-- Mapping for macro nor
function charon.nor(...)
return not charon._or(...);
end
-- Mapping for macro xor
function charon.xor(...)
throw('Not implemented');
end
-- Namespace.
return charon;