Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A short manual, please #1

Open
wboeke opened this issue Nov 13, 2020 · 3 comments
Open

A short manual, please #1

wboeke opened this issue Nov 13, 2020 · 3 comments

Comments

@wboeke
Copy link

wboeke commented Nov 13, 2020

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
Copy link
Collaborator

colrdavidson commented Nov 14, 2020

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
Copy link
Author

wboeke commented Nov 16, 2020

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
Copy link
Owner

rswier commented Nov 16, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants