-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_conversion.c
More file actions
2234 lines (2121 loc) · 77.3 KB
/
lambda_conversion.c
File metadata and controls
2234 lines (2121 loc) · 77.3 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
/*
* CEKF - VM supporting amb
* Copyright (C) 2022-2023 Bill Hails
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* @file lambda_conversion.c
*
* conversion of the AST generated by the parser
* to an intermediate "plain" lambda calculus which
* will then be fed into the type checker and the
* A-Normal Form converter.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "ast_debug.h"
#include "common.h"
#include "lambda_conversion.h"
#include "lambda_helper.h"
#include "lazy_substitution.h"
#include "pratt_scanner.h"
#include "print_generator.h"
#include "symbols.h"
#include "tpmc_logic.h"
#include "tpmc_mermaid.h"
#include "utils.h"
char *lambda_conversion_function = NULL; // set by --dump-lambda flag
static LamBindings *convertFuncDefs(AstDefinitions *, LamContext *);
static LamArgs *convertExpressions(AstExpressions *, LamContext *);
static LamSequence *convertSequence(AstExpressions *, LamContext *);
static LamBindings *prependDefinition(AstDefinition *, LamContext *,
LamBindings *);
static LamBindings *prependDefine(AstDefine *, LamContext *, LamBindings *);
static LamExp *convertExpression(AstExpression *, LamContext *);
static bool typeHasFields(AstTypeBody *);
static LamTypeDefList *collectTypeDefs(AstDefinitions *, LamContext *);
static void collectAliases(AstDefinitions *, LamContext *);
static LamTypeConstructor *convertTypeConstructor(AstTypeConstructor *,
LamTypeSig *, int, int, bool,
LamContext *);
static void collectTypeInfo(HashSymbol *, AstTypeConstructorArgs *,
LamTypeConstructor *, bool, int, int, int,
LamContext *);
static LamTypeConstructorArgs *convertAstTypeList(AstTypeList *, LamContext *);
static LamTypeConstructorArgs *convertAstTypeMap(AstTypeMap *, LamContext *);
static LamTypeConstructorArgs *
convertAstTypeConstructorArgs(AstTypeConstructorArgs *, LamContext *);
static LamExp *convertNest(AstNest *, LamContext *);
static LamExp *lamConvert(AstDefinitions *, AstExpressions *, LamContext *);
static LamExp *convertSymbol(ParserInfo, HashSymbol *, LamContext *);
static LamExp *convertAnnotatedSymbol(AstAnnotatedSymbol *, LamContext *);
static LamExp *convertCut(AstExpression *, LamContext *);
#ifdef DEBUG_LAMBDA_CONVERT
#include "debugging_on.h"
#else
#include "debugging_off.h"
#endif
/**
* @brief Creates a value that can be returned in the case of an error
*/
static LamExp *lamExpError(ParserInfo I) {
return newLamExp_Var(I, errorSymbol());
}
/**
* @brief Converts an AST program to a lambda expression.
*
* This is the top-level public entry point to the lambda conversion
* code.
*
* @param prog The AST program to convert.
* @return The resulting lambda expression.
*/
LamExp *lamConvertProg(AstProg *prog) {
ENTER(lamConvertProg);
LamContext *env = newLamContext(CPI(prog), NULL);
int save = PROTECT(env);
LamExp *result = lamConvert(prog->preamble, prog->body, env);
UNPROTECT(save);
LEAVE(lamConvertProg);
return result;
}
/**
* @brief Converts an AST nest to a lambda expression.
*
* @param nest The AST nest to convert.
* @param env The lambda context to use.
* @return The resulting lambda expression.
*/
static LamExp *convertNest(AstNest *nest, LamContext *env) {
ENTER(convertNest);
env = newLamContext(CPI(nest), env);
int save = PROTECT(env);
LamExp *result = lamConvert(nest->definitions, nest->expressions, env);
PROTECT(result);
UNPROTECT(save);
LEAVE(convertNest);
return result;
}
static LamExp *convertCut(AstExpression *value, LamContext *env) {
LamExp *exp = convertExpression(value, env);
int save = PROTECT(exp);
LamExp *result = newLamExp_Cut(CPI(value), exp);
UNPROTECT(save);
return result;
}
/**
* @brief Adds constructor information to the lambda context.
*
* @param context The lambda context to modify.
* @param symbol The name of the constructor.
* @param info The constructor information to add.
* @return void
*/
static void addConstructorInfoToLamContext(LamContext *context,
HashSymbol *symbol,
LamTypeConstructorInfo *info) {
setLamInfoTable(context->frame, symbol, info);
}
static void separateLambdas(LamBindings *funcDefs, LamBindings **lambdas,
LamBindings **other) {
if (funcDefs != NULL) {
separateLambdas(funcDefs->next, lambdas, other);
if (funcDefs->val->type == LAMEXP_TYPE_LAM) {
*lambdas = newLamBindings(CPI(funcDefs), funcDefs->var,
funcDefs->val, *lambdas);
PROTECT(*lambdas);
} else {
*other = newLamBindings(CPI(funcDefs), funcDefs->var, funcDefs->val,
*other);
PROTECT(*other);
}
}
}
static void appendLamBindings(LamBindings **lambdas, LamBindings **other) {
if (*lambdas == NULL) {
*lambdas = *other;
} else if (*other != NULL) {
LamBindings *walker = *lambdas;
while (walker->next != NULL) {
walker = walker->next;
}
walker->next = *other;
}
}
/**
* @brief Hoists function definitions to the front of the letrec bindings.
*
* @param funcDefs The letrec bindings to hoist.
* @return The resulting letrec bindings with functions hoisted to the front.
*/
__attribute__((unused)) static LamBindings *
hoistFunctionDefinitions(LamBindings *funcDefs) {
int save = PROTECT(funcDefs);
LamBindings *lambdas = NULL;
LamBindings *other = NULL;
separateLambdas(funcDefs, &lambdas, &other);
appendLamBindings(&lambdas, &other);
UNPROTECT(save);
return lambdas;
}
/**
* @brief Workhorse routine that converts various nest-like scenarios to a
* common LamExp
*
* @param definitions If there were definitions.
* @param expressions The AST expressions to convert.
* @param env The lambda context to use.
* @return The resulting lambda expression.
*/
static LamExp *lamConvert(AstDefinitions *definitions,
AstExpressions *expressions, LamContext *env) {
ENTER(lamConvert);
// record aliases in env
collectAliases(definitions, env);
// convert and collect all the typeDefs (also adds them to env)
LamTypeDefList *typeDefList = collectTypeDefs(definitions, env);
int save = PROTECT(typeDefList);
// convert and collect all the definitions:
// [funcs and vars]
LamBindings *defsList = convertFuncDefs(definitions, env);
PROTECT(defsList);
// hoist function definitions to the front:
// [funcs]
// [vars]
LamBindings *funcDefsList = NULL;
LamBindings *varDefsList = NULL;
separateLambdas(defsList, &funcDefsList, &varDefsList); // PROTECTS them
// prepend print functions:
// [printers] [funcs]
// [vars]
funcDefsList = makePrintFunctions(typeDefList, funcDefsList, env);
PROTECT(funcDefsList);
// convert the body sequence
LamSequence *body = convertSequence(expressions, env);
PROTECT(body);
// promote the body sequence to a LamExp
// [[body]]
LamExp *letRecBody =
(body == NULL) ? NULL : newLamExp_Sequence(CPI(body), body);
PROTECT(letRecBody);
LamExp *result = NULL;
if (varDefsList != NULL) {
// prepend variable definitions to the letrec body
// [vars] [[body]]
letRecBody =
makeLamExp_LetStar(CPI(varDefsList), varDefsList, letRecBody);
PROTECT(letRecBody);
}
// if there are functions, create a letrec, else just use the body
if (funcDefsList != NULL) {
// [[printers] [funcs] [vars] [[body]]]
if (letRecBody == NULL) {
// a program with definitions but no body needs a valid
// expression so the letrec is well-formed
letRecBody = newLamExp_Stdint(CPI(funcDefsList), 0);
PROTECT(letRecBody);
}
result = makeLamExp_LetRec(CPI(letRecBody), funcDefsList, letRecBody);
} else {
// [vars] [[body]]
result = letRecBody;
}
PROTECT(result);
// prepend typeDefs if any
if (typeDefList != NULL) {
// [typeDefs] [[printers] [funcs] [vars] [[body]]]
result = makeLamExp_TypeDefs(CPI(typeDefList), typeDefList, result);
}
// cleanup and return
UNPROTECT(save);
LEAVE(lamConvert);
return result;
}
/**
* @brief Converts an AST If Expression to a lambda expression.
*
* @param iff The If Expression to convert.
* @param context The lambda context to use.
* @return The resulting lambda expression.
*/
static LamExp *lamConvertIff(AstIff *iff, LamContext *context) {
ENTER(lamConvertIff);
LamExp *test = convertExpression(iff->test, context);
int save = PROTECT(test);
LamExp *consequent = convertNest(iff->consequent, context);
PROTECT(consequent);
LamExp *alternative = convertNest(iff->alternative, context);
PROTECT(alternative);
LamExp *result = makeLamExp_Iff(CPI(test), test, consequent, alternative);
UNPROTECT(save);
LEAVE(lamConvertIff);
return result;
}
/**
* @brief Converts an AST Print Expression to a lambda expression.
*
* @param print The AST Print Expression to convert.
* @param context The lambda context to use.
* @return The resulting lambda expression.
*/
static LamExp *lamConvertPrint(AstPrint *print, LamContext *context) {
ENTER(lamConvertPrint);
LamExp *exp = convertExpression(print->exp, context);
int save = PROTECT(exp);
LamExp *result = makeLamExp_Print(CPI(exp), exp);
UNPROTECT(save);
LEAVE(lamConvertPrint);
return result;
}
/**
* @brief Converts an AST TypeOf Expression to a lambda expression that returns
* the type as a string.
*
* @param typeOfExp The AST TypeOf Expression to convert.
* @param context The lambda context to use.
* @return The resulting lambda expression.
*/
static LamExp *lamConvertTypeOf(AstTypeOf *typeOfExp, LamContext *context) {
ENTER(lamConvertTypeOf);
LamExp *exp = convertExpression(typeOfExp->exp, context);
int save = PROTECT(exp);
LamExp *result = makeLamExp_TypeOf(CPI(exp), exp);
UNPROTECT(save);
LEAVE(lamConvertTypeOf);
return result;
}
/**
* @brief Converts an AST Tuple Expression to a lambda expression that
* constructs a tuple.
*
* @param tuple The Tuple Expression to convert.
* @param env The lambda context to use.
* @return The resulting lambda expression.
*/
static LamExp *lamConvertTuple(ParserInfo PI, AstExpressions *tuple,
LamContext *env) {
LamArgs *expressions = convertExpressions(tuple, env);
int save = PROTECT(expressions);
LamExp *res = newLamExp_MakeTuple(PI, expressions);
UNPROTECT(save);
return res;
}
/**
* @brief Checks if a particular definition is a macro, and if so, adds it to
* the macro table in the environment.
*
* @param definition The AST definition to check.
* @param env The lambda context to use.
* @return void
*/
static void checkLazy(AstDefinition *definition, LamContext *env) {
if (definition->type == AST_DEFINITION_TYPE_LAZY) {
setSymbolSet(env->macros, getAstDefinition_Lazy(definition)->name);
}
}
/**
* @brief Converts a list of AST function definitions to a list of letrec
* bindings.
*
* @param definitions The AST function definitions to convert.
* @param env The lambda context to use.
* @return The resulting list of letrec bindings.
*/
static LamBindings *convertFuncDefs(AstDefinitions *definitions,
LamContext *env) {
ENTER(convertFuncDefs);
if (definitions == NULL) {
LEAVE(convertFuncDefs);
return NULL;
}
checkLazy(definitions->definition, env);
LamBindings *next = convertFuncDefs(definitions->next, env);
int save = PROTECT(next);
LamBindings *this = prependDefinition(definitions->definition, env, next);
UNPROTECT(save);
LEAVE(convertFuncDefs);
return this;
}
/**
* @brief Converts a list of AST type symbols to a list of lambda type signature
* arguments.
*
* @param symbols The AST type symbols to convert.
* @return The resulting lambda type signature arguments.
*/
static LamTypeSigArgs *convertTypeSymbols(AstTypeSymbols *symbols) {
if (symbols == NULL)
return NULL;
LamTypeSigArgs *next = convertTypeSymbols(symbols->next);
int save = PROTECT(next);
LamTypeSigArgs *this =
newLamTypeSigArgs(CPI(symbols), symbols->typeSymbol, next);
UNPROTECT(save);
return this;
}
/**
* @brief Converts an AST Type Signature to a lambda type signature.
*
* @param typeSig The AST Type Signature to convert.
* @return The resulting lambda type signature.
*/
static LamTypeSig *convertTypeSig(AstTypeSig *typeSig) {
LamTypeSigArgs *args = convertTypeSymbols(typeSig->typeSymbols);
int save = PROTECT(args);
LamTypeSig *res = newLamTypeSig(CPI(typeSig), typeSig->symbol, args);
UNPROTECT(save);
return res;
}
/**
* @brief Checks to see if a symbol is an alias for a type constructor
* invocation.
*
* @param los The AST LookUpOrSymbol to check.
* @param env The lambda context to use.
* @return The resulting lambda type constructor type, or NULL if not found.
*/
static LamTypeConstructorType *expandSymbolAlias(AstLookUpOrSymbol *los,
LamContext *env) {
switch (los->type) {
case AST_LOOKUPORSYMBOL_TYPE_SYMBOL:
return lookUpConstructorTypeInLamContext(
env, getAstLookUpOrSymbol_Symbol(los));
case AST_LOOKUPORSYMBOL_TYPE_LOOKUP:
return NULL;
default:
cant_happen("unrecognized %s", astLookUpOrSymbolTypeName(los->type));
}
}
/**
* @brief checks to see if a type function is actually an alias for another.
*
* @param function The AST type function to check.
* @param env The lambda context to use.
* @return The resulting lambda type constructor type, either original or alias.
*/
static LamTypeConstructorType *expandFunctionAlias(AstTypeFunction *function,
LamContext *env) {
if (function->typeList != NULL) {
return NULL;
}
return expandSymbolAlias(function->symbol, env);
}
/**
* @brief Converts an AST Type Function to a lambda type function.
*
* @param astTypeFunction The AST Type Function to convert.
* @param env The lambda context to use.
* @return The resulting lambda type function.
*/
static LamTypeFunction *convertAstTypeFunction(AstTypeFunction *astTypeFunction,
LamContext *env) {
LamTypeConstructorArgs *lamTypeConstructorArgs =
convertAstTypeList(astTypeFunction->typeList, env);
int save = PROTECT(lamTypeConstructorArgs);
HashSymbol *name = getAstLookUpOrSymbol_Symbol(astTypeFunction->symbol);
LamTypeFunction *this =
newLamTypeFunction(CPI(astTypeFunction), name, lamTypeConstructorArgs);
UNPROTECT(save);
return this;
}
/**
* @brief Converts an AST Type Clause to a lambda type constructor type.
*
* @param astTypeClause The AST Type Clause to convert.
* @param env The lambda context to use.
* @return The resulting lambda type constructor type.
*/
static LamTypeConstructorType *
convertAstTypeClause(AstTypeClause *astTypeClause, LamContext *env) {
switch (astTypeClause->type) {
case AST_TYPECLAUSE_TYPE_INTEGER:
return newLamTypeConstructorType_Integer(CPI(astTypeClause));
case AST_TYPECLAUSE_TYPE_CHARACTER:
return newLamTypeConstructorType_Character(CPI(astTypeClause));
case AST_TYPECLAUSE_TYPE_VAR:
return newLamTypeConstructorType_Var(
CPI(astTypeClause), getAstTypeClause_Var(astTypeClause));
case AST_TYPECLAUSE_TYPE_TYPEFUNCTION: {
LamTypeConstructorType *alias = expandFunctionAlias(
getAstTypeClause_TypeFunction(astTypeClause), env);
if (alias != NULL) {
return alias;
}
LamTypeFunction *lamTypeFunction = convertAstTypeFunction(
getAstTypeClause_TypeFunction(astTypeClause), env);
int save = PROTECT(lamTypeFunction);
LamTypeConstructorType *this = newLamTypeConstructorType_Function(
CPI(astTypeClause), lamTypeFunction);
UNPROTECT(save);
return this;
}
case AST_TYPECLAUSE_TYPE_TYPETUPLE: {
LamTypeConstructorArgs *lamTypeConstructorArgs =
convertAstTypeList(getAstTypeClause_TypeTuple(astTypeClause), env);
int save = PROTECT(lamTypeConstructorArgs);
LamTypeConstructorType *this = newLamTypeConstructorType_Tuple(
CPI(astTypeClause), lamTypeConstructorArgs);
UNPROTECT(save);
return this;
}
default:
cant_happen(
"unrecognised astTypeClause type %d in convertAstTypeClause",
astTypeClause->type);
}
}
/**
* @brief Creates a lambda type function representing a function from one type
* to another.
*
* @param lhs The left-hand side type constructor.
* @param rhs The right-hand side type constructor.
* @return The resulting lambda type function.
*/
static LamTypeFunction *makeArrow(LamTypeConstructorType *lhs,
LamTypeConstructorType *rhs) {
LamTypeConstructorArgs *rhsArg =
newLamTypeConstructorArgs(CPI(rhs), rhs, NULL);
int save = PROTECT(rhsArg);
LamTypeConstructorArgs *argss =
newLamTypeConstructorArgs(CPI(lhs), lhs, rhsArg);
PROTECT(argss);
LamTypeFunction *res = newLamTypeFunction(CPI(lhs), arrowSymbol(), argss);
UNPROTECT(save);
return res;
}
/**
* @brief Converts an AST Type to a lambda type constructor type.
*
* This function handles both simple types and function types.
*
* @param astType The AST Type to convert.
* @param env The lambda context to use.
* @return The resulting lambda type constructor type.
*/
static LamTypeConstructorType *convertAstType(AstType *astType,
LamContext *env) {
if (astType->next) { // it's a function
LamTypeConstructorType *next = convertAstType(astType->next, env);
int save = PROTECT(next);
LamTypeConstructorType *this =
convertAstTypeClause(astType->typeClause, env);
PROTECT(this);
LamTypeFunction *arrow = makeArrow(this, next);
PROTECT(arrow);
LamTypeConstructorType *res =
newLamTypeConstructorType_Function(CPI(astType), arrow);
UNPROTECT(save);
return res;
} else {
return convertAstTypeClause(astType->typeClause, env);
}
}
/**
* @brief Converts an AST Type List to a list of lambda type constructor
* arguments.
*
* @param typeList The AST Type List to convert.
* @param env The lambda context to use.
* @return The resulting list of lambda type constructor arguments.
*/
static LamTypeConstructorArgs *convertAstTypeList(AstTypeList *typeList,
LamContext *env) {
if (typeList == NULL)
return NULL;
LamTypeConstructorArgs *next = convertAstTypeList(typeList->next, env);
int save = PROTECT(next);
LamTypeConstructorType *arg = convertAstType(typeList->type, env);
PROTECT(arg);
LamTypeConstructorArgs *this =
newLamTypeConstructorArgs(CPI(arg), arg, next);
UNPROTECT(save);
return this;
}
/**
* @brief Converts an AST Type Map to a list of lambda type constructor
* arguments.
*
* @param typeMap The AST Type Map to convert.
* @param env The lambda context to use.
* @return The resulting list of lambda type constructor arguments.
*/
static LamTypeConstructorArgs *convertAstTypeMap(AstTypeMap *typeMap,
LamContext *env) {
if (typeMap == NULL)
return NULL;
LamTypeConstructorArgs *next = convertAstTypeMap(typeMap->next, env);
int save = PROTECT(next);
LamTypeConstructorType *arg = convertAstType(typeMap->type, env);
PROTECT(arg);
LamTypeConstructorArgs *this =
newLamTypeConstructorArgs(CPI(arg), arg, next);
UNPROTECT(save);
return this;
}
/**
* @brief Converts AST Type Constructor Arguments (list or map) to a list of
* lambda type constructor arguments.
*
* @param args The AST Type Constructor Arguments to convert.
* @param env The lambda context to use.
* @return The resulting list of lambda type constructor arguments.
*/
static LamTypeConstructorArgs *
convertAstTypeConstructorArgs(AstTypeConstructorArgs *args, LamContext *env) {
if (args == NULL) {
return NULL;
}
switch (args->type) {
case AST_TYPECONSTRUCTORARGS_TYPE_LIST: {
return convertAstTypeList(getAstTypeConstructorArgs_List(args), env);
}
case AST_TYPECONSTRUCTORARGS_TYPE_MAP: {
return convertAstTypeMap(getAstTypeConstructorArgs_Map(args), env);
}
default:
cant_happen("unrecognized %s",
astTypeConstructorArgsTypeName(args->type));
}
}
/**
* @brief Converts an AST Type Map to a list of lambda type tags.
*
* This function recursively converts the AST Type Map into a linked list of
* LamTypeTags.
*
* @param map The AST Type Map to convert.
* @return The resulting list of lambda type tags.
*/
static LamTypeTags *astTypeConstructorArgMapToTags(AstTypeMap *map) {
if (map == NULL)
return NULL;
LamTypeTags *next = astTypeConstructorArgMapToTags(map->next);
int save = PROTECT(next);
LamTypeTags *this = newLamTypeTags(CPI(map), map->key, next);
UNPROTECT(save);
return this;
}
/**
* @brief converts the AST Type Constructor Arguments to a list of lambda type
* tags, IFF the arguments are a map.
*
* @param args The AST Type Constructor Arguments to convert.
* @return The resulting list of lambda type tags, or NULL if the arguments are
* not a map.
*/
static LamTypeTags *makeLamTypeTags(AstTypeConstructorArgs *args) {
if (args == NULL) {
return NULL;
}
switch (args->type) {
case AST_TYPECONSTRUCTORARGS_TYPE_LIST:
return NULL;
case AST_TYPECONSTRUCTORARGS_TYPE_MAP:
return astTypeConstructorArgMapToTags(
getAstTypeConstructorArgs_Map(args));
default:
cant_happen("unrecognized %s",
astTypeConstructorArgsTypeName(args->type));
}
}
/**
* @brief Collects as much type information as possible about the type
* constructor and stores it in the context.
*/
static void collectTypeInfo(HashSymbol *symbol, AstTypeConstructorArgs *args,
LamTypeConstructor *type, bool needsVec,
int enumCount, int index, int arity,
LamContext *env) {
ENTER(collectTypeInfo);
LamTypeTags *tags = makeLamTypeTags(args);
int save = PROTECT(tags);
LamTypeConstructorInfo *info = newLamTypeConstructorInfo(
CPI(type), symbol, type, tags, needsVec, arity, enumCount, index);
PROTECT(info);
addConstructorInfoToLamContext(env, symbol, info);
UNPROTECT(save);
LEAVE(collectTypeInfo);
}
/**
* @brief counts the number of items in the AST Type Constructor Arguments (list
* or map).
*
* @param args The AST Type Constructor Arguments to count.
* @return The number of items in the arguments.
*/
static Index countAstTypeConstructorArgs(AstTypeConstructorArgs *args) {
if (args == NULL)
return 0;
switch (args->type) {
case AST_TYPECONSTRUCTORARGS_TYPE_LIST: {
return countAstTypeList(getAstTypeConstructorArgs_List(args));
}
case AST_TYPECONSTRUCTORARGS_TYPE_MAP: {
return countAstTypeMap(getAstTypeConstructorArgs_Map(args));
}
default:
cant_happen("unrecognized %s",
astTypeConstructorArgsTypeName(args->type));
}
}
/**
* @brief Converts an AST Type Constructor to a lambda type constructor and
* collects its type information.
*
* @param typeConstructor The AST Type Constructor to convert.
* @param type The type signature of the constructor type.
* @param enumCount The number of constructors of this type.
* @param index The index of the constructor (serves as an identifier).
* @param needsVec Whether the constructor needs to create a vector.
* @return The resulting lambda type constructor.
*/
static LamTypeConstructor *
convertTypeConstructor(AstTypeConstructor *typeConstructor, LamTypeSig *type,
int enumCount, int index, bool needsVec,
LamContext *env) {
int nArgs = countAstTypeConstructorArgs(typeConstructor->args);
LamTypeConstructorArgs *args =
convertAstTypeConstructorArgs(typeConstructor->args, env);
int save = PROTECT(args);
LamTypeConstructor *lamTypeConstructor =
newLamTypeConstructor(CPI(type), typeConstructor->symbol, type, args);
PROTECT(lamTypeConstructor);
collectTypeInfo(typeConstructor->symbol, typeConstructor->args,
lamTypeConstructor, needsVec, enumCount, index, nArgs, env);
UNPROTECT(save);
return lamTypeConstructor;
}
/**
* @brief Converts an AST Type Definition to a lambda type definition.
*
* @param typeDef The AST Type Definition to convert.
* @param env The lambda context.
* @return The resulting lambda type definition.
*/
static LamTypeDef *convertTypeDef(AstTypeDef *typeDef, LamContext *env) {
LamTypeSig *type = convertTypeSig(typeDef->typeSig);
int save = PROTECT(type);
AstTypeBody *typeBody = typeDef->typeBody;
bool needsVec = typeHasFields(typeBody);
int enumCount = countAstTypeBody(typeBody);
int index = 0;
LamTypeConstructorList *lamTypeConstructorList = NULL;
int save2 = PROTECT(type);
while (typeBody != NULL) {
LamTypeConstructor *lamTypeConstructor = convertTypeConstructor(
typeBody->typeConstructor, type, enumCount, index, needsVec, env);
int save3 = PROTECT(lamTypeConstructor);
lamTypeConstructorList = newLamTypeConstructorList(
CPI(lamTypeConstructor), lamTypeConstructor,
lamTypeConstructorList);
REPLACE_PROTECT(save2, lamTypeConstructorList);
UNPROTECT(save3);
typeBody = typeBody->next;
index++;
}
LamTypeDef *res = newLamTypeDef(CPI(type), type, lamTypeConstructorList);
UNPROTECT(save);
return res;
}
/**
* @brief Collects an alias definition and adds it to the lambda context.
*
* @param alias The AST Alias to collect.
* @param env The lambda context to use.
* @return void
*/
static void collectAlias(AstAlias *alias, LamContext *env) {
LamTypeConstructorType *type = convertAstType(alias->type, env);
int save = PROTECT(type);
setLamAliasTable(env->aliases, alias->name, type);
UNPROTECT(save);
}
/**
* @brief recurses over a list of definitions, collecting any aliases in the
* context.
*
* @param definitions The list of AST definitions to process.
* @param env The lambda context to populate.
* @return void
*/
static void collectAliases(AstDefinitions *definitions, LamContext *env) {
if (definitions == NULL) {
return;
}
switch (definitions->definition->type) {
case AST_DEFINITION_TYPE_DEFINE:
case AST_DEFINITION_TYPE_BLANK:
case AST_DEFINITION_TYPE_TYPEDEF:
case AST_DEFINITION_TYPE_LAZY:
case AST_DEFINITION_TYPE_MULTI:
break;
case AST_DEFINITION_TYPE_ALIAS:
collectAlias(getAstDefinition_Alias(definitions->definition), env);
break;
case AST_DEFINITION_TYPE_SYNTAXDECL:
case AST_DEFINITION_TYPE_SYNTAXUSE:
can_happen(CPI(definitions->definition),
"syntax carrier %s reached lambda conversion before syntax "
"lowering",
astDefinitionTypeName(definitions->definition->type));
break;
default:
cant_happen("unrecognised %s",
astDefinitionTypeName(definitions->definition->type));
}
collectAliases(definitions->next, env);
}
/**
* @brief Checks against duplicate macro arguments in a list.
*
* @param arg The macro argument to check.
* @param args The list of existing macro arguments.
* @return void
*/
static void checkDuplicateLazyArg(HashSymbol *arg, SymbolList *args) {
if (args == NULL)
return;
if (arg == args->symbol) {
can_happen(CPI(args), "duplicate argument \"%s\" in macro definition",
arg->name);
return;
}
checkDuplicateLazyArg(arg, args->next);
}
/**
* @brief Collects macro arguments from an AST argument list.
*
* This function recursively collects macro arguments and both
* checks for duplicates and asserts that the arguments are symbols.
*
* @param argList The AST argument list to collect from.
* @return A linked list of lambda variable arguments.
*/
static SymbolList *collectLazyArgs(AstFargList *argList) {
if (argList == NULL)
return NULL;
SymbolList *next = collectLazyArgs(argList->next);
int save = PROTECT(next);
HashSymbol *arg = getAstFarg_Symbol(argList->arg);
checkDuplicateLazyArg(arg, next);
SymbolList *this = newSymbolList(CPI(argList), arg, next);
UNPROTECT(save);
return this;
}
/**
* @brief Populates a lambda macro arguments table from a list of arguments.
*
* This function iterates over the list of lambda variable arguments and adds
* each one to the macro arguments table with a NULL value.
*
* @param symbols The macro arguments table to populate.
* @param args The list of lambda variable arguments.
* @return void
*/
static void populateArgsTable(SymbolSet *symbols, SymbolList *args) {
if (args == NULL)
return;
setSymbolSet(symbols, args->symbol);
populateArgsTable(symbols, args->next);
}
/**
* @brief Converts an AST Lazy Definition to a lambda expression.
*
* This function converts the lazy definition into a lambda expression.
* Lazys evaluate their arguments on-demand, so the generated lazy lambda
* must wrap each of its arguments in a promise.
*
* @param astLazy The AST Lazy Definition to convert.
* @param env The lambda context to use.
* @return The resulting lambda expression for the macro.
*/
static LamExp *convertAstLazy(AstDefLazy *astLazy, LamContext *env) {
ENTER(convertAstLazy);
// get the list of argument symbols
SymbolList *args = collectLazyArgs(astLazy->definition->altArgs->argList);
int save = PROTECT(args);
// do a standard conversion of the macro body
LamExp *body = convertNest(astLazy->definition->nest, env);
PROTECT(body);
// create a random-access set of the macro argument symbols
SymbolSet *symbolTable = newSymbolSet();
PROTECT(symbolTable);
populateArgsTable(symbolTable, args);
// force all the argument thunks in the body of the macro
body = lamPerformLazySubstitutions(body, symbolTable);
PROTECT(body);
// prepare the resulting lambda expression
LamExp *res = makeLamExp_Lam(CPI(astLazy), args, body);
PROTECT(res);
getLamExp_Lam(res)->isLazy = true;
// remember it's a macro
setSymbolSet(env->macros, astLazy->name);
LEAVE(convertAstLazy);
UNPROTECT(save);
return res;
}
/**
* @brief Collects type definitions from a list of AST definitions.
*
* This function recursively collects type definitions from the AST
* and converts them into lambda type definitions.
*
* @param definitions The AST definitions to collect from.
* @param env The lambda context to use.
* @return A linked list of lambda type definitions.
*/
static LamTypeDefList *collectTypeDefs(AstDefinitions *definitions,
LamContext *env) {
if (definitions == NULL) {
return NULL;
}
switch (definitions->definition->type) {
case AST_DEFINITION_TYPE_DEFINE:
case AST_DEFINITION_TYPE_ALIAS:
case AST_DEFINITION_TYPE_BLANK:
case AST_DEFINITION_TYPE_LAZY:
case AST_DEFINITION_TYPE_MULTI:
return collectTypeDefs(definitions->next, env);
case AST_DEFINITION_TYPE_TYPEDEF: {
LamTypeDef *lamTypeDef = convertTypeDef(
getAstDefinition_TypeDef(definitions->definition), env);
int save = PROTECT(lamTypeDef);
LamTypeDefList *rest = collectTypeDefs(definitions->next, env);
PROTECT(rest);
LamTypeDefList *res =
newLamTypeDefList(CPI(lamTypeDef), lamTypeDef, rest);
UNPROTECT(save);
return res;
}
case AST_DEFINITION_TYPE_SYNTAXDECL:
case AST_DEFINITION_TYPE_SYNTAXUSE:
can_happen(CPI(definitions->definition),
"syntax carrier %s reached lambda conversion before syntax "
"lowering",
astDefinitionTypeName(definitions->definition->type));
return collectTypeDefs(definitions->next, env);
default:
cant_happen("unrecognised %s",
astDefinitionTypeName(definitions->definition->type));
}
}
/**
* @brief Convert and prepend a lazy definition to the list of letRec bindings.
* @param lazy The AST lazy definition to convert.
* @param env The lambda context to use.
* @param next The letRec list to prepend to.
* @return The new letRec bindings with the lazy definition prepended.
*/
static LamBindings *prependLazy(AstDefLazy *lazy, LamContext *env,
LamBindings *next) {
ENTER(prependLazy);
LamExp *exp = convertAstLazy(lazy, env);
int save = PROTECT(exp);
LamBindings *this = newLamBindings(CPI(lazy), lazy->name, exp, next);
UNPROTECT(save);
LEAVE(prependLazy);
return this;
}
LamExp *makeUnpackTuple(ParserInfo PI, LamExp *temp, int index, int size) {
return makeLamExp_TupleIndex(PI, index, size, temp);
}
static LamBindings *prependMultiSymbols(AstSymbolList *symbols, int index,
int size, LamExp *temp,
LamBindings *next) {
if (symbols == NULL) {
return next;
}