recursion; list via support.c

main
Clyne 6 months ago
parent ed78a6054c
commit 6be79eda61
Signed by: clyne
GPG Key ID: 1B74EE6C49C96795

@ -58,12 +58,20 @@ PopAST::PopAST(const std::string& n): BaseAST(n) {}
llvm::Value *PopAST::codegen(LLVMState& llvmState) const
{
if (name == "self") {
extern std::list<ThunkAST> scope;
auto func = scope.back().func;
auto var = llvmState.createVariable(name);
llvmState.builder.CreateStore(func, var, false);
return Var::addLocal(name, var).value;
} else {
auto gep = llvmState.createPop();
auto var = llvmState.createVariable(name);
auto load = llvmState.builder.CreateLoad(llvmState.inttype, gep);
llvmState.builder.CreateStore(load, var, false);
return Var::addLocal(name, var).value;
}
}
CallAST::CallAST(const std::string& n): BaseAST(n) {}

@ -31,7 +31,7 @@
#include "var.hpp"
static LLVMState llvmState;
static std::list<ThunkAST> scope;
std::list<ThunkAST> scope;
static bool parseString(std::string_view sv);
@ -42,8 +42,10 @@ int main()
std::string line;
for (unsigned lineno = 1; std::cin.good(); ++lineno) {
std::getline(std::cin, line);
if (!parseString(line))
if (!parseString(line)) {
std::cerr << " at line " << lineno << std::endl;
return -1;
}
}
llvmState.output();

@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
extern int32_t sp;
extern int32_t stack;
@ -9,6 +10,11 @@ void emit()
putchar(*(&stack + --sp));
}
void print()
{
printf("%d\n", *(&stack + --sp));
}
void sub()
{
int32_t *st = &stack;
@ -33,3 +39,33 @@ void eq()
--sp;
st[sp - 1] = st[sp - 1] == st[sp];
}
void cons()
{
int32_t *st = &stack;
int32_t *pair = malloc(2 * sizeof(int32_t));
--sp;
pair[0] = st[sp];
pair[1] = st[sp - 1];
st[sp - 1] = (int32_t)pair;
}
void car()
{
int32_t *st = &stack;
st[sp - 1] = ((int32_t *)st[sp - 1])[0];
}
void cdr()
{
int32_t *st = &stack;
st[sp - 1] = ((int32_t *)st[sp - 1])[1];
}
void decons()
{
int32_t *st = &stack;
--sp;
free((int32_t *)st[sp]);
}

@ -10,20 +10,27 @@
(nil eq) $null?
($x x) $force
(10 emit) $cr
; recursion via y-combinator
($f ($x (^x x) f) dup force) $Y ($g (^g Y)) $rec
(dup car swap cdr) $carcdr
; if-stmt
($c $t $f c ^f ^t rot cswap $_ force) $if
($f $t $c $fn ^f ^t ^c fn) $endif
; range
($self $body $start $end
^if (^start ^end eq) nil
(^start body ^end ^start 1 + ^body self)
($self $start $end
^if (^start ^end eq)
^nil
(^start ^end ^start 1 + self swap cons)
endif
) $range
($self $func $list
^if (^list null?)
nil
(^list carcdr swap func ^func self)
endif
) rec $do
) $foreach
70 65 ^emit do cr
58 48 range
^emit foreach cr
)

Loading…
Cancel
Save