recursion; list via support.c

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

@ -58,6 +58,13 @@ PopAST::PopAST(const std::string& n): BaseAST(n) {}
llvm::Value *PopAST::codegen(LLVMState& llvmState) const 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 gep = llvmState.createPop();
auto var = llvmState.createVariable(name); auto var = llvmState.createVariable(name);
auto load = llvmState.builder.CreateLoad(llvmState.inttype, gep); auto load = llvmState.builder.CreateLoad(llvmState.inttype, gep);
@ -65,6 +72,7 @@ llvm::Value *PopAST::codegen(LLVMState& llvmState) const
return Var::addLocal(name, var).value; return Var::addLocal(name, var).value;
} }
}
CallAST::CallAST(const std::string& n): BaseAST(n) {} CallAST::CallAST(const std::string& n): BaseAST(n) {}

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

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
extern int32_t sp; extern int32_t sp;
extern int32_t stack; extern int32_t stack;
@ -9,6 +10,11 @@ void emit()
putchar(*(&stack + --sp)); putchar(*(&stack + --sp));
} }
void print()
{
printf("%d\n", *(&stack + --sp));
}
void sub() void sub()
{ {
int32_t *st = &stack; int32_t *st = &stack;
@ -33,3 +39,33 @@ void eq()
--sp; --sp;
st[sp - 1] = st[sp - 1] == st[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? (nil eq) $null?
($x x) $force ($x x) $force
(10 emit) $cr (10 emit) $cr
(dup car swap cdr) $carcdr
; recursion via y-combinator
($f ($x (^x x) f) dup force) $Y ($g (^g Y)) $rec
; if-stmt ; if-stmt
($c $t $f c ^f ^t rot cswap $_ force) $if ($c $t $f c ^f ^t rot cswap $_ force) $if
($f $t $c $fn ^f ^t ^c fn) $endif ($f $t $c $fn ^f ^t ^c fn) $endif
; range ; range
($self $body $start $end ($self $start $end
^if (^start ^end eq) nil ^if (^start ^end eq)
(^start body ^end ^start 1 + ^body self) ^nil
(^start ^end ^start 1 + self swap cons)
endif
) $range
($self $func $list
^if (^list null?)
nil
(^list carcdr swap func ^func self)
endif endif
) rec $do ) $foreach
70 65 ^emit do cr 58 48 range
^emit foreach cr
) )

Loading…
Cancel
Save