Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide vec #47

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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))
Expand Down Expand Up @@ -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)

Expand All @@ -63,28 +64,28 @@ $(TARGET): $(MAIN_OBJ) $(ALL_OBJ)

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)
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 $@

Expand Down Expand Up @@ -133,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
34 changes: 33 additions & 1 deletion src/anf_normalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/lambda.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ structs:
size: int # number of alternatives
index: int # index into list of alternatives


enums:
LamPrimOp:
- ADD
Expand All @@ -206,9 +205,9 @@ enums:
- LT
- GE
- LE
- VEC
- XOR
- CMP
- VEC

LamUnaryOp:
- NEG
Expand All @@ -229,6 +228,7 @@ unions:
makeVec: LamMakeVec
construct: LamConstruct
deconstruct: LamDeconstruct
tag: LamExp
constant: LamConstant
apply: LamApply
iff: LamIff
Expand Down
3 changes: 3 additions & 0 deletions src/lambda_conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 7 additions & 11 deletions src/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -378,16 +378,12 @@ static LamMatch *makePlainMatch(LamTypeConstructorList *constructors, LamContext
return res;
}

static LamMatch *makeVecMatch(LamTypeConstructorList *constructors, LamContext *env) {
LamMatchList *cases = makeVecMatchList(constructors, env);
static LamMatch *makeTagMatch(LamTypeConstructorList *constructors, LamContext *env) {
LamMatchList *cases = makeTagMatchList(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);
Expand All @@ -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);
}
Expand Down
9 changes: 8 additions & 1 deletion src/tc_analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions src/tpmc_translate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading