rswier / swizzle

An esoteric programming language
GNU General Public License v2.0
26 stars 2 forks source link

A short manual, please #1

Open wboeke opened 3 years ago

wboeke commented 3 years ago

Hi, I am investigating Swizzle, even extended it with a REPL and replaced the iset functions with clang's code blocks. Howerer I do not understand everything. Am I supposed to learn the language by reading the source code? Also: are recursive functions possible?

Maybe it's a good idea to extend the README a little bit.

Kind regards, Wouter Boeke

colrdavidson commented 3 years ago

So, the tricky bit about Swizzle is that there's a value table (val) and a function table (iset). remap is effectively the only way to jam something into an instruction table slot, so that it gets directly executed, rather than just hitting ivar, idigit, inop, or an existing function.

In most of the example programs, we took the results of a dlsym call into libc and inserted it into a function table slot so that we could effectively call useful c functions from swizzle code. The brainfuck interpreter (bf.swz) is a great example of remap, where we rebound the basic math functions to brainfuck symbols instead, effectively transforming the swizzle interpreter into an extended brainfuck interpreter at runtime.

wboeke commented 3 years ago

Hi, I'm somebody who wants to answer his own questions .... Of course it is possible to create recursive functions in Swizzle. One should do it the same way as in Forth. In this language a recursive fibonacci looks like this: : fib dup 1 > if dup 1 - recurse swap 2 - recurse + else drop 1 then ; Words like dup and swap must be available, so I added them to Swizzle. My fibonacci now is as follows: {F .d (1 > ? .d 1 - F .s 2 - F + : ; 1) } On my laptop the function call: 30 F takes less than 0.5sec, which is on par with existing fast Forth interpreters.

I am still trying to understand how Swizzle works. Some programs in the examples dir look like pure magic to me however.

Wouter

rswier commented 3 years ago

I just added your recursive Fibonacci example (with some slight modifications). I created swizzle functions for the DUP and SWAP. DUP is just &^. DROP is already defined as ;. The SWAP function is pretty ugly, it probably should be added as a built-in to the swizzle interpreter.

Rob