-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathFromTree.hs
833 lines (711 loc) · 28.9 KB
/
FromTree.hs
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
module Juvix.Compiler.Nockma.Translation.FromTree
( runCompilerWithAnoma,
runCompilerWithJuvix,
fromEntryPoint,
fromTreeTable,
ProgramCallingConvention (..),
CompilerOptions (..),
CompilerFunction (..),
FunctionId (..),
add,
dec,
mul,
sub,
pow2,
nockNatLiteral,
callStdlib,
appendRights,
foldTerms,
pathToArg,
)
where
import Juvix.Compiler.Nockma.Language.Path
import Juvix.Compiler.Nockma.Pretty
import Juvix.Compiler.Nockma.Stdlib
import Juvix.Compiler.Nockma.StdlibFunction
import Juvix.Compiler.Pipeline.EntryPoint
import Juvix.Compiler.Tree.Data.InfoTable qualified as Tree
import Juvix.Compiler.Tree.Language qualified as Tree
import Juvix.Compiler.Tree.Language.Rep
import Juvix.Prelude hiding (Atom, Path)
data ProgramCallingConvention
= ProgramCallingConventionJuvix
| ProgramCallingConventionAnoma
nockmaMemRep :: MemRep -> NockmaMemRep
nockmaMemRep = \case
MemRepTuple -> NockmaMemRepTuple
MemRepConstr -> NockmaMemRepConstr
MemRepTag -> NockmaMemRepConstr
MemRepUnit -> NockmaMemRepConstr
MemRepUnpacked {} -> NockmaMemRepConstr
data NockmaMemRepListConstr
= NockmaMemRepListConstrNil
| NockmaMemRepListConstrCons
deriving stock (Eq)
data NockmaMemRep
= NockmaMemRepConstr
| NockmaMemRepTuple
| NockmaMemRepList NockmaMemRepListConstr
newtype NockmaBuiltinTag
= NockmaBuiltinBool Bool
type UserFunctionId = Symbol
data FunctionId
= UserFunction UserFunctionId
| BuiltinFunction BuiltinFunctionId
deriving stock (Generic, Eq)
instance Hashable FunctionId
data BuiltinFunctionId
= -- | Not intended to be used, it exists only so the Enum and Bounded instances can be derived.
BuiltinPlaceholder
deriving stock (Eq, Enum, Bounded, Generic)
instance Hashable BuiltinFunctionId
newtype CompilerOptions = CompilerOptions
{_compilerOptionsEnableTrace :: Bool}
fromEntryPoint :: EntryPoint -> CompilerOptions
fromEntryPoint EntryPoint {..} =
CompilerOptions
{ _compilerOptionsEnableTrace = _entryPointDebug
}
data FunctionInfo = FunctionInfo
{ _functionInfoPath :: Path,
_functionInfoArity :: Natural
}
data CompilerCtx = CompilerCtx
{ _compilerFunctionInfos :: HashMap FunctionId FunctionInfo,
_compilerConstructorInfos :: ConstructorInfos,
_compilerOptions :: CompilerOptions
}
data ConstructorInfo = ConstructorInfo
{ _constructorInfoArity :: Natural,
_constructorInfoMemRep :: NockmaMemRep
}
type ConstructorInfos = HashMap Tree.Tag ConstructorInfo
data CompilerFunction = CompilerFunction
{ _compilerFunctionName :: FunctionId,
_compilerFunctionArity :: Natural,
_compilerFunction :: Sem '[Reader CompilerCtx] (Term Natural)
}
data StackId
= Args
| TempStack
| StandardLibrary
| FunctionsLibrary
deriving stock (Enum, Bounded, Eq, Show)
-- | A closure has the following structure:
-- [code totalArgsNum argsNum args], where
-- 1. code is code to run when fully applied.
-- 2. totalArgsNum is the number of arguments that the function
-- which created the closure expects.
-- 3. argsNum is the number of arguments that have been applied to the closure.
-- 4. args is the list of args that have been applied.
-- The length of the list should be argsNum.
data ClosurePathId
= ClosureCode
| ClosureTotalArgsNum
| ClosureArgsNum
| ClosureArgs
deriving stock (Bounded, Enum)
pathFromEnum :: (Enum a) => a -> Path
pathFromEnum = indexStack . fromIntegral . fromEnum
data ConstructorPathId
= ConstructorTag
| ConstructorArgs
deriving stock (Bounded, Enum)
constructorPath :: ConstructorPathId -> Path
constructorPath = pathFromEnum
data FunctionPathId
= FunctionCode
functionPath :: FunctionPathId -> Path
functionPath = \case
FunctionCode -> []
stackPath :: StackId -> Path
stackPath s = indexStack (fromIntegral (fromEnum s))
indexTuple :: Natural -> Natural -> Path
indexTuple len idx
| idx >= len = impossible
| otherwise =
let lastL
| idx == len - 1 = []
| otherwise = [L]
in replicate idx R ++ lastL
indexStack :: Natural -> Path
indexStack idx = replicate idx R ++ [L]
indexInStack :: StackId -> Natural -> Path
indexInStack s idx = stackPath s ++ indexStack idx
pathToArg :: Natural -> Path
pathToArg = indexInStack Args
makeLenses ''CompilerOptions
makeLenses ''CompilerFunction
makeLenses ''CompilerCtx
makeLenses ''ConstructorInfo
makeLenses ''FunctionInfo
termFromParts :: (Bounded p, Enum p) => (p -> Term Natural) -> Term Natural
termFromParts f = remakeList [f pi | pi <- allElements]
makeClosure :: (ClosurePathId -> Term Natural) -> Term Natural
makeClosure = termFromParts
makeConstructor :: (ConstructorPathId -> Term Natural) -> Term Natural
makeConstructor = termFromParts
makeFunction :: (FunctionPathId -> Term Natural) -> Term Natural
makeFunction f = f FunctionCode
foldTerms :: NonEmpty (Term Natural) -> Term Natural
foldTerms = foldr1 (#)
allConstructors :: Tree.InfoTable -> Tree.ConstructorInfo -> NonEmpty Tree.ConstructorInfo
allConstructors Tree.InfoTable {..} ci =
let indInfo = getInductiveInfo (ci ^. Tree.constructorInductive)
in nonEmpty' (getConstructorInfo'' <$> indInfo ^. Tree.inductiveConstructors)
where
getInductiveInfo :: Symbol -> Tree.InductiveInfo
getInductiveInfo s = _infoInductives ^?! at s . _Just
getConstructorInfo'' :: Tree.Tag -> Tree.ConstructorInfo
getConstructorInfo'' t = _infoConstrs ^?! at t . _Just
supportsListNockmaRep :: Tree.InfoTable -> Tree.ConstructorInfo -> Maybe NockmaMemRepListConstr
supportsListNockmaRep tab ci = case allConstructors tab ci of
c1 :| [c2]
| [0, 2] `elem` permutations ((^. Tree.constructorArgsNum) <$> [c1, c2]) -> Just $ case ci ^. Tree.constructorArgsNum of
0 -> NockmaMemRepListConstrNil
2 -> NockmaMemRepListConstrCons
_ -> impossible
| otherwise -> Nothing
_ -> Nothing
-- | Use `Tree.toNockma` before calling this function
fromTreeTable :: (Members '[Error JuvixError, Reader CompilerOptions] r) => ProgramCallingConvention -> Tree.InfoTable -> Sem r (Cell Natural)
fromTreeTable cc t = case t ^. Tree.infoMainFunction of
Just mainFun -> do
opts <- ask
return (fromTree opts mainFun t)
Nothing -> throw @JuvixError (error "TODO missing main")
where
fromTree :: CompilerOptions -> Tree.Symbol -> Tree.InfoTable -> Cell Natural
fromTree opts mainSym [email protected] {..} =
let funs = map compileFunction allFunctions
mkConstructorInfo :: Tree.ConstructorInfo -> ConstructorInfo
mkConstructorInfo [email protected] {..} =
ConstructorInfo
{ _constructorInfoArity = fromIntegral _constructorArgsNum,
_constructorInfoMemRep = rep
}
where
rep :: NockmaMemRep
rep = maybe r NockmaMemRepList (supportsListNockmaRep tab ci)
where
r = nockmaMemRep (memRep ci (getInductiveInfo (ci ^. Tree.constructorInductive)))
constrs :: ConstructorInfos
constrs = mkConstructorInfo <$> _infoConstrs
getInductiveInfo :: Symbol -> Tree.InductiveInfo
getInductiveInfo s = _infoInductives ^?! at s . _Just
in runCompilerWith cc opts constrs funs mainFun
where
mainFun :: CompilerFunction
mainFun = compileFunction (_infoFunctions ^?! at mainSym . _Just)
allFunctions :: [Tree.FunctionInfo]
allFunctions = filter notMain (toList _infoFunctions)
where
notMain :: Tree.FunctionInfo -> Bool
notMain Tree.FunctionInfo {..} = _functionSymbol /= mainSym
compileFunction :: Tree.FunctionInfo -> CompilerFunction
compileFunction Tree.FunctionInfo {..} =
CompilerFunction
{ _compilerFunctionName = UserFunction _functionSymbol,
_compilerFunctionArity = fromIntegral _functionArgsNum,
_compilerFunction = compile _functionCode
}
memRep :: Tree.ConstructorInfo -> Tree.InductiveInfo -> Tree.MemRep
memRep ci ind
| numArgs >= 1 && numConstrs == 1 = MemRepTuple
| otherwise = MemRepConstr
where
numConstrs = length (ind ^. Tree.inductiveConstructors)
numArgs = ci ^. Tree.constructorArgsNum
fromOffsetRef :: Tree.OffsetRef -> Natural
fromOffsetRef = fromIntegral . (^. Tree.offsetRefOffset)
compile :: forall r. (Members '[Reader CompilerCtx] r) => Tree.Node -> Sem r (Term Natural)
compile = \case
Tree.Binop b -> goBinop b
Tree.Unop b -> goUnop b
Tree.Const c -> return (goConst (c ^. Tree.nodeConstant))
Tree.MemRef c -> goMemRef (c ^. Tree.nodeMemRef)
Tree.AllocConstr c -> goAllocConstr c
Tree.AllocClosure c -> goAllocClosure c
Tree.ExtendClosure c -> goExtendClosure c
Tree.Call c -> goCall c
Tree.Branch b -> goBranch b
Tree.Case c -> goCase c
Tree.Save s -> goSave s
Tree.CallClosures {} -> impossible
where
goAllocConstr :: Tree.NodeAllocConstr -> Sem r (Term Natural)
goAllocConstr Tree.NodeAllocConstr {..} = do
args <- mapM compile _nodeAllocConstrArgs
info <- getConstructorInfo _nodeAllocConstrTag
let memrep = info ^. constructorInfoMemRep
return $ goConstructor memrep _nodeAllocConstrTag args
goMemRef :: Tree.MemRef -> Sem r (Term Natural)
goMemRef = \case
Tree.DRef d -> return (goDirectRef d)
Tree.ConstrRef Tree.Field {..} -> do
info <- getConstructorInfo _fieldTag
let memrep = info ^. constructorInfoMemRep
argIx = fromIntegral _fieldOffset
arity = info ^. constructorInfoArity
path = case memrep of
NockmaMemRepConstr ->
directRefPath _fieldRef
++ constructorPath ConstructorArgs
++ indexStack argIx
NockmaMemRepTuple ->
directRefPath _fieldRef
++ indexTuple arity argIx
NockmaMemRepList constr -> case constr of
NockmaMemRepListConstrNil -> impossible
NockmaMemRepListConstrCons -> directRefPath _fieldRef ++ indexTuple 2 argIx
return (OpAddress # path)
where
goDirectRef :: Tree.DirectRef -> Term Natural
goDirectRef dr = OpAddress # directRefPath dr
goConst :: Tree.Constant -> Term Natural
goConst = \case
Tree.ConstInt i
| i < 0 -> error "negative integer"
| otherwise -> nockIntegralLiteral i
Tree.ConstBool i -> nockBoolLiteral i
Tree.ConstString {} -> stringsErr
Tree.ConstUnit -> OpQuote # constUnit
Tree.ConstVoid -> OpQuote # constVoid
goSave :: Tree.NodeSave -> Sem r (Term Natural)
goSave Tree.NodeSave {..} = do
arg <- compile _nodeSaveArg
body <- compile _nodeSaveBody
return (withTemp arg body)
goCase :: Tree.NodeCase -> Sem r (Term Natural)
goCase c = do
def <- mapM compile (c ^. Tree.nodeCaseDefault)
arg <- compile (c ^. Tree.nodeCaseArg)
branches <-
sequence
[ do
let withTemp' t
| b ^. Tree.caseBranchSave = withTemp arg t
| otherwise = t
body' <- withTemp' <$> compile (b ^. Tree.caseBranchBody)
return (b ^. Tree.caseBranchTag, body')
| b <- c ^. Tree.nodeCaseBranches
]
caseCmd arg def branches
goBranch :: Tree.NodeBranch -> Sem r (Term Natural)
goBranch Tree.NodeBranch {..} = do
arg <- compile _nodeBranchArg
iftrue <- compile _nodeBranchTrue
iffalse <- compile _nodeBranchFalse
return (branch arg iftrue iffalse)
goUnop :: Tree.NodeUnop -> Sem r (Term Natural)
goUnop Tree.NodeUnop {..} = do
arg <- compile _nodeUnopArg
case _nodeUnopOpcode of
Tree.OpShow -> stringsErr
Tree.OpStrToInt -> stringsErr
Tree.OpFail -> return crash
Tree.OpTrace -> goTrace arg
Tree.OpArgsNum ->
let getF f = getClosureField f arg
in return (sub (getF ClosureTotalArgsNum) (getF ClosureArgsNum))
goTrace :: Term Natural -> Sem r (Term Natural)
goTrace arg = do
enabled <- asks (^. compilerOptions . compilerOptionsEnableTrace)
return $
if
| enabled -> OpTrace # arg # arg
| otherwise -> arg
goBinop :: Tree.NodeBinop -> Sem r (Term Natural)
goBinop Tree.NodeBinop {..} = do
arg1 <- compile _nodeBinopArg1
arg2 <- compile _nodeBinopArg2
let args = [arg1, arg2]
case _nodeBinopOpcode of
Tree.IntAdd -> return (callStdlib StdlibAdd args)
Tree.IntSub -> return (callStdlib StdlibSub args)
Tree.IntMul -> return (callStdlib StdlibMul args)
Tree.IntDiv -> return (callStdlib StdlibDiv args)
Tree.IntMod -> return (callStdlib StdlibMod args)
Tree.IntLt -> return (callStdlib StdlibLt args)
Tree.IntLe -> return (callStdlib StdlibLe args)
Tree.OpSeq -> return (OpHint # (nockNil' # arg1) # arg2)
Tree.ValEq -> testEq _nodeBinopArg1 _nodeBinopArg2
Tree.StrConcat -> stringsErr
goAllocClosure :: Tree.NodeAllocClosure -> Sem r (Term Natural)
goAllocClosure Tree.NodeAllocClosure {..} = do
let fun = UserFunction _nodeAllocClosureFunSymbol
fpath <- getFunctionPath fun
farity <- getFunctionArity fun
args <- mapM compile _nodeAllocClosureArgs
return . makeClosure $ \case
ClosureCode -> OpAddress # fpath
ClosureTotalArgsNum -> nockNatLiteral farity
ClosureArgsNum -> nockIntegralLiteral (length args)
ClosureArgs -> remakeList args
goExtendClosure :: Tree.NodeExtendClosure -> Sem r (Term Natural)
goExtendClosure = extendClosure
goCall :: Tree.NodeCall -> Sem r (Term Natural)
goCall Tree.NodeCall {..} = do
newargs <- mapM compile _nodeCallArgs
case _nodeCallType of
Tree.CallFun fun -> callFunWithArgs (UserFunction fun) newargs
Tree.CallClosure f -> do
f' <- compile f
let argsNum = getClosureField ClosureArgsNum f'
oldArgs = getClosureField ClosureArgs f'
fcode = getClosureField ClosureCode f'
posOfArgsNil = appendRights emptyPath argsNum
allArgs = replaceSubterm' oldArgs posOfArgsNil (remakeList newargs)
return (OpApply # replaceArgsWithTerm allArgs # fcode)
appendRights :: Path -> Term Natural -> Term Natural
appendRights path n = dec (mul (pow2 n) (OpInc # OpQuote # path))
pushTemp :: Term Natural -> Term Natural
pushTemp toBePushed =
remakeList
[ let p = OpAddress # stackPath s
in if
| TempStack == s -> toBePushed # p
| otherwise -> p
| s <- allElements
]
withTemp :: Term Natural -> Term Natural -> Term Natural
withTemp toBePushed body =
OpSequence # pushTemp toBePushed # body
testEq :: (Members '[Reader CompilerCtx] r) => Tree.Node -> Tree.Node -> Sem r (Term Natural)
testEq a b = do
a' <- compile a
b' <- compile b
return (OpEq # a' # b')
nockNatLiteral :: Natural -> Term Natural
nockNatLiteral = nockIntegralLiteral
nockIntegralLiteral :: (Integral a) => a -> Term Natural
nockIntegralLiteral = (OpQuote #) . toNock @Natural . fromIntegral
extendClosure ::
(Members '[Reader CompilerCtx] r) =>
Tree.NodeExtendClosure ->
Sem r (Term Natural)
extendClosure Tree.NodeExtendClosure {..} = do
args <- mapM compile _nodeExtendClosureArgs
closure <- compile _nodeExtendClosureFun
let argsNum = getClosureField ClosureArgsNum closure
oldArgs = getClosureField ClosureArgs closure
fcode = getClosureField ClosureCode closure
posOfArgsNil = appendRights emptyPath argsNum
allArgs = replaceSubterm' oldArgs posOfArgsNil (remakeList args)
newArgsNum = add argsNum (nockIntegralLiteral (length _nodeExtendClosureArgs))
return . makeClosure $ \case
ClosureCode -> fcode
ClosureTotalArgsNum -> getClosureField ClosureTotalArgsNum closure
ClosureArgsNum -> newArgsNum
ClosureArgs -> allArgs
-- Calling convention for Anoma stdlib
--
-- [push
-- [seq [@ locStdlib] getF] :: Obtain the function f within the stdlib.
-- :: locStdlib is the location of the stdlib in the subject
-- :: getF is a term that fetches f relative to the stdlib
-- [call L :: eval formula @ L of the following
-- [replace [RL :: edit at axis RL
-- [seq [@ R] :: evaluate the a formula in the original context without f on it
-- a]] :: the formula giving a goes here
-- @ L] :: this whole replace is editing what's at axis L, i.e. what was
-- ]
-- ]
callStdlib :: StdlibFunction -> [Term Natural] -> Term Natural
callStdlib fun args =
let fPath = stdlibPath fun
getFunCode = OpAddress # stackPath StandardLibrary >># fPath
adjustArgs = case nonEmpty args of
Just args' -> OpReplace # ([R, L] # ((OpAddress # [R]) >># foldTerms args')) # (OpAddress # [L])
Nothing -> OpAddress # [L]
callFn = OpCall # [L] # adjustArgs
callCell = set cellCall (Just meta) (OpPush #. (getFunCode # callFn))
meta =
StdlibCall
{ _stdlibCallArgs = maybe nockNil' foldTerms (nonEmpty args),
_stdlibCallFunction = fun
}
in TermCell callCell
constUnit :: Term Natural
constUnit = constVoid
constVoid :: Term Natural
constVoid = TermAtom nockVoid
directRefPath :: Tree.DirectRef -> Path
directRefPath = \case
Tree.ArgRef a -> pathToArg (fromOffsetRef a)
Tree.TempRef Tree.RefTemp {..} ->
tempRefPath
(fromIntegral (fromJust _refTempTempHeight))
(fromOffsetRef _refTempOffsetRef)
tempRefPath :: Natural -> Natural -> Path
tempRefPath tempHeight off = indexInStack TempStack (tempHeight - off - 1)
nockmaBuiltinTag :: Tree.BuiltinDataTag -> NockmaBuiltinTag
nockmaBuiltinTag = \case
Tree.TagTrue -> NockmaBuiltinBool True
Tree.TagFalse -> NockmaBuiltinBool False
Tree.TagReturn -> impossible
Tree.TagBind -> impossible
Tree.TagWrite -> impossible
Tree.TagReadLn -> impossible
-- | Generic constructors are encoded as [tag args], where args is a
-- nil terminated list.
goConstructor :: NockmaMemRep -> Tree.Tag -> [Term Natural] -> Term Natural
goConstructor mr t args = case t of
Tree.BuiltinTag b -> case nockmaBuiltinTag b of
NockmaBuiltinBool v -> nockBoolLiteral v
Tree.UserTag tag -> case mr of
NockmaMemRepConstr ->
makeConstructor $ \case
ConstructorTag -> OpQuote # (fromIntegral (tag ^. Tree.tagUserWord) :: Natural)
ConstructorArgs -> remakeList args
NockmaMemRepTuple -> foldTerms (nonEmpty' args)
NockmaMemRepList constr -> case constr of
NockmaMemRepListConstrNil
| null args -> remakeList []
| otherwise -> impossible
NockmaMemRepListConstrCons -> case args of
[l, r] -> TCell l r
_ -> impossible
unsupported :: Text -> a
unsupported thing = error ("The Nockma backend does not support " <> thing)
stringsErr :: a
stringsErr = unsupported "strings"
-- | Computes a - b
sub :: Term Natural -> Term Natural -> Term Natural
sub a b = callStdlib StdlibSub [a, b]
makeList :: [Term Natural] -> Term Natural
makeList ts = foldTerms (ts `prependList` pure (TermAtom nockNil))
remakeList :: (Foldable l) => l (Term Natural) -> Term Natural
remakeList ts = foldTerms (toList ts `prependList` pure (OpQuote # nockNil'))
-- | Initialize the stack. The resulting term is intended to be evaulated
-- against a subject that contains function arguments.
initStackWithArgs :: [Term Natural] -> [Term Natural] -> Term Natural
initStackWithArgs defs getArgs = remakeList (initSubStack <$> allElements)
where
initSubStack :: StackId -> Term Natural
initSubStack = \case
Args -> remakeList getArgs
TempStack -> OpQuote # nockNil'
StandardLibrary -> OpQuote # stdlib
FunctionsLibrary -> OpQuote # makeList defs
-- | Initialize the stack. Populate the FunctionsLibrary with the passed terms.
initStack :: [Term Natural] -> Term Natural
initStack defs = makeList (initSubStack <$> allElements)
where
initSubStack :: StackId -> Term Natural
initSubStack = \case
Args -> nockNil'
TempStack -> nockNil'
StandardLibrary -> stdlib
FunctionsLibrary -> makeList defs
runCompilerWithAnoma :: CompilerOptions -> ConstructorInfos -> [CompilerFunction] -> CompilerFunction -> Cell Natural
runCompilerWithAnoma = runCompilerWith ProgramCallingConventionAnoma
runCompilerWithJuvix :: CompilerOptions -> ConstructorInfos -> [CompilerFunction] -> CompilerFunction -> Cell Natural
runCompilerWithJuvix = runCompilerWith ProgramCallingConventionJuvix
runCompilerWith :: ProgramCallingConvention -> CompilerOptions -> ConstructorInfos -> [CompilerFunction] -> CompilerFunction -> Cell Natural
runCompilerWith callingConvention opts constrs libFuns mainFun = run . runReader compilerCtx $ mkEntryPoint
where
allFuns :: NonEmpty CompilerFunction
allFuns = mainFun :| libFuns ++ (builtinFunction <$> allElements)
compilerCtx :: CompilerCtx
compilerCtx =
CompilerCtx
{ _compilerFunctionInfos = functionInfos,
_compilerConstructorInfos = constrs,
_compilerOptions = opts
}
compiledFuns :: NonEmpty (Term Natural)
compiledFuns =
makeFunction'
<$> ( run . runReader compilerCtx . (^. compilerFunction)
<$> allFuns
)
makeFunction' :: Term Natural -> Term Natural
makeFunction' c = makeFunction $ \case
FunctionCode -> c
functionInfos :: HashMap FunctionId FunctionInfo
functionInfos = hashMap (run (runInputNaturals (toList <$> userFunctions)))
userFunctions :: (Members '[Input Natural] r) => Sem r (NonEmpty (FunctionId, FunctionInfo))
userFunctions = forM allFuns $ \CompilerFunction {..} -> do
i <- input
return
( _compilerFunctionName,
FunctionInfo
{ _functionInfoPath = indexInStack FunctionsLibrary i,
_functionInfoArity = _compilerFunctionArity
}
)
makeAnomaFun :: (Members '[Reader CompilerCtx] r) => Sem r (Cell Natural)
makeAnomaFun = do
entryTerm <- callFun (mainFun ^. compilerFunctionName)
let mainArity :: Natural
mainArity = mainFun ^. compilerFunctionArity
args :: [Term Natural]
args = [OpAddress # [R, L] ++ indexTuple mainArity (pred i) | i <- [1 .. mainArity]]
wrapperCode :: Term Natural
wrapperCode = OpApply # (initStackWithArgs (toList compiledFuns) args) # (OpQuote # entryTerm)
argsPlaceholder :: Term Natural
argsPlaceholder = nockNil'
env :: Term Natural
env = nockNil'
return (wrapperCode #. (argsPlaceholder # env))
makeJuvixFun :: (Members '[Reader CompilerCtx] r) => Sem r (Cell Natural)
makeJuvixFun = do
entryTerm <- callFunWithArgs (mainFun ^. compilerFunctionName) []
return (initStack (toList compiledFuns) #. entryTerm)
mkEntryPoint = case callingConvention of
ProgramCallingConventionAnoma -> makeAnomaFun
ProgramCallingConventionJuvix -> makeJuvixFun
builtinFunction :: BuiltinFunctionId -> CompilerFunction
builtinFunction = \case
BuiltinPlaceholder ->
CompilerFunction
{ _compilerFunctionName = BuiltinFunction BuiltinPlaceholder,
_compilerFunctionArity = 0,
_compilerFunction = return crash
}
-- | Call a function. Arguments to the function are assumed to be in the Args stack
callFun ::
(Members '[Reader CompilerCtx] r) =>
FunctionId ->
Sem r (Term Natural)
callFun fun = do
fpath <- getFunctionPath fun
let p' = fpath ++ functionPath FunctionCode
return (OpCall # p' # (OpAddress # emptyPath))
-- | Call a function with the passed arguments
callFunWithArgs ::
(Members '[Reader CompilerCtx] r) =>
FunctionId ->
[Term Natural] ->
Sem r (Term Natural)
callFunWithArgs fun args = (replaceArgs args >>#) <$> callFun fun
replaceArgsWithTerm :: Term Natural -> Term Natural
replaceArgsWithTerm term =
remakeList
[ if
| Args == s -> term
| otherwise -> OpAddress # stackPath s
| s <- allElements
]
replaceArgs :: [Term Natural] -> Term Natural
replaceArgs args =
remakeList
[ if
| Args == s -> remakeList args
| otherwise -> OpAddress # stackPath s
| s <- allElements
]
getFunctionPath :: (Members '[Reader CompilerCtx] r) => FunctionId -> Sem r Path
getFunctionPath funName = asks (^?! compilerFunctionInfos . at funName . _Just . functionInfoPath)
evaluated :: Term Natural -> Term Natural
evaluated t = OpApply # (OpAddress # emptyPath) # t
-- | obj[eval(relPath)] := newVal
-- relPath is relative to obj
replaceSubterm' :: Term Natural -> Term Natural -> Term Natural -> Term Natural
replaceSubterm' obj relPath newVal =
evaluated $ (OpQuote # OpReplace) # ((relPath # (OpQuote # newVal)) # (OpQuote # obj))
builtinTagToTerm :: NockmaBuiltinTag -> Term Natural
builtinTagToTerm = \case
NockmaBuiltinBool v -> nockBoolLiteral v
constructorTagToTerm :: Tree.Tag -> Term Natural
constructorTagToTerm = \case
Tree.UserTag t -> OpQuote # toNock (fromIntegral (t ^. Tree.tagUserWord) :: Natural)
Tree.BuiltinTag b -> builtinTagToTerm (nockmaBuiltinTag b)
caseCmd ::
forall r.
(Members '[Reader CompilerCtx] r) =>
Term Natural ->
Maybe (Term Natural) ->
[(Tree.Tag, Term Natural)] ->
Sem r (Term Natural)
caseCmd arg defaultBranch = \case
[] -> return (fromJust defaultBranch)
(tag, b) : bs -> case tag of
Tree.BuiltinTag t -> case nockmaBuiltinTag t of
NockmaBuiltinBool v -> return (goBoolTag v b bs)
Tree.UserTag {} -> do
rep <- getConstructorMemRep tag
case rep of
NockmaMemRepConstr -> goRepConstr tag b bs
NockmaMemRepTuple
| null bs, isNothing defaultBranch -> return b
| otherwise -> error "redundant branch. Impossible?"
NockmaMemRepList constr -> do
bs' <- mapM (firstM asNockmaMemRepListConstr) bs
return (goRepList ((constr, b) :| bs'))
where
goRepConstr ::
Tree.Tag ->
Term Natural ->
[(Tree.Tag, Term Natural)] ->
Sem r (Term Natural)
goRepConstr tag b bs = do
let cond :: Term Natural =
OpEq
# constructorTagToTerm tag
# (getConstructorField ConstructorTag arg)
elseBr <- caseCmd arg defaultBranch bs
return (branch cond b elseBr)
asNockmaMemRepListConstr :: Tree.Tag -> Sem r NockmaMemRepListConstr
asNockmaMemRepListConstr tag = case tag of
Tree.UserTag {} -> do
rep <- getConstructorMemRep tag
case rep of
NockmaMemRepList constr -> return constr
_ -> impossible
Tree.BuiltinTag {} -> impossible
goBoolTag ::
Bool ->
Term Natural ->
[(Tree.Tag, Term Natural)] ->
(Term Natural)
goBoolTag v b bs =
let otherBranch = fromMaybe crash (firstJust f bs <|> defaultBranch)
in if
| v -> branch arg b otherBranch
| otherwise -> branch arg otherBranch b
where
f :: (Tree.Tag, Term Natural) -> Maybe (Term Natural)
f (tag', br) = case tag' of
Tree.UserTag {} -> impossible
Tree.BuiltinTag tag -> case nockmaBuiltinTag tag of
NockmaBuiltinBool v' -> guard (v /= v') $> br
goRepList :: NonEmpty (NockmaMemRepListConstr, Term Natural) -> Term Natural
goRepList ((c, b) :| bs) =
let cond = OpIsCell # arg
otherBranch = fromMaybe crash (firstJust f bs <|> defaultBranch)
in case c of
NockmaMemRepListConstrCons -> branch cond b otherBranch
NockmaMemRepListConstrNil -> branch cond otherBranch b
where
f :: (NockmaMemRepListConstr, Term Natural) -> Maybe (Term Natural)
f (c', br) = guard (c /= c') $> br
branch ::
Term Natural ->
Term Natural ->
Term Natural ->
Term Natural
branch cond t f = OpIf # cond # t # f
getFunctionArity :: (Members '[Reader CompilerCtx] r) => FunctionId -> Sem r Natural
getFunctionArity s = asks (^?! compilerFunctionInfos . at s . _Just . functionInfoArity)
getConstructorInfo :: (Members '[Reader CompilerCtx] r) => Tree.Tag -> Sem r ConstructorInfo
getConstructorInfo tag = asks (^?! compilerConstructorInfos . at tag . _Just)
getClosureField :: ClosurePathId -> Term Natural -> Term Natural
getClosureField = getField
getConstructorField :: ConstructorPathId -> Term Natural -> Term Natural
getConstructorField = getField
getField :: (Enum field) => field -> Term Natural -> Term Natural
getField field t = t >># (OpAddress # pathFromEnum field)
getConstructorMemRep :: (Members '[Reader CompilerCtx] r) => Tree.Tag -> Sem r NockmaMemRep
getConstructorMemRep tag = (^. constructorInfoMemRep) <$> getConstructorInfo tag
crash :: Term Natural
crash = (OpAddress # OpAddress # OpAddress)
mul :: Term Natural -> Term Natural -> Term Natural
mul a b = callStdlib StdlibMul [a, b]
pow2 :: Term Natural -> Term Natural
pow2 = callStdlib StdlibPow2 . pure
add :: Term Natural -> Term Natural -> Term Natural
add a b = callStdlib StdlibAdd [a, b]
dec :: Term Natural -> Term Natural
dec = callStdlib StdlibDec . pure