From 04b9e1092ed91242ee3321d05b496c06672fb9a8 Mon Sep 17 00:00:00 2001 From: Bill Hails Date: Wed, 24 Jan 2024 15:03:56 +0000 Subject: [PATCH 1/2] phase 1 complete --- Makefile | 15 ++++++++------- src/anf_normalize.c | 34 +++++++++++++++++++++++++++++++++- src/lambda.yaml | 4 ++-- src/lambda_conversion.c | 3 +++ src/print.c | 10 +++------- src/tc_analyze.c | 9 ++++++++- src/tpmc_translate.c | 7 +------ 7 files changed, 58 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 584ee37..bd8f9c9 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all clean deps profile check-grammar list-cores test clean-test +.PHONY: all clean deps profile check-grammar list-cores test TARGET=cekf @@ -13,9 +13,9 @@ DEBUGGING=-g # CCMODE = $(OPTIMIZING) CCMODE = $(DEBUGGING) - CC=cc -Wall -Wextra -Werror $(CCMODE) LAXCC=cc -Werror $(CCMODE) +PYTHON=python3 EXTRA_YAML=$(wildcard src/*.yaml) EXTRA_C_TARGETS=$(patsubst src/%.yaml,tmp/%.c,$(EXTRA_YAML)) @@ -65,19 +65,19 @@ include $(ALL_DEP) $(EXTRA_C_TARGETS): tmp/%.c: src/%.yaml tools/makeAST.py | tmp - python3 tools/makeAST.py $< c > $@ || (rm -f $@ ; exit 1) + $(PYTHON) tools/makeAST.py $< c > $@ || (rm -f $@ ; exit 1) $(EXTRA_H_TARGETS): tmp/%.h: src/%.yaml tools/makeAST.py | tmp - python3 tools/makeAST.py $< h > $@ || (rm -f $@ ; exit 1) + $(PYTHON) tools/makeAST.py $< h > $@ || (rm -f $@ ; exit 1) $(EXTRA_OBJTYPES_H_TARGETS): tmp/%_objtypes.h: src/%.yaml tools/makeAST.py | tmp - python3 tools/makeAST.py $< objtypes_h > $@ || (rm -f $@ ; exit 1) + $(PYTHON) tools/makeAST.py $< objtypes_h > $@ || (rm -f $@ ; exit 1) $(EXTRA_DEBUG_H_TARGETS): tmp/%_debug.h: src/%.yaml tools/makeAST.py | tmp - python3 tools/makeAST.py $< debug_h > $@ || (rm -f $@ ; exit 1) + $(PYTHON) tools/makeAST.py $< debug_h > $@ || (rm -f $@ ; exit 1) $(EXTRA_DEBUG_C_TARGETS): tmp/%_debug.c: src/%.yaml tools/makeAST.py | tmp - python3 tools/makeAST.py $< debug_c > $@ || (rm -f $@ ; exit 1) + $(PYTHON) tools/makeAST.py $< debug_c > $@ || (rm -f $@ ; exit 1) .generated: $(EXTRA_TARGETS) $(TMP_H) @@ -85,6 +85,7 @@ $(EXTRA_DEBUG_C_TARGETS): tmp/%_debug.c: src/%.yaml tools/makeAST.py | tmp tags: src/* ctags src/* + $(MAIN_OBJ) $(OBJ): obj/%.o: src/%.c | obj $(CC) -I tmp/ -I src/ -c $< -o $@ diff --git a/src/anf_normalize.c b/src/anf_normalize.c index 3a10e31..13effe0 100644 --- a/src/anf_normalize.c +++ b/src/anf_normalize.c @@ -83,6 +83,7 @@ static CexpCondCases *normalizeCondCases(LamCondCases *cases); static CexpLetRec *replaceCexpLetRec(CexpLetRec *cexpLetRec, LamLetRecBindings *lamLetRecBindings); static Exp *normalizeConstruct(LamConstruct *construct, Exp *tail); static Exp *normalizeDeconstruct(LamDeconstruct *deconstruct, Exp *tail); +static Exp *normalizeTag(LamExp *tag, Exp *tail); Exp *anfNormalize(LamExp *lamExp) { return normalize(lamExp, NULL); @@ -159,6 +160,8 @@ static Exp *normalize(LamExp *lamExp, Exp *tail) { return normalizeDeconstruct(lamExp->val.deconstruct, tail); case LAMEXP_TYPE_CONSTRUCT: return normalizeConstruct(lamExp->val.construct, tail); + case LAMEXP_TYPE_TAG: + return normalizeTag(lamExp->val.tag, tail); case LAMEXP_TYPE_CONSTANT: return normalizeStdInteger(lamExp->val.constant->tag, tail); case LAMEXP_TYPE_LET: @@ -275,6 +278,14 @@ static LamPrimApp *deconstructToPrimApp(LamDeconstruct *deconstruct) { return res; } +static LamPrimApp *tagToPrimApp(LamExp *tagged) { + LamExp *index = newLamExp(LAMEXP_TYPE_STDINT, LAMEXP_VAL_STDINT(0)); + int save = PROTECT(index); + LamPrimApp *res = newLamPrimApp(LAMPRIMOP_TYPE_VEC, index, tagged); + UNPROTECT(save); + return res; +} + static Exp *normalizeDeconstruct(LamDeconstruct *deconstruct, Exp *tail) { ENTER(noramaalizeDeconstruct); LamPrimApp *primApp = deconstructToPrimApp(deconstruct); @@ -285,6 +296,16 @@ static Exp *normalizeDeconstruct(LamDeconstruct *deconstruct, Exp *tail) { return res; } +static Exp *normalizeTag(LamExp *tagged, Exp *tail) { + ENTER(noramaalizeTag); + LamPrimApp *primApp = tagToPrimApp(tagged); + int save = PROTECT(primApp); + Exp *res = normalizePrim(primApp, tail); + UNPROTECT(save); + LEAVE(noramaalizeTag); + return res; +} + static Exp *normalizeLetRec(LamLetRec *lamLetRec, Exp *tail) { ENTER(normalizeLetRec); IFDEBUG(ppLamLetRec(lamLetRec)); @@ -826,7 +847,15 @@ static CexpCondCases *normalizeCondCases(LamCondCases *cases) { static Aexp *replaceLamDeconstruct(LamDeconstruct *lamDeconstruct, HashTable *replacements) { LamPrimApp *primApp = deconstructToPrimApp(lamDeconstruct); int save = PROTECT(primApp); - Aexp *res = replaceLamPrim(primApp, replacements); // prim needs lamExp + Aexp *res = replaceLamPrim(primApp, replacements); + UNPROTECT(save); + return res; +} + +static Aexp *replaceLamTag(LamExp *tagged, HashTable *replacements) { + LamPrimApp *primApp = tagToPrimApp(tagged); + int save = PROTECT(primApp); + Aexp *res = replaceLamPrim(primApp, replacements); UNPROTECT(save); return res; } @@ -862,6 +891,9 @@ static Aexp *replaceLamExp(LamExp *lamExp, HashTable *replacements) { case LAMEXP_TYPE_CONSTRUCT: res = replaceLamConstruct(lamExp->val.construct, replacements); break; + case LAMEXP_TYPE_TAG: + res = replaceLamTag(lamExp->val.tag, replacements); + break; case LAMEXP_TYPE_CONSTANT: res = aexpNormalizeStdInteger(lamExp->val.constant->tag); break; diff --git a/src/lambda.yaml b/src/lambda.yaml index 0e25ad1..33b3edd 100644 --- a/src/lambda.yaml +++ b/src/lambda.yaml @@ -191,7 +191,6 @@ structs: size: int # number of alternatives index: int # index into list of alternatives - enums: LamPrimOp: - ADD @@ -206,9 +205,9 @@ enums: - LT - GE - LE - - VEC - XOR - CMP + - VEC LamUnaryOp: - NEG @@ -229,6 +228,7 @@ unions: makeVec: LamMakeVec construct: LamConstruct deconstruct: LamDeconstruct + tag: LamExp constant: LamConstant apply: LamApply iff: LamIff diff --git a/src/lambda_conversion.c b/src/lambda_conversion.c index e6183b8..9f86365 100644 --- a/src/lambda_conversion.c +++ b/src/lambda_conversion.c @@ -424,6 +424,9 @@ LamExp *lamPerformSubstitutions(LamExp *exp, HashTable *substitutions) { case LAMEXP_TYPE_CONSTRUCT: exp->val.construct = performConstructSubstitutions(exp->val.construct, substitutions); break; + case LAMEXP_TYPE_TAG: + exp->val.tag = lamPerformSubstitutions(exp->val.tag, substitutions); + break; case LAMEXP_TYPE_APPLY: exp->val.apply = performApplySubstitutions(exp->val.apply, substitutions); break; diff --git a/src/print.c b/src/print.c index 1aeeab2..0b0884c 100644 --- a/src/print.c +++ b/src/print.c @@ -378,16 +378,12 @@ static LamMatch *makePlainMatch(LamTypeConstructorList *constructors, LamContext return res; } -static LamMatch *makeVecMatch(LamTypeConstructorList *constructors, LamContext *env) { +static LamMatch *makeTagMatch(LamTypeConstructorList *constructors, LamContext *env) { LamMatchList *cases = makeVecMatchList(constructors, env); int save = PROTECT(cases); - LamExp *zero = newLamExp(LAMEXP_TYPE_STDINT, LAMEXP_VAL_STDINT(0)); - PROTECT(zero); LamExp *var = printArgVar(); PROTECT(var); - LamPrimApp *vec = newLamPrimApp(LAMPRIMOP_TYPE_VEC, zero, var); - PROTECT(vec); - LamExp *prim = newLamExp(LAMEXP_TYPE_PRIM, LAMEXP_VAL_PRIM(vec)); + LamExp *prim = newLamExp(LAMEXP_TYPE_TAG, LAMEXP_VAL_TAG(var)); PROTECT(prim); LamMatch *res = newLamMatch(prim, cases); UNPROTECT(save); @@ -402,7 +398,7 @@ static LamExp *makeFunctionBody(LamTypeConstructorList *constructors, LamContext } LamMatch *match = NULL; if (info->vec) { - match = makeVecMatch(constructors, env); + match = makeTagMatch(constructors, env); } else { match = makePlainMatch(constructors, env); } diff --git a/src/tc_analyze.c b/src/tc_analyze.c index 51dbcad..e920f9f 100644 --- a/src/tc_analyze.c +++ b/src/tc_analyze.c @@ -68,6 +68,7 @@ static TcType *analyzeUnary(LamUnaryApp *app, TcEnv *env, TcNg *ng); static TcType *analyzeSequence(LamSequence *sequence, TcEnv *env, TcNg *ng); static TcType *analyzeConstruct(LamConstruct *construct, TcEnv *env, TcNg *ng); static TcType *analyzeDeconstruct(LamDeconstruct *deconstruct, TcEnv *env, TcNg *ng); +static TcType *analyzeTag(LamExp *tag, TcEnv *env, TcNg *ng); static TcType *analyzeConstant(LamConstant *constant, TcEnv *env, TcNg *ng); static TcType *analyzeApply(LamApply *apply, TcEnv *env, TcNg *ng); static TcType *analyzeIff(LamIff *iff, TcEnv *env, TcNg *ng); @@ -162,6 +163,8 @@ static TcType *analyzeExp(LamExp *exp, TcEnv *env, TcNg *ng) { return analyzeConstruct(exp->val.construct, env, ng); case LAMEXP_TYPE_DECONSTRUCT: return analyzeDeconstruct(exp->val.deconstruct, env, ng); + case LAMEXP_TYPE_TAG: + return analyzeTag(exp->val.tag, env, ng); case LAMEXP_TYPE_CONSTANT: return analyzeConstant(exp->val.constant, env, ng); case LAMEXP_TYPE_APPLY: @@ -365,7 +368,7 @@ static TcType *analyzePrim(LamPrimApp *app, TcEnv *env, TcNg *ng) { res = analyzeStarship(app->exp1, app->exp2, env, ng); break; case LAMPRIMOP_TYPE_VEC: - res = analyzeExp(app->exp2, env, ng); + cant_happen("encountered VEC in analyzePrim"); break; case LAMPRIMOP_TYPE_XOR: res = analyzeBinaryBool(app->exp1, app->exp2, env, ng); @@ -498,6 +501,10 @@ static TcType *analyzeDeconstruct(LamDeconstruct *deconstruct, TcEnv *env, TcNg return fieldType; } +static TcType *analyzeTag(LamExp *tagged, TcEnv *env, TcNg *ng) { + return analyzeExp(tagged, env, ng); +} + static TcType *analyzeConstant(LamConstant *constant, TcEnv *env, TcNg *ng) { ENTER(analyzeConstant); TcType *constType = lookup(env, constant->name, ng); diff --git a/src/tpmc_translate.c b/src/tpmc_translate.c index e6e85aa..d7a34ff 100644 --- a/src/tpmc_translate.c +++ b/src/tpmc_translate.c @@ -375,12 +375,7 @@ static LamExp *translateArcList(TpmcArcList *arcList, LamExp *testVar, HashTable PROTECT(matches); LamExp *testExp = NULL; if (info->vec) { - // zero is the index of the enum tag in the vector - LamExp *zero = newLamExp(LAMEXP_TYPE_STDINT, LAMEXP_VAL_STDINT(0)); - PROTECT(zero); - LamPrimApp *vec = newLamPrimApp(LAMPRIMOP_TYPE_VEC, zero, testVar); - PROTECT(vec); - testExp = newLamExp(LAMEXP_TYPE_PRIM, LAMEXP_VAL_PRIM(vec)); + testExp = newLamExp(LAMEXP_TYPE_TAG, LAMEXP_VAL_TAG(testVar)); PROTECT(testExp); } else { testExp = testVar; From 639ea92e0d03ca572aab733c4a7dbbb2e8470bfa Mon Sep 17 00:00:00 2001 From: Bill Hails Date: Wed, 24 Jan 2024 16:03:13 +0000 Subject: [PATCH 2/2] mor consistent naming --- Makefile | 9 +++++---- src/print.c | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index bd8f9c9..25dcabc 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,7 @@ ALL_OBJ=$(OBJ) $(EXTRA_OBJ) ALL_DEP=$(DEP) $(EXTRA_DEP) $(TEST_DEP) TMP_H=tmp/parser.h tmp/lexer.h +TMP_C=tmp/parser.c tmp/lexer.c all: $(TARGET) @@ -63,7 +64,6 @@ $(TARGET): $(MAIN_OBJ) $(ALL_OBJ) include $(ALL_DEP) - $(EXTRA_C_TARGETS): tmp/%.c: src/%.yaml tools/makeAST.py | tmp $(PYTHON) tools/makeAST.py $< c > $@ || (rm -f $@ ; exit 1) @@ -83,8 +83,8 @@ $(EXTRA_DEBUG_C_TARGETS): tmp/%_debug.c: src/%.yaml tools/makeAST.py | tmp .generated: $(EXTRA_TARGETS) $(TMP_H) touch $@ -tags: src/* - ctags src/* +tags: src/* $(EXTRA_TARGETS) $(TMP_H) $(TMP_C) + ctags src/* $(EXTRA_TARGETS) $(TMP_H) $(TMP_C) $(MAIN_OBJ) $(OBJ): obj/%.o: src/%.c | obj $(CC) -I tmp/ -I src/ -c $< -o $@ @@ -134,4 +134,5 @@ check-grammar: list-cores: ls -rt1 /var/lib/apport/coredump/* | tail -1 -# vim: noet,sw=8,tabstop=8 + +# vim: noet sw=8 diff --git a/src/print.c b/src/print.c index 0b0884c..b698eef 100644 --- a/src/print.c +++ b/src/print.c @@ -345,13 +345,13 @@ static LamMatchList *makePlainMatchList(LamTypeConstructorList *constructors, La return res; } -static LamMatchList *makeVecMatchList(LamTypeConstructorList *constructors, LamContext *env) { +static LamMatchList *makeTagMatchList(LamTypeConstructorList *constructors, LamContext *env) { if (constructors == NULL) return NULL; - LamMatchList *next = makeVecMatchList(constructors->next, env); + LamMatchList *next = makeTagMatchList(constructors->next, env); int save = PROTECT(next); LamTypeConstructorInfo *info = lookupInLamContext(env, constructors->constructor->name); if (info == NULL) { - cant_happen("cannot find info for type constructor %s in makeVecMatchList", + cant_happen("cannot find info for type constructor %s in makeTagMatchList", constructors->constructor->name->name); } LamIntList *matches = newLamIntList(info->index, info->type->name, NULL); @@ -379,7 +379,7 @@ static LamMatch *makePlainMatch(LamTypeConstructorList *constructors, LamContext } static LamMatch *makeTagMatch(LamTypeConstructorList *constructors, LamContext *env) { - LamMatchList *cases = makeVecMatchList(constructors, env); + LamMatchList *cases = makeTagMatchList(constructors, env); int save = PROTECT(cases); LamExp *var = printArgVar(); PROTECT(var);