Commit f9d21335 authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

fallback is possible through IFs as well as SELECTs

parent c51700b1
......@@ -319,7 +319,7 @@ VarEntry* parseVar(char **ptr, VarEntry* target) {
}
Code* parseTerm(char **ptr);
Code* parseSelectBody(char **ptr, Code* fallback, int fallback_nrargs);
Code* parseFallbackBody(char **ptr, Code* fallback, int fallback_nrargs);
/*
* It is very messy, because it handles 3 cases:
......@@ -530,7 +530,7 @@ SelectEntry* parseSelect(char **ptr, Code* fallback, int fallback_nrargs) {
isDefault = true;
(*ptr)++;
defaultBody = (Code*) parseSelectBody(ptr, fallback, fallback_nrargs);
defaultBody = (Code*) parseFallbackBody(ptr, fallback, fallback_nrargs);
type = **ptr;
}
......@@ -553,7 +553,7 @@ SelectEntry* parseSelect(char **ptr, Code* fallback, int fallback_nrargs) {
nrConses = ((ADTEntry*) firstCase)->nrConses;
}
Code* firstBody = (Code*) parseSelectBody(ptr, child_fallback,
Code* firstBody = (Code*) parseFallbackBody(ptr, child_fallback,
child_fallback_base_nrargs + firstCase->arity);
entry = (SelectEntry*) alloc_code(sizeof (SelectEntry) + sizeof (Code*) * nrConses);
......@@ -578,7 +578,7 @@ SelectEntry* parseSelect(char **ptr, Code* fallback, int fallback_nrargs) {
for (int i = 0; i < nrCases; i++) {
(*ptr)++; // skip type
ADTEntry* nextCase = (ADTEntry*) parseFunName(ptr);
entry->bodies[nextCase->idx] = (Code*) parseSelectBody(ptr, child_fallback,
entry->bodies[nextCase->idx] = (Code*) parseFallbackBody(ptr, child_fallback,
child_fallback_base_nrargs + nextCase->base.arity);
entry->default_map &= ~(1<<nextCase->idx);
}
......@@ -613,7 +613,7 @@ SelectEntry* parseSelect(char **ptr, Code* fallback, int fallback_nrargs) {
entry->base.type = CT_SELECT_STR;
}
entry->cases[i].body = (Code*) parseSelectBody(ptr, child_fallback, child_fallback_base_nrargs);
entry->cases[i].body = (Code*) parseFallbackBody(ptr, child_fallback, child_fallback_base_nrargs);
}
}
......@@ -625,7 +625,18 @@ SelectEntry* parseSelect(char **ptr, Code* fallback, int fallback_nrargs) {
return entry;
}
Code* parseSelectBody(char **ptr, Code* fallback, int fallback_nrargs) {
IfEntry* parseIf(char **ptr, Code* fallback, int fallback_nrargs) {
struct IfEntry* entry = (IfEntry*) alloc_code(sizeof (IfEntry));
entry->base.type = CT_IF;
entry->cond = parseTerm(ptr);
entry->texpr = parseFallbackBody(ptr, fallback, fallback_nrargs);
entry->fexpr = parseFallbackBody(ptr, fallback, fallback_nrargs);
set_create_thunk_fun((Code*) entry);
return entry;
}
Code* parseFallbackBody(char **ptr, Code* fallback, int fallback_nrargs) {
char type = **ptr;
......@@ -634,23 +645,17 @@ Code* parseSelectBody(char **ptr, Code* fallback, int fallback_nrargs) {
(*ptr)++;
return (Code*) parseSelect(ptr, fallback, fallback_nrargs);
}
else if(type == 'I')
{
(*ptr)++;
return (Code*) parseIf(ptr, fallback, fallback_nrargs);
}
else
{
return parseTerm(ptr);
}
}
IfEntry* parseIf(char **ptr) {
struct IfEntry* entry = (IfEntry*) alloc_code(sizeof (IfEntry));
entry->base.type = CT_IF;
entry->cond = parseTerm(ptr);
entry->texpr = parseTerm(ptr);
entry->fexpr = parseTerm(ptr);
set_create_thunk_fun((Code*) entry);
return entry;
}
LetEntry* parseLet(char **ptr) {
Code* body = parseTerm(ptr);
......@@ -710,7 +715,7 @@ Code* parseTerm(char **ptr) {
case 'S': // Select
return (Code*) parseSelect(ptr, NULL, 0);
case 'I': // If
return (Code*) parseIf(ptr);
return (Code*) parseIf(ptr, NULL, 0);
case 'E': // Let
return (Code*) parseLet(ptr);
default:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment