Skip to content

Commit

Permalink
vm: rename asmWith to runheap.
Browse files Browse the repository at this point in the history
`asm` now expects ION and outputs the start address and the heap.

`runion` expects ION and runs it on standard input and output.
  • Loading branch information
blynn committed Jan 3, 2024
1 parent 3515a7d commit ee1258d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
15 changes: 11 additions & 4 deletions golf.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ where:
------------------------------------------------------------------------
data CL = S | K | App CL CL
encode m n = case m of
x :@ y -> (True, (encode x (encode y n)))
S -> (False, (False, n))
K -> (False, (True , n))
App x y -> (True, (encode x (encode y n)))
------------------------------------------------------------------------

Some of our type-challenged compilers will happily run this crazy code.
Expand Down Expand Up @@ -161,6 +161,14 @@ C 00001
K 00000
------------------------------------------------------------------------
Beware! For some reason I flipped the usual Church encodings of booleans in
this example. That is, just for this section, we have:
------------------------------------------------------------------------
False = \x y -> x
True = \x y -> y
------------------------------------------------------------------------
Then the following is a binary self-interpreter:
------------------------------------------------------------------------
Expand All @@ -185,12 +193,11 @@ bits.
The following demonstrates this self-interpreter in our "Fixity" compiler:
------------------------------------------------------------------------
data Bool = True | False;
data Bool = False | True;
id x = x;
const x y = x;
flip x y z = x z y;
ap x y z = x z(y z);
bool x y b = case b of { True -> y ; False -> x };
uncurry f x = case x of { (,) a b -> f a b };
(.) f g x = f(g x);
data CL = K | C | S | T | V | B | App CL CL;
Expand All @@ -204,7 +211,7 @@ encode m n = case m of
; App x y -> (True, (encode x (encode y n)))
};
t = uncurry;
v = bool;
v x y z = z x y;
eval c = t(v(t(v(t(t . v(v(t(c . v const flip))(c ap))(c . v t v)))(c(.)))) (eval(eval . (c .))));
demo _ = eval id (encode (App T (App K (App (App S K) K))) ("one", "two"));
------------------------------------------------------------------------
Expand Down
20 changes: 15 additions & 5 deletions vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,15 @@ void parseMore(u (*get)()) {
char *str;
u str_get() { return *(unsigned char*)str++; }

void parse(char *s) {
void parseWith(u (*get)()) {
hp = 128;
tabn = 0;
parseMore(get);
}

void parse(char *s) {
str = s;
parseMore(str_get);
parseWith(str_get);
}

void parseRaw(char *s) {
Expand Down Expand Up @@ -663,12 +667,18 @@ int main(int argc, char **argv) {
if (!strcmp(argv[1], "testdis")) return dis("disassembly.hs"), 0;
if (!strcmp(argv[1], "dis")) return dis(argv[2]), 0;
if (!strcmp(argv[1], "asm")) {
fp_reset("raw");
loadRaw(fp_get);
parseWith(ioget);
printf("%u", root_memo);
for(u i=128; i<hp; i++) printf(",%u", mem[i]);
return 0;
}
if (!strcmp(argv[1], "runion")) {
fp_reset(argv[2]);
parseWith(fp_get);
run(ioget, pc);
return 0;
}
if (!strcmp(argv[1], "asmWith")) {
if (!strcmp(argv[1], "runheap")) {
fp_reset(argv[2]);
loadRaw(fp_get);
run(ioget, pc);
Expand Down

0 comments on commit ee1258d

Please sign in to comment.