orangeduck / BuildYourOwnLisp

Learn C and build your own programming language in under 1000 lines of code!
http://www.buildyourownlisp.com/
Other
2.9k stars 394 forks source link

question about conditional evaluation #46

Closed radiofreejohn closed 10 years ago

radiofreejohn commented 10 years ago

Just curious, is there a convention for when something should or should not have delayed evaluation?

Your conditional implementation takes 3 args, the first is the conditional as an s-expression and the other two are q-expressions.

Why aren't all the arguments q-expressions with the conditional evaluated in builtin_if?

I only ask this because when I built my conditional (before reading yours) I made all the arguments q-expressions and evaluated them in builtin_if one by one.

I sort of understand why the functions for the condition are q-expressions: you want the "if" operator to execute them later if necessary.

orangeduck commented 10 years ago

There isn't really a convention. I think it is a matter of taste ultimately, and that having if take three Q-Expressions is fine. Ultimately there isn't much difference.

I choose to make if take as the first input the result of an S-Expression because that part is always going to be evaluated, so you may as well let the S-Expression mechanism do that for you. It also makes it look more like classical if constructs from C for example, which have the condition in parenthesis.

The other inputs to if have to be delayed in evaluation because only one of them should actually be evaluated and you don't know which till the function runs. Generally I've made the language so that inputs are only Q-Expressions when their evaluation has to be delayed.

radiofreejohn commented 10 years ago

Thanks, that makes sense :)