mikera
mikera

Reputation: 106371

Functions vs. symbols for Lisp / Clojure DSL

I'm creating a DSL in Clojure and have a choice between either:

a) Representing the DSL in symbolic form that can be converted to an AST later:

'(foo (bar (baz 1) (boo 3)))

b) Representing the DSL as pure functions that generate AST nodes:

(foo (bar (baz 1) (boo 3)))
=> [AST with foo at top level]

The AST will subsequently be compiled.

Is there any strong reason to prefer one approach vs. the other?

Upvotes: 3

Views: 232

Answers (1)

Ankur
Ankur

Reputation: 33657

It looks like the approach a) is more flexible in the sense that the parser will parse the s-expressions and basically can move back and forward in the expression tree to generated the required code for ex: while parsing child of foo the parser can backtrack to foo or may be to foo's parent to fetch some other required info etc. Whereas in the b) approach the DSL is normal function calls so boo call doesn't know about its parent and so on and hence you cannot apply backtracking in that case.

In terms of complexity - the a) approach although is flexible can be a bit complex specially if backtracking is implemented whereas approach b) should be easy to implement,

Upvotes: 5

Related Questions