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

How can we implement macros #169

Open ssrangisetti opened 2 years ago

ssrangisetti commented 2 years ago

The approach I am taking is to make macro similar to lambda and pass arguments as q expressions and macro would return q expression which I will evaluate. But since we cannot create s expressions macros have to return them as q expressions (eg. (* (+ 1 2) 3) will be returned as {* {+ 1 2} 3}). But I am having issues with differentiating between actual q expressions and s expressions converted to q expressions. I have tried using keyword qexpr like {qexpr {a b c d}} would represent {a b c d} but this approach fails if we ever use join on this.

Is it possible to implement macros without any special syntax like quote? Or is quote actually required for having macro support

orangeduck commented 2 years ago

The main difference between Macros and Q-Expressions is that with Macros you actually look at the function name itself to decide if it is going to evaluate the arguments or not. Where are with Q-Expressions there is dedicated syntax to halt evaluation. So perhaps I would start by making a way to define functions which do not evaluate their arguments, and have these be detected in the evaluation function. Hope that helps!