Skip to content

Commit

Permalink
move Map to prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
StavromulaBeta committed Aug 7, 2024
1 parent 008ccae commit 1320b06
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 68 deletions.
1 change: 0 additions & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
{.name="error", .calltype=call, .argc=1, .args={string}},
{.name="list", .calltype=call, .argc=1, .args={block}, .returns=true, .rettype=list},
{.name="number", .calltype=call, .argc=1, .args={string}, .returns=true, .rettype=number},
{.name="map", .calltype=call, .argc=2, .args={block, list}, .returns=true, .rettype=list, .stack=true},
{.name="range", .calltype=call, .argc=2, .args={number, number}, .returns=true, .rettype=list},
{.name="index", .calltype=call, .argc=2, .args={number, list}, .returns=true, .rettype=any},
{.name="precompute", .calltype=call, .argc=1, .args={block}, .returns=true, .rettype=block},
Expand Down
3 changes: 2 additions & 1 deletion src/cognac.c
Original file line number Diff line number Diff line change
Expand Up @@ -2416,7 +2416,6 @@ void _inline_functions(func_t* f, func_list_t** funcs)

void inline_functions(module_t* m)
{
compute_sources(m);
_inline_functions(m->entry, &m->funcs);
}

Expand Down Expand Up @@ -2820,6 +2819,7 @@ void static_branches(module_t* m)
reg_t* i1 = pop_register_front(regs);
reg_t* i2 = pop_register_front(regs);
push_register_front(make_register(any, a), regs);
//printf("%p %p\n", i1, i2);
if (!i1 || !i2) break;
ast_list_t* s1 = i1->source;
ast_list_t* s2 = i2->source;
Expand Down Expand Up @@ -3158,6 +3158,7 @@ int main(int argc, char** argv)
merge_symbols,
assign_sequence_numbers,
inline_functions,
compute_sources,
static_branches,
static_calls,
determine_arguments,
Expand Down
11 changes: 11 additions & 0 deletions src/prelude.cog
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,15 @@ Def Sort
Append
);

Def Map (
Def Pred;
Let L be a List!;

Do If Empty? L ( L )
else (
Let X be First of L;
Let Res be Pred X;

return Push Res to Map (Pred) over Rest of L;
);
);
66 changes: 0 additions & 66 deletions src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ static NUMBER ___round(NUMBER);
static NUMBER ___ceiling(NUMBER);
static void ___assert(STRING, BOOLEAN);
static void ___error(STRING);
static LIST ___map(BLOCK, LIST);
static LIST ___filter(BLOCK, LIST);
static LIST ___range(NUMBER, NUMBER);
static ANY ___index(NUMBER, LIST);
Expand Down Expand Up @@ -1487,71 +1486,6 @@ static void ___error(STRING str)
throw_error(str);
}

/*
static LIST ___map(BLOCK blk, LIST lst)
{
flush_stack_cache();
ANYPTR tmp_stack_start = stack.start;
stack.start = stack.top;
cognate_list start = {0};
cognate_list* ptr = &start;
for (; lst ; lst = lst->next)
{
push(lst->object);
call_block(blk);
flush_stack_cache();
while (stack.top != stack.start)
{
cognate_list* new = gc_malloc(sizeof *new);
new->object = pop();
new->next = NULL;
ptr->next = new;
ptr = new;
}
}
stack.top = stack.start;
stack.start = tmp_stack_start;
return start.next;
}
*/

static LIST ___map(BLOCK blk, LIST lst)
{
cognate_list start = {0};
cognate_list* ptr = &start;
for (; lst ; lst = lst->next)
{
push(lst->object);
call_block(blk);
cognate_list* new = gc_malloc(sizeof *new);
new->object = pop();
new->next = NULL;
ptr->next = new;
ptr = new;
}
return start.next;
}

static LIST ___filter(BLOCK blk, LIST lst)
{
cognate_list start = {0};
cognate_list* ptr = &start;
for (; lst ; lst = lst->next)
{
push(lst->object);
call_block(blk);
if (unbox_BOOLEAN(pop()))
{
cognate_list* new = gc_malloc(sizeof *new);
new->object = lst->object;
new->next = NULL;
ptr->next = new;
ptr = new;
}
}
return start.next;
}

static LIST ___range(NUMBER start, NUMBER end)
{
if (end < start)
Expand Down

0 comments on commit 1320b06

Please sign in to comment.