-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.lua
More file actions
1243 lines (1173 loc) · 69 KB
/
final.lua
File metadata and controls
1243 lines (1173 loc) · 69 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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--TODOs:
-- 1. Make Negi parser manifest and move existing parser code there. Currently it's nodes are all disconnected and just exist in main context, which just looks like some kid didn't put back toys inside a box.
-- 2. Tuple load and Tuple context. This is the last major issue that keep me from testing phase.
-- 2.1 Tuple keep mutations only if passed explicitly after pop_layer. The main difficulty of this is sometimes Tuple keeps mutations and sometimes don't?
-- 2.2 Tuple should work simmilarly like KES layers, "store" parent and delta from parent Tuple.
-- 3. Host representation. Lua have quite messy syntax and context, we need to nicely wrap this up inside some Manifest or Tuple.
-- 4. Rework dynamic membrane as delayed behaivour: rework push_layer into always grounded. grounded - immediate, dynamic - verb, isolated - contained.
-- 5. Add "ask" clause to dispatch
return (function ()
--Frontend: NegI - Negotiation Interface (the interface)
--Backend: FINAL - Framework for Intent Negotiation and Authority Logic (or Final Is Not A Language) (the substrate)
-- This works more or less as ship of thesus, FINAL provides common interfaces for other manifests to communicate with each other in platform agnostic way
local newstate = function () -- something similar to lua_newstate but for FINAL
local new_conv = function (c, ...) --helps chaining like resolve_chain("salt")(4)(5)(1).r
local h = {c = c,r = {...}}
setmetatable(h, {
__call = function (self, ...)
return new_conv(c, c(...))
end })
return h
end
local table_invert = function (t)
local s={}
for k,v in pairs(t) do
s[v]=k
end
return s
end
local bimap_write = function (bimap, view_key, index, value)
local views = {bimap[view_key]}
for i,e in pairs(bimap) do if i ~= view_key then views[2] = bimap[i]; break end end
if value ~= nil then
views[1][index] = value
views[2][value] = index
else
views[2][views[1][index]] = nil
views[1][index] = nil end end
local FLESH = { -- stands for "FINAL Local Environment Shared Handles"
FISH = { -- "FINAL Internal State Holder" - used by particular manifests to pass around some data. Could be avoided via KES, but those values are quite commonly used, so it will influence performance.
tail_context = nil, -- context from pop_layer
pending_labels = nil, -- "API" for labels, that are about to be loaded by something
},
KES = { -- "Knowledge Environment State" (considered finished, until bugs will be found)
layers = {{d = 1,c = {}}}, -- stack of references, string names ready for free (initial layer is preloaded)
labels = {lb = {}, bl = {}}, -- BiMap<label: String, bind: Number> holds labeled refences, bimap/not in bindings - because it's an extension that exist only for the user
bindings = {}, -- Array<bind: Number, {records: Map<layer_id: Integer, entry: Manifest>, order: BiMap<layer_id: Integer, order: Integer>}> holds references to data
relevance = { -- tracks what currently available in active context
dl = {[1]=1}, -- Array<depth: Integer, layer_id: Integer>
ld = {[1]=1}}, -- Map<layer_id: Integer, depth: Integer> stores which layers are relevent to current context, mostly used as Set<layer_id: Integer>
isolations = { -- tracks where grounded layer must be used. basically ordered Set<depth: Integer>
["od"] = {}, -- Array<order: Integer, depth: Integer>
["do"] = {}}, -- Map<depth: Integer, order: Integer> "this is the reason I hate keywords"
resolve = function (self, ref) -- note that strings are names for indicies
if (type(ref) ~= "string" and type(ref) ~= "number") then
error("FINAL: FLESH.KES:resolve - invalid argument, expected number or string", 2)
end
ref = (type(ref) == "number") and ref or self.labels.lb[ref] -- we override the table depending on what we resolving
local rt = self.bindings[ref]
if (rt == nil) then return end -- the environment don't know about this binding so we exit early, this line is optional
local m, fh -- data, from_here
if #rt.order.ol > #self.relevance.dl then -- NOTE: records store changes per each layer, so this checks if it's effective to do vertical or horizontal search traversal
for d = #self.relevance.dl, 1, -1 do -- inverse order, because we pick fresh ones, in order to hit early
local l = self.relevance.dl[d]
if rt.order.lo[l] then m = rt.records[l]; fh = (#self.layers == l); break end -- we do not use rt.records[e], because it may contain nil, due to "namespace" reservation
end
else
for o = #rt.order.ol, 1, -1 do -- inverse order, because we pick fresh ones, in order to hit early
local l = rt.order.ol[o]
if self.relevance.ld[l] then m = rt.records[l]; fh = (#self.layers == l); break end
end
end
return m, fh -- Manifest(lua table, that represnts it) and if this info from current layer
end,
push_layer = function (self, parent, grounded, isolated, context) -- I need to remodel how parent is retrieved, otherwise making always grounded will break isolations, because dynamic will always have a parent of nearest isolation, besially it's time for a FISH update
--we make an exception for root layer, because there's nothing to isolate against
local l
if (#self.layers > 0) then -- initial layer is preloaded, but I still add it if user decide to pop the root layer and there will be those who would like to do that for the fun of it (hi tsoding)
if (type(parent) ~= "number") then error("FINAL: FLESH.KES:push_layer - parent<number> expected for explicit grounded, got "..type(parent), 2) end -- I also think that root layers should be definable if no parent specified
local p_depth = (parent and self.layers[parent].d or 0) -- parent depth
local iso_depth_i, iso_depth = #self.isolations.od, nil
while ((self.isolations.od[iso_depth_i] or 0) > p_depth) -- we check if layer definition was outside of isolation. also binary search could be applied here
iso_depth = self.isolations.od[iso_depth_i]
iso_depth_i = iso_depth_i - 1 end
if (iso_depth and not grounded) then -- if dynamic defined outside isolation, then it shouldn't consider effect of isolation
parent = self.relevance.dl[iso_depth - 1] -- find parent of layer outside of isolation
p_depth = (parent and (self.layers[parent].d) or 0) -- parent depth
grounded = true end
l = { -- new layer data
d = (grounded and p_depth or (self.layers[#self.layers].d or 0)) + 1, -- new layer depth
s = grounded and {r={},i={}} or nil, -- if there is grounded, then those are shadowed layers (r - relevance, i - isolation)
c = context -- `c` is Set<reference: Number|String, exist: Boolean> references relvant to this context layer
else l = {d = 1, s = grounded and {r={},i={}} or nil, c = (size and table.create) and table.create(0, size) or {}}} end
if isolated then bimap_write(self.isolations, "od", #self.isolations.od+1, l.d) end -- isolated (external binding resolving, causes it to use resolving oblivious to effects from here)
if grounded then -- grounded
for i = #self.relevance.dl, l.d, -1 do -- exclude all layers between parent and new layer via depth
l.s.r[#l.s.r+1] = self.relevance.dl[i] -- add shadowed layers (we can ask depth form them directly)
bimap_write(self.relevance, "dl", i, nil) end -- removing irrelevant layers
if iso_depth then -- if crossing or sealing isolations
for i = #self.isolations.od, self.isolations["do"][iso_depth], -1 do -- iso_depth is calculated anyways, but I think I need to reorganize this code
l.s.i[#l.s.i+1] = self.isolations.od[i] -- add shadowed isolations (we can ask depth form them directly)
bimap_write(self.isolations, "od", i, nil) end end end -- removing irrelevant isolations
self.layers[#self.layers+1] = l
bimap_write(self.relevance, "ld", #self.layers, l.d)
return #self.layers -- used if Sequence will define another Sequence
end,
pop_layer = function (self, migrate) -- P.S. while push and pop suggest stack structure, this isn't purely just that due to parent detours
self.isolations["do"][self.layers[#self.layers].d] = nil -- lift isolation sandbox
local shadowed_data = self.layers[#self.layers].s
if shadowed_data then
for _,e in ipairs(shadowed_data.r) do -- restoring shadowed context relevance
bimap_write(self.relevance, "ld", e, self.layers[e].d) end
for _,e in ipairs(shadowed_data.i) do -- restoring shadowed context isolations
bimap_write(self.isolations, "od", #self.isolations.od + 1, e) end end
if not migrate then
for i,_ in pairs(self.layers[#self.layers].c) do -- removing references from bindings
local db = self.bindings
local rt = db[i]
rt.records[#self.layers] = nil
if (rt.order.lo[#self.layers] == rt.order.ol[#rt.order.ol]) then
bimap_write(rt.order, "lo", #self.layers, nil)
else error("KES:pop_layer - invalid layer in unload transaction query. [Z_Z] Currently I'm thinking to keep it as user error or make code for handling this.", 2) end
if #rt.records <= 0 then db[i] = nil; bimap_write(self.labels, "bl", i, nil) end end end
if (#self.layers > 0) then self.layers[#self.layers] = nil end -- removing layer
return migrate and self.layers[#self.layers + 1].c or nil -- for tail calls it's preferably to return lifted context
end,
get_context = function (self) return #self.layers end, -- used by Sequence to memorise context for later use
write_entry = function (self, ref, m) -- reference : Number|String, [manifest: Any|Nil]
local db = self.bindings
if (type(ref) == "string") then
bimap_write(self.labels, "lb", ref, self.labels.lb[ref] or (#db + 1))
ref = self.labels.lb[ref]
else ref = ref or (#db + 1) end
db[ref] = db[ref] or { records = {}, order = {ol = {}, lo = {}} }
db[ref].records[#self.layers] = m -- note that nil will reserve the place on layer
bimap_write(db[ref].order, "lo", #self.layers, #db[ref].order.lo + 1)
if not self.layers[#self.layers].c[ref] then -- if it's new binding for this context
self.layers[#self.layers].c[ref] = true
end return ref
end, -- entry writes could only happen in current context
direct_snapshot = function (self, layer_id, c)
c = c or {}
for i,_ in pairs(self.layers[layer_id].c) do
c[i] = self.bindings[i].records[layer_id] end
return c end,
inner_snapshot = function (self) -- used in tuple, in order to track writes
return self.direct_snapshot(#self.layers)
end,
cview_snapshot = function (self, outer)
local c = {}
for i = #self.relevance.dl, 1, -1 do
local e = self.relevance.dl[i]
for i,_ in pairs(self.layers[e].c) do
c[i] = c[i] or self.bindings[i].records[e] end end
return c
end,
binding_label_get = function (self, b) return self.labels.bl[b] end, -- Used by Tuple to make label list.
},
dispatch = function (self, lterm, rterm, protocol)
if lterm == nil then return end
if protocol then
if rterm then
if protocol.can then
local label_p = self.KES.bindings[self.KES.bindings.Label.records[self.host_layer]].records[self.host_layer] -- we use direct access, because this stuff will depend on furst record anyways
if self.capcheck(label_p, rterm) then return {protocol = protocol.can[rterm.state.name], state = lterm.state} end -- TODO: we need to check if it's exist
if protocol.ask then
local artifact_p = self.KES.bindings[self.KES.bindings.Artifact.records[self.host_layer]].records[self.host_layer]
local label_p = self.KES.bindings[self.KES.bindings.Label.records[self.host_layer]].records[self.host_layer]
local hanc = self.KES:resolve(protocol.ask)
if self.capcheck(label_p, rterm) then if self.capcheck(artifact_p, hanc) then return hanc.state.artifact(lterm, rterm)
else return self:dispatch(hanc, {
protocol = tuple_p.state,
state = {items = {lterm, rterm}, labels = {"self", "arg"}}}, hanc.protocol) end end end
if rterm and rterm.protocol and rterm.protocol.get then -- I'm conflicted about this
rterm = self:dispatch(rterm, nil, rterm.protocol) -- get exectuion
end
if protocol.call then
local artifact_p = self.KES.bindings[self.KES.bindings.Artifact.records[self.host_layer]].records[self.host_layer]
local tuple_p = self.KES.bindings[self.KES.bindings.Tuple.records[self.host_layer]].records[self.host_layer]
local hanc = self.KES:resolve(protocol.get)
if self.capcheck(artifact_p, hanc) then
return hanc.state.artifact(lterm, rterm)
else return self:dispatch(hanc, {
protocol = tuple_p.state,
state = {items = {lterm, rterm}, labels = {"self", "arg"}}}, hanc.protocol) end end-- needs some standartization on how this should be passed around, don't like hardcoded "self" and "arg"
if protocol.get then -- fallback to underlying manifest for an answer
local artifact_p = self.KES.bindings[self.KES.bindings.Artifact.records[self.host_layer]].records[self.host_layer]
local tuple_p = self.KES.bindings[self.KES.bindings.Tuple.records[self.host_layer]].records[self.host_layer]
local unhc = self.KES:resolve(protocol.get)
local fabk
if self.capcheck(artifact_p, unhc) then
fabk = unhc.state.artifact(lterm)
else fabk = self:dispatch(unhc, lterm, unhc.protocol) end
return self:dispatch(fabk, rterm, fabk.protocol)
else return {
protocol = self.KES.bindings[self.KES.bindings.Error.records[self.host_layer]].records[self.host_layer].state,
state = {desc = "ENIMGA: FLESH:dispatch Error: rterm is outside of lterm protocol response capability"}} end
elseif protocol.get then
local artifact_p = self.KES.bindings[self.KES.bindings.Artifact.records[self.host_layer]].records[self.host_layer]
local unhc = self.KES:resolve(protocol.get)
if self.capcheck(artifact_p, unhc) then
return unhc.state.artifact(lterm)
else return self:dispatch(unhc, lterm, unhc.protocol) end
else return lterm end
elseif lterm.protocol then return self:dispatch(lterm, rterm, lterm.protocol)
elseif rterm then return {
protocol = self.KES.bindings[self.KES.bindings.Error.records[self.host_layer]].records[self.host_layer].state,
state = {desc = "ENIMGA: FLESH:dispatch Error: missing protocol"}}
else return lterm end
end,
--env methods
reset = function (self) end, -- inits defaults and other stuff
}
--FLESH.host_layer = FLESH.KES:push_layer() -- we will use it in order to reference stuff at the base
FLESH.host_layer = 1 -- we already have layer in KES, so no need for push_layer
for _,e in pairs({
"Native","Artifact","Error","Protocol","Manifest","gap","Number",
"String","Label","Tuple","Sequence","Membrane","Negotiation"
}) do
FLESH.KES:write_entry(e) -- we can directly write stuff, without explicitly defining binding
end
-- Artifact assumptions:
-- on call it recieves 2 manifests (tabels, not KES IDs): self, arg
-- on get it's just self (tables, not KES IDs)
-- the return should return Manifest (tables, not KES IDs)
--I need to make things clear
--Manifest can only store references to stuff inside it's state
--meaning if it's related to user modifyable storage, we store KES label or binding (refer to Tuple)
--if something different getting stored, it's host resources, that user can only interact with it via protocol
--which means yes, state itself is opaque host resource
FLESH.make = {}
local a_stubs = {FLESH.KES:write_entry(),FLESH.KES:write_entry(),FLESH.KES:write_entry(),FLESH.KES:write_entry()}
FLESH.KES:write_entry("Artifact", { -- Artifact for handling external authority
protocol = {
can = {
["in"] = {call = a_stubs[1]},
["="] = {call = a_stubs[2]}}},
state = {
can = {
reload = {get = a_stubs[3]}},
introspect = {get = a_stubs[4]}, -- can't decide on a name yet, should return ingridients for cooking the authority in question
call = a_stubs[4]}})
FLESH.make.Artifact = function (chunk, chunkname, mode, env)
local plfmt = { __index = function (s,i) -- we make lazy fetch on Artifact, because we have a metacircular situation
local v = rawget(s,i)
if i ~= "protocol" then return v end
if type(v) ~= "table" then
v = FLESH.KES:resolve(v).state
rawset(s,i,v)
setmetatable(s, nil) end
return v end}
chunkname = chunkname or "chunk"
local a, e = load(chunk, "FINAL:"..chunkname, mode, env)
if (a) then a, e = pcall(a) end
if (e) then
local em = {
protocol = "Error",
state = {desc = "Artifact: Failed to load "..chunkname.." due to host error: "..e}}
setmetatable(em, plfmt)
return em end
local am = {
protocol = FLESH.KES.bindings[FLESH.KES.bindings.Artifact.records[FLESH.host_layer] ].records[FLESH.host_layer].state,
state = {
chunk = chunk,
chunkname = chunkname,
mode = mode,
env = env,
artifact = a}} end
FLESH.intentcheck = function(self, arg) -- State intent of manifests are matching?
for i,e in pairs(self.state) do
if (arg.protocol[i] ~= e) then
return false end end
for i,e in pairs(self.state.can) do
if (arg.protocol.can[i] ~= e) then
return false end end
return true end
FLESH.capcheck = function(self, arg) -- Even through fallbacks, is manifest implements this protocol?
-- TODO: check protocol in flat form, we need to make sure clauses are reachable, not that there is inside chain some Manifest that satisfy intent.
local fail = function () return arg.protocol.get and FLESH.capcheck(FLESH:dispatch(arg, nil, arg.protocol)) or false end
for i,e in pairs(self.state) do
if (arg.protocol[i] ~= e) then
return fail() end end
for i,e in pairs(self.state.can) do
if (arg.protocol.can[i] ~= e) then
return fail() end end
return true end
--common between protocol manifests
local capability_check = FLESH.KES:write_entry(a_stubs[1], FLESH.make.Artifact([[return function (self, arg)
return FLESH.capcheck(self, arg) and FLESH.KES:resolve("true") or FLESH.KES:resolve("false") end]], "capcheck"))
FLESH.KES:write_entry(a_stubs[2], FLESH.make.Artifact([[return function (self, arg) end]])) -- should make artifact creation off state, table, Tuple and string
FLESH.KES:write_entry(a_stubs[3], FLESH.make.Artifact([[return function (self)
return FLESH.make.Artifact(self.state.chunk, self.state.chunkname, self.state.mode, self.state.env) end]]))
FLESH.KES:write_entry(a_stubs[4], FLESH.make.Artifact([[return function(self, arg)
local tunp = unpack or table.unpack
return self.state.artifact(tunp(arg)) end]]))
local p_artifact = function (...) return FLESH.KES:write_entry(nil, FLESH.make.Artifact(...)) end
local host_name = FLESH.KES:write_entry() -- we reserve space for the host name
FLESH.KES:write_entry("Native", { -- should represent state during introspection, to make it hostile
protocol = {
["in"] = {call = capability_check}},
state = {
responder = {
name = {get = host_name}}}}) -- it's later constructed a string with "Lua 5.5"
FLESH.KES:write_entry("Error", { -- Error "as value"
protocol = {
["in"] = {call = capability_check},
["="] = {call = p_artifact([[]])}},
state = {
can = {
name = {get = p_artifact([[]])},
desc = {get = p_artifact([[return function (self)
return { -- UNFINISHED
protocol = FLESH.KES:resolve("String").state,
state = tostring(self.state.desc)}
end]])},
caller = {get = p_artifact([[]])},
trace = {get = p_artifact([[]])},
},
}
})
FLESH.KES:write_entry("Token", { -- adds parser data to values
protocol = {["in"] = {call = capability_check}},
state = {
can = {
token = {can = {
root = {get = p_artifact([[]])}, -- references root Token from where it is
parent = {get = p_artifact([[]])}, -- return parent Token (probably won't add this, because I don't store that)
element = {get = p_artifact([[]])}, -- text representation of Token
id = {get = nil}, -- it's id (probably will remove it)
position = {get = nil}, -- position relative to root Token text representation
content = {get = nil}, -- return Tuple with it's child Tokens (probably won't add this, because I store that in opaque non-uniform states)
}}
},
get = p_artifact([[return function (self) return self.state.token end]]), -- fallback to standard token operation
}})
-- no implicit conversions, this is only between this specific implementation
local make_trans_op = function (op)
return p_artifact([[return function (self, arg)
if (FLESH.capcheck({state = self.protocol},arg)) then -- 2nd value might have different protocol, I think I'll need to rework this into lua general value protocol check or something.
return FLESH:import(self.state ]]..op..[[ arg.state)
else
return -- Error manifest
end
end]])
end
local make_trans = function (trans)
return p_artifact([[return function (self)
return FLESH:import(]]..trans..[[(self.state))
end]])
end
local make_host_res_init = function (host_type)
return p_artifact([[return function (self, arg)
-- wrap host resource manifest
if (FLESH.capcheck(self,arg)) then return arg end -- literal uses same protocol, so we just passing
if (type(arg) == "]]..host_type..[[") then return {protocol = self.state,state = arg}} end
return -- Error manifest
end]])
end
FLESH.make.Tuple = function (t)
local s = {labels = {}, items = {}}
for i,e in pairs(t) do
s.items[#s.items+1] = e
if type(k) == "string" then
s.labels[k] = #s.items
end
end
return {protocol = FLESH.KES:resolve("Tuple").state, state = s}
end
FLESH.make.Error = function (desc)
return {
protocol = FLESH.KES.bindings[FLESH.KES.bindings.Error.records[FLESH.host_layer] ].records[FLESH.host_layer].state,
state = {desc = desc}}end
local host_protocols = FLESH.make.Tuple({ -- while it's a mapping table, FINAL fundamentally disagree with lua on type existance, so for example userdata can't be capchecked
["nil"] = FLESH.KES:write_entry(nil, {protocol = {},state = {}}),
boolean = FLESH.KES:write_entry(nil, {protocol = {
can = {
["in"] = {call = capability_check},
["="] = make_host_res_init("boolean")}},
state = {
can = {
["|"] = {call = make_trans_op("or")},
["&"] = {call = make_trans_op("and")},
["~"] = {get = make_trans("not")},
["=="] = {call = make_trans_op("==")},
["~="] = {call = make_trans_op("~=")},
}
}}),
number = FLESH.KES:write_entry(nil, {protocol = {
can = {
["in"] = {call = capability_check},
["="] = make_host_res_init("number")}},
state = {
can = {
["+"] = {call = make_trans_op("+")},
["-"] = {call = make_trans_op("-")},
["*"] = {call = make_trans_op("*")},
["/"] = {call = make_trans_op("/")},
["%"] = {call = make_trans_op("%")},
["^"] = {call = make_trans_op("^")},
["|"] = {call = make_trans_op("|")},
["&"] = {call = make_trans_op("&")},
["<<"] = {call = make_trans_op("<<")},
[">>"] = {call = make_trans_op(">>")},
["=="] = {call = make_trans_op("==")},
["~="] = {call = make_trans_op("~=")},
["<"] = {call = make_trans_op("<")},
[">"] = {call = make_trans_op(">")},
["<="] = {call = make_trans_op("<=")},
[">="] = {call = make_trans_op(">=")},
abs = {get = make_trans("math.abs")},
acos = {get = make_trans("math.acos")},
asin = {get = make_trans("math.asin")},
atan = {get = make_trans("math.atan")},
ceil = {get = make_trans("math.ceil")},
cos = {get = make_trans("math.cos")},
deg = {get = make_trans("math.deg")},
exp = {get = make_trans("math.exp")},
floor = {get = make_trans("math.floor")},
fmod = {get = make_trans("math.fmod")},
frexp = {get = make_trans("math.frexp")},
huge = {get = make_trans("math.huge")}, -- const
ldexp = {get = nil}, -- math.ldexp (m, e) - Returns m2e, where e is an integer.
log = {get = make_trans("math.log")},
max = {get = make_trans("math.max")},
maxinteger = {get = make_trans("math.maxinteger")}, -- const
min = {get = make_trans("math.min")},
mininteger = {get = make_trans("math.mininteger")}, -- const
modf = {get = nil}, -- Returns the integral part of x and the fractional part of x. Its second result is always a float.
pi = {get = make_trans("math.pi")}, -- const
rad = {get = make_trans("math.rad")},
--random = {get = make_trans("math.random")},
--randomseed = {call = nil}, -- [x, [y]]
sin = {get = make_trans("math.sin")},
sqrt = {get = make_trans("math.sqrt")},
tan = {get = make_trans("math.tan")},
tointeger = {get = make_trans("math.tointeger")},
type = {get = nil}, -- returns "integer" or "float" or fail
ult = {call = nil}, -- math.ult (m, n)
to = {
can = {
string = {get = p_artifact([[return function (self)
return { -- UNFINISHED
protocol = FLESH.KES:resolve(host_types.state.items[host_types.state.labels.string]).state,
state = tostring(self.state)}
end]])}}}
}
}}),
string = FLESH.KES:write_entry(nil, {protocol = {
can = {
["in"] = {call = capability_check},
["="] = make_host_res_init("string")}},
state = {
can = {
["+"] = {call = make_trans_op("..")},
["=="] = {call = make_trans_op("==")},
["~="] = {call = make_trans_op("~=")},
format = {call = p_artifact([[return function (self, arg)
-- check if arg is tuple and go on
]])},
size = {get = make_trans("string.len")},
lower = {get = make_trans("string.lower")},
upper = {get = make_trans("string.upper")},
reverse = {get = make_trans("string.reverse")},
to = {
can = {
number = {get = p_artifact([[return function (self)
return {
protocol = FLESH.KES:resolve(host_types.state.items[host_types.state.labels.number]).state,
state = tonumber(self.state)}
end]])}}}
}
}}),
userdata = nil, -- the lua lables it userdata, but basically it's a capability wildcard that FINAL can't use to check against userdata instance
["function"] = FLESH.KES:write_entry(nil, {protocol = {
can = {
["in"] = {call = capability_check},
["="] = make_host_res_init("function")}},
state = {
can = {
dump = {get = p_artifact([[]])}
},
call = p_artifact([[return function (self, arg)
local tuple_p
return FLESH:import(self.state(table.unpack(args)))
end]])
}}),
thread = FLESH.KES:write_entry(nil, {protocol = {
can = {
["in"] = {call = capability_check},
["="] = make_host_res_init("thread")}},
state = {
can = {
close = {get = p_artifact([[]])},
isyieldable = {get = p_artifact([[]])},
resume = {get = p_artifact([[]])},
status = {get = p_artifact([[]])},
wrap = {get = p_artifact([[]])},
}
}}),
table = FLESH.KES:write_entry(nil, {protocol = { -- this also somewhat capability wildcard, but the importer uses different protocol for table, if it's table isn't empty
can = {
["in"] = {call = capability_check},
["="] = p_artifact([[return function (self, arg)
-- create from table literal and native
if (FLESH.capcheck(self,arg)) then return arg end -- literal uses same protocol, so we just passing
if (type(arg) == "table") then return {protocol = self.state,state = arg}} end
return -- Error manifest
end]])}},
state = {
can = {
["+"] = {call = make_trans_op("..")},
["=="] = {call = make_trans_op("==")},
["~="] = {call = make_trans_op("~=")},
size = {call = make_trans("#")},
},
call = p_artifact([[return function (self, arg)
-- TODO: we somehow need to check if arg is a number or a manifest
return FLESH:import(self[arg.state]) -- we need to chanage the intent of import, so it would use this data
end]])
}}),
unknown = FLESH.KES:write_entry(nil, {protocol = {
can = {
["in"] = {call = capability_check},
["="] = p_artifact([[return function (self, arg) -- UNFINISHED
return {protocol = self.state,state = arg}
end]])}},
state = {}}),
})
FLESH.import = (function ()
local value_mapping = function (self, o)
return {
protocol = host.protocols[type(o)].state, -- I need to rework the structure
state = o} end
local gen_mt_protocol = function (self, mt)
for k,v in pairs(mt) do
end
end
local mapping = {
["nil"] = (function ()
local instance = nil -- I should think on how I can integrate it
return function (self, o)
return instance
end end)(),
boolean = (function ()
local instances = {value_mapping(self, false), value_mapping(self, true)} -- we keep lua from creating new tables for finite amount of states, by just caching them
return function (self, o)
return instances[o and 2 or 1]
end end)(),
number = value_mapping,
string = value_mapping,
userdata = function (self, o)
local capability = getmetatable(o)
-- we generate capability mapping no matter the reason
return {protocol = gen_mt_protocol(self, o), state = o}
end,
["function"] = value_mapping,
thread = value_mapping,
table = function (self, o)
local mt = getmetatable(o)
if mt then
return {protocol = gen_mt_protocol(self, mt), state = o} -- unfinished, since it doesn't handle default table behaivour
else
return value_mapping(self, o)
end
end
}
return function (self, o) -- imports lua object "o" inside FINAL environment
-- it should find FINAL's host knowledge and apply appropriate interface from it.
local pif = mapping[type(o)]
if pif then
return pif(self, o)
else
-- at this point I should make Error constructors, preferrably that would have error codes
end
end end)()
local host_tuple = FLESH.make.Tuple({
meta = FLESH.KES:write_entry(nil, FLESH.make.Tuple({
name = FLESH.KES:write_entry(nil, FLESH:import("Lua 5.5")),
version = FLESH.KES:write_entry(nil, FLESH:import("0.0.1"))
})),
intrinsics = FLESH.KES:write_entry(nil, FLESH.make.Tuple({
types = nil,
concepts = nil,
})),
authority = FLESH.KES:write_entry(nil, FLESH.make.Tuple({
coroutine = nil,
debug = nil,
io = nil,
os = nil,
package = nil,
})),
})
--[[ -- since FINAL structured differently, lua interafec should be altered to fit the philosophy
basic -- ???
_G -- Host
_VERSION -- Host
assert -- Host
collectgarbage -- Host
dofile -- Host
error -- Host
getmetatable -- table, userdata
ipairs -- table, userdata
load -- Host
loadfile -- Host
next -- table, userdata
pairs -- table, userdata
pcall -- Host
print -- Host (we don't have any other way to interact with lua console)
rawequal -- lua types
rawget -- table
rawlen -- table, string
rawset -- table
require -- Host
select -- function?
setmetatable -- table, userdata
tonumber -- string, number
tostring -- lua types
type -- lua types
warn -- Host
xpcall -- Host
]]
FLESH.KES:write_entry("Protocol", { -- Protocol for new Protocols
protocol = {
can = {
--of = {call = p_artifact([[]])}, -- no matter how sweet it looks, we literally can't do that
["in"] = {call = capability_check}, -- capability_check is shared artifact
["="] = {call = p_artifact([[]])}
},
call = p_artifact([[]]), -- artifact for creating new protocol manifests
},
state = {
["in"] = capability_check
}
})
FLESH.KES:write_entry("Manifest", { -- Protocol for directly constructing Manifests
protocol = {
can = {
["in"] = {call = capability_check}, -- capability_check is shared artifact
["="] = {call = p_artifact([[return function (self, arg)
--we take Tuple from arg
--make manifest for the KES with actual lua tables
--store it inside KES (of course)
--add lua metatable so I won't have to chain KES accesses
--return new manifest's reference
end]])},
},
},
state = {} -- thats the any type
})
FLESH.KES:write_entry("//", {protocol = {
call = p_artifact("return function (self, arg) return { protocol = { call = p_artifact(\"return function (self, arg) return arg end\")}} end")}})
FLESH.KES:write_entry("gap", {protocol = {}, state = {}})
FLESH.KES:write_entry("Number", FLESH.KES:resolve(host_types.state.items[host_types.state.labels.number]))
FLESH.KES:write_entry("String", FLESH.KES:resolve(host_types.state.items[host_types.state.labels.string]))
FLESH.KES:write_entry("Label", { -- it's job is just labeling manifests
protocol = {
["in"] = {call = capability_check},
["="] = {call = p_artifact([[]])}
},
state = {
can = {
[":"] = {get = p_artifact([[return function (self)
FISH.pending_labels[self.state.name] = true
return { protocol = { call = p_artifact("return function (self, arg) return arg end")} }end]])},
["name"] = {get = p_artifact([[return function (self)
return {
protocol = FLESH.KES.bindings[FLESH.KES.bindings.String.records[FLESH.host_layer] ].records[FLESH.host_layer].state,
state = self.state.name}
end]])},
},
get = p_artifact([[return function (self)
return FLESH.KES:resolve(self.state.name)
end]])
}
})
--[[
Tuple_instance.state = {
labels = {}, -- BiMap<label: string, binding: number>
bindings = {}, -- Array<bind: number, {depend: table|nil, delta: any}>
}
]]
FLESH.KES:write_entry("Tuple", {
protocol = {
["in"] = {call = capability_check},
["="] = {call = p_artifact([[]])}
},
state = {
can = {
["+"] = {call = p_artifact([[]])},
["*"] = {call = p_artifact([[]])},
delta = {get = p_artifact([[]])},
load = {get = p_artifact([[return function (self)
local labels, items = self.state.labels, self.state.items
if labels then
if items then
for i,e in pairs(labels) do
FISH.pending_labels[i] = true -- items[e]... FISH.pending_labels can't sustain this, I need different interface for passing pending context effects. I also have same problem in membranes
end end end end]])},
["."] = {get = p_artifact([[return function (self) --TODO
self.state.labels
end]])},
},
call = p_artifact([[return function (self, arg)
local num_p = FLESH.KES.bindings[FLESH.KES.bindings.Number.records[FLESH.host_layer] ].records[FLESH.host_layer]
if (FLESH.capcheck(num_p, arg)) then
else if (FLESH.capcheck({state = self.protocol}, arg) and arg) then -- slicing in python style
else
end
end]])
}
})
FLESH.KES:write_entry("Sequence", {
protocol = {
["in"] = {call = capability_check},
["="] = {call = p_artifact([[]])}
},
state = {
can = {
prods = {get = p_artifact([[]])}, -- in order to get raw data, @ must be used
creturn = {get = p_artifact([[]])}, -- in order to get raw data, @ must be used
introspect = {get = p_artifact([[]])}
},
call = p_artifact([[return function (self, arg)
local prods = self.state.prods
FLESH.KES:push_layer(self.state.parent, self.state.grounded, self.state.isolated, FLESH.FISH.tail_context or table.create(0, #prods))
local pl = {}
FISH.pending_labels = pl
local tuple_p = FLESH.KES.bindings[FLESH.KES.bindings.Tuple.records[FLESH.host_layer] ].records[FLESH.host_layer]
if (FLESH.capcheck(tuple_p, arg)) then FLESH:dispatch(arg,nil,arg.protocol.can.load) end
for i,e in pairs(FISH.pending_labels) do FLESH.KES:write_entry() end
for i,e in ipairs(prods) do
local s = FLESH.KES:resolve(e)
pl = {}
FISH.pending_labels = pl
FISH.def_grounded, FISH.def_isolated = false, false
if (s.protocol.get) then s = FLESH:dispatch(s, nil, s.protocol) end
for i,e in pairs(pl) do FLESH.KES:write_entry(i, s) end
end
FISH.pending_labels = {}
local s = FLESH.KES:resolve(self.state.creturn)
--FLESH.FISH.tail_context = FLESH.KES.layers[#FLESH.KES.layers]
if (s.protocol.get) then s = FLESH:dispatch(s, nil, s.protocol) end -- no TCO due to inderection
FLESH.KES:pop_layer()
return s
end]])
}
})
FLESH.KES:write_entry("Contain", {})
FLESH.KES:write_entry("Quote", {})
FLESH.KES:write_entry("Make", {})
FLESH.KES:write_entry("Membrane", { -- controls effect(or context layer mutations) propogation of current context layer relative to other context layers
protocol = {
["in"] = {call = capability_check},
["="] = {call = p_artifact([[]])}
},
state = {
get = p_artifact([[return function (self) -- do consider that this is definition of something, meaning it paints
local content = self.state.content
if content then
if (self.state.kind == 2) then -- grounded
-- store context, where this membrane was defined
FISH.def_grounded = true -- should have probably done direct Sequence manipulation, instead of using FISH, but on other side I should add call clause to sequence for specifying what mode to use
elseif (self.state.kind == 1) then -- dynamic (or wrapper)
-- neutral, inherit active behaviour. If user desire default behaviour
elseif (self.state.kind == 0) then -- isolated
FISH.def_isolated = true
-- outer implicit mutation from inner is restricted at definition context layer
else
return {
protocol = FLESH.assets.protocols.Error,
state = {
desc = "Membrane: Invalid kind"
}}
end end
return FLESH:dispatch(content, nil, content.protocol)
end]]),
}
})
FLESH.KES:write_entry("Negotiation", {
protocol = {
["in"] = {call = capability_check},
["="] = {call = p_artifact([[]])}
},
state = {
bindings = {
},
get = p_artifact([[return function (self)
-- resolve terms: we hold references in state, not data
local lt = self.state.lterm
local rt = self.state.rterm
return FLESH:dispatch(lt, rt, lt.protocol)
end]])
}
})
FLESH.KES:write_entry("false", {
protocol = FLESH.KES:resolve("Number").state,
state = {value = FLESH.KES:write_entry(nil,{external = 0})}})
FLESH.KES:write_entry("true", {
protocol = FLESH.KES:resolve("Number").state,
state = {value = FLESH.KES:write_entry(nil,{external = 1})}})
FLESH.KES:write_entry(host_name, {
protocol = FLESH.KES:resolve("String").state,
state = {value = FLESH.KES:write_entry(nil,{external = "Lua 5.5"})}})
local AST = { -- refactoring the Sequence generator for parser
GAP = function ()
return nil -- we don't have anything on here
end,
NUMBER = function (value)
return {
protocol = FLESH.KES.bindings[FLESH.KES.bindings.Number.records[FLESH.host_layer]].records[FLESH.host_layer].state,
state = value} end,
STRING = function (value)
return {
protocol = FLESH.KES.bindings[FLESH.KES.bindings.String.records[FLESH.host_layer]].records[FLESH.host_layer].state,
state = value} end,
LABEL = function (name)
return {
protocol = FLESH.KES.bindings[FLESH.KES.bindings.Label.records[FLESH.host_layer]].records[FLESH.host_layer].state,
state = {name = name}} end,
TUPLE = function (items) -- TODO: this one isn't a tuple, but a constructor for it that creates environment for writing, like Sequence
return { -- constructor
protocol = {get = p_artifact([[return function (self)
-- it's easier to do the lua way on lua side, though later I'll need to repalce it with Manifest that don't create another artifact like this
local items = self.state.items
local proc_items = table.create and table.create(#items) or {}
local labels = table.create and table.create(0,#items) or {}
for i,m in ipairs(items) do
local pl = {}
FISH.pending_labels = pl
proc_items[#proc_items+1] = FLESH:dispatch(m, nil, m.protocol)
for k,v in pairs(pl) do
labels[k] = #proc_items end end
return {
protocol = FLESH.KES.bindings[FLESH.KES.bindings.Tuple.records[FLESH.host_layer] ].records[FLESH.host_layer].state,
state = {items = proc_items, labels = labels}}
end]])},--ref to manifest for running an evaluation (that would be Sequence or Artifact).
state = {items = items}} end,
SEQUENCE = function (prods, creturn) -- Sequence holds quoted stuff, so we are not doing any actual construction
return {
protocol = {
get = p_artifact([[return function (self)
return FLESH.KES:write_entry(nil, {
protocol = FLESH.KES:resolve("Sequence").state,
state = {grounded = FISH.def_grounded or false, isolated = FISH.def_isolated or false, prods = self.state.prods, creturn = self.state.creturn, parent = FLESH.get_context()}
})
end]])},
state = {
prods = prods, creturn = creturn
}} end,
MEMBRANE = function (kind, content)
return {
protocol = FLESH.KES.bindings[FLESH.KES.bindings.Membrane.records[FLESH.host_layer]].records[FLESH.host_layer].state,
state = {kind = kind, content = content}} end,
NEGOTIATION = function (lterm, rterm) -- evaluation units
return {
protocol = FLESH.KES.bindings[FLESH.KES.bindings.Negotiation.records[FLESH.host_layer]].records[FLESH.host_layer].state,
state = {lterm = lterm, rterm = rterm}} end
}
local manifest = { -- manifest structure reference ()
template = {
protocol = { -- always table
can = {}, -- prio, find appropriate Label in front of manifest (maybe I should make this into tuple, that depicts the environment for the label in front of it)
call = nil, -- not found appropriate Label in can, do it if there is negotiation
get = nil, -- not found appropriate Label in can, do it anyways with (if no call) or without negotioation. this rule exist to describle labels
ask = nil, -- default case for respondrers, can work with call but it's heavily advised to not use with call, otherwise it will cause confusion)
},
state = { -- internal state of manifest, could only be used by protocol it's bundled with.
}}}
FLESH:reset()
return FLESH
end
return {
_VERSION="0.0.1",
newstate = newstate,
parse = (function ()
local table_invert = function (t)
local s={}
for k,v in pairs(t) do
s[v]=k
end
return s
end
-- Parser assets and constants
local TOKENTYPE = { -- enumeration for tokenizer
NUMBER = 0,
STRING = 1,
LABEL = 2,
MEMBRANE_OPEN = 3,
MEMBRANE_CLOSE = 4,
FINISH_ELEMENT = 5,
FINISH_ACTION = 6
}
local tokenname = table_invert(TOKENTYPE)
-- Internal AST node creators (for parser output)
--as state API matures, I should replace types with actual manifest creation commands from API.
--so I have plans to make ast nodes obsolete
--but this table will remain as interface that parser uses, I will just replace what those functions will do
local AST = { -- what parser uses to make "AST"
GAP = function () return {type = 0} end, -- part of language. helps in tracking gaps in tuples and sequences
NUMBER = function (value) return {type = 1, value = value} end,
STRING = function (value) return {type = 2, value = value} end,
LABEL = function (name) return {type = 3, name = name} end,
TUPLE = function (items) return {type = 4, items = items} end,
SEQUENCE = function (prods, creturn) return {type = 5, prods = prods, creturn = creturn} end,
MEMBRANE = function (kind, content) return {type = 6, kind = kind, content = content} end,
NEGOTIATION = function (lterm,rterm) return {type = 7, lterm = lterm, rterm = rterm} end
}
-- Internal: Recursive descent parsers (one per non-terminal)
local parse_term, parse_negotiation, parse_product, parse_sequence
parse_term = function(tokens, pos)
local tok
if pos <= #tokens then
tok = tokens[pos]
if tok.type == TOKENTYPE.MEMBRANE_OPEN then -- handle membrane: isolated | dynamic | grounded
local kind = tok.value
local seq, new_pos = parse_sequence(tokens, pos + 1)
pos = new_pos
local concl = tokens[pos]
if (concl == nil) then -- TODO: make this mess unified, there are a lot of repetition
error(
"parse_term ERROR: Expected membrane_close '"..
string.sub(")}]",tok.value+1,tok.value+1)..
"', but got nothing. Membrane begins "..
" at line:"..tostring(tok.at.line)..
", char:"..tostring(tok.at.char), 2)
elseif (concl.type ~= TOKENTYPE.MEMBRANE_CLOSE) then
error(
"parse_term ERROR: Expected membrane_close for some reason (probably parser bug, because it's implicitly checked), but got '"..
tokenname[concl.type]..
"' at line:"..tostring(concl.at.line)..
", char:"..tostring(concl.at.char), 2)
elseif (concl.value ~= kind) then
error(
"parse_term ERROR: Bracket missmatch. Forgot to open with '"..
string.sub("({[",tok.value+1,tok.value+1)..
"' membrane? Expected this membrane to close with '"..
string.sub(")}]",tok.value+1,tok.value+1)..
"' at line:"..tostring(concl.at.line)..
", char:"..tostring(concl.at.char), 2)
end
return AST.MEMBRANE(kind, seq), pos + 1
elseif tok.type == TOKENTYPE.NUMBER then -- handle NUMBER
return AST.NUMBER(tok.value), pos + 1