Writing your own Lisp-ette is a brilliant evening or weekend project, regardless of the language. It's some of the simplest non-toy parsing you can attempt, a bit of light data structure work, and understanding eval/apply is 80% of the work in implementing it. I would highly recommend anyone to have a go, and try not to follow existing code too closely: figure out the problems in your language of choice. The post ide…
Lisp in fewer than 200 lines of C
61–70 of 108 posts
Re: Lisp in fewer than 200 lines of C
#62If you like this, you might like Lisp interpreter written in assembly in a single file. It is one of the best commented code ever written imo. https://github.com/marcpaq/arpilisp
Re: Lisp in fewer than 200 lines of C
#63Oof, all the macros are broken: #define is_space(x) (x == ' ' || x == '\n') #define is_parens(x) (x == '(' || x == ')') Should be #define is_space(x) ((x) == ' ' || (x) == '\n') #define is_parens(x) ((x) == '(' || (x) == ')') Probably doesn’t matter in practice for this. It could end up being a nasty source of bug later on in the project.
#define is_space(x) ({ typeof(x) y = x; y == ' ' || y = '\n'; })
(Or in this case, turn it into an actual function and let the compiler figure out optimization.)Re: Lisp in fewer than 200 lines of C
#64Writing your own Lisp-ette is a brilliant evening or weekend project, regardless of the language. It's some of the simplest non-toy parsing you can attempt, a bit of light data structure work, and understanding eval/apply is 80% of the work in implementing it. I would highly recommend anyone to have a go, and try not to follow existing code too closely: figure out the problems in your language of choice. The post ide…
I'm writing Scheme R5RS in Kotlin (https://github.com/kovrik/scheme-in-kotlin) and have implemented everything except macros (6).
Have no idea how to beat them.
Re: Lisp in fewer than 200 lines of C
#65 if (is_pair(cdr(ob))) {
printf(" ");
print_obj(cdr(ob), 0);
}
How could this `if` statement ever evaluate to false? We already verified that `cdr(ob) != 0`, and the CDR can never be a plain old string, so isn't this `if` superfluous?Re: Lisp in fewer than 200 lines of C
#66Writing your own Lisp-ette is a brilliant evening or weekend project, regardless of the language. It's some of the simplest non-toy parsing you can attempt, a bit of light data structure work, and understanding eval/apply is 80% of the work in implementing it. I would highly recommend anyone to have a go, and try not to follow existing code too closely: figure out the problems in your language of choice. The post ide…
Agree. I'm writing Scheme R5RS in Kotlin ( https://github.com/kovrik/scheme-in-kotlin ) and have implemented everything except macros (6). Have no idea how to beat them.
Re: Lisp in fewer than 200 lines of C
#67If you like this, you might like Lisp interpreter written in assembly in a single file. It is one of the best commented code ever written imo. https://github.com/marcpaq/arpilisp
Re: Lisp in fewer than 200 lines of C
#68Oof, all the macros are broken: #define is_space(x) (x == ' ' || x == '\n') #define is_parens(x) (x == '(' || x == ')') Should be #define is_space(x) ((x) == ' ' || (x) == '\n') #define is_parens(x) ((x) == '(' || (x) == ')') Probably doesn’t matter in practice for this. It could end up being a nasty source of bug later on in the project.
That still evaluates x twice, which can also be a source of bugs. I usually take one of two approaches: either decide that this is a weekend hack and using the macros whenever the expansion isn't obvious in my head is a sign of too much complexity, or use this GCC extension: #define is_space(x) ({ typeof(x) y = x; y == ' ' || y = '\n'; }) (Or in this case, turn it into an actual function and let the compiler figure o…
template
bool is_space(const T & x) {
return x == ‘ ‘ || x == ‘\n’;
}Re: Lisp in fewer than 200 lines of C
#69Earlier quoted context omitted.
Agree. I'm writing Scheme R5RS in Kotlin ( https://github.com/kovrik/scheme-in-kotlin ) and have implemented everything except macros (6). Have no idea how to beat them.
Just think of them as defining functions that take s expressions in and spit s expressions out before “normal” runtime evaluation and which hence only see built in symbols.
But then there are things like hygiene, performance and some tricky edge-cases.
And I couldn't find any standard (and simple) algorithm to implement macros (preferably written in something other than Scheme itself).
Still trying to wrap my head around.
Re: Lisp in fewer than 200 lines of C
#70If you like this, you might like Lisp interpreter written in assembly in a single file. It is one of the best commented code ever written imo. https://github.com/marcpaq/arpilisp
Since this is written in assembly is it much faster than a C version since this one can manage its own stack frames and stack variables and such? I always imagined that’s the case and that a lisp implemented fully in assembly would be the trick to a super fast lisp that can complete with Go.