5 ms·
It's hard to imagine what real lisp-like macros look like with other, non-homoiconic langauges. Anyone have an example?
by endlessvoid94 12y ago
It's hard to imagine what real lisp-like macros look like with other, non-homoiconic langauges. Anyone have an example?
- daeken 12y agoNemerle is non-homoiconic, but has full-on macros. Here you can see some examples from my Nextem library (a bunch of niceties I built up over the years): http://trac.assembla.com/nextem/browser/trunk/Examples/String.n http://trac.assembla.com/nextem/browser/trunk/Examples/Strin... along with the macros behind it http://trac.assembla.com/nextem/browser/trunk/Macros/StringFormatting.n http://trac.assembla.com/nextem/browser/trunk/Macros/StringF... http://trac.assembla.com/nextem/browser/trunk/Macros/Regex.n http://trac.assembla.com/nextem/browser/trunk/Macros/Regex.n The macros run at compile-time, and emit code by use of the <[ ]> blocks -- those get turned into ASTs at compile-time and get filled with the proper data.
- Cyther606 12y agoNimrod has strong metaprogramming capabilities. template htmlTag(tag: expr) {.immediate.} = proc tag(): string = "<" & astToStr(tag) & ">" htmlTag(br) htmlTag(html) echo br() produces: <br> http://nimrod-lang.org/talk01/slides.html http://nimrod-lang.org/talk01/slides.html https://news.ycombinator.com/item?id=7071429 https://news.ycombinator.com/item?id=7071429
- S4M 12y agoJulia is not lisp like, and has macros [1]. The way it works is that your code is first interpreted to get the AST that looks like LISP code (`a+bc+1` becomes `+(a,(b,c),1)`) and your macro operates on that. So basically it works like a lisp macro (until you want to create a macro that creates macro, which is something I haven't done but heard about). http://docs.julialang.org/en/release-0.2/manual/metaprogramming/ http://docs.julialang.org/en/release-0.2/manual/metaprogramm...
- S4M 12y agoCORRECTION: `+(a,*(b,c),1)` is NOT like LISP code. However, Julia helps you to get the head of an expression (in this case, +) and it's args.
- MartinMond 12y agoCheck out Elixir's metaprogramming guide: http://elixir-lang.org/getting_started/meta/1.html http://elixir-lang.org/getting_started/meta/1.html
- jpd 12y agoBesides those already mentioned, there's Dylan, which was originally S-expression based before switching to it's Wirth-style syntax.
- tel 12y agoHaskell's "Template Haskell" system is pretty sophisticated... And challenging enough to be some deep black magic. You end up being able to reify the entire Haskell syntax tree from "quoted" fragments, manipulate and generate a modified tree, and then "print" it back into program flow. Totally capable, but not terrifically fun. It also has some compilation restrictions which are annoying if necessary.
- dllthomas 12y agoI just (last night) dug into some TH hackery. I put together a quasiquoter for testing/debugging that would walk a do block and decorate every statement with (`onException` ...) set up to print the source location when an exception is thrown. I actually had to first parse with haskell-src-exts to get the location info, and currently I'm turning it into an ExpQ by pretty-printing and reparsing - which I don't love, but gets the job done. https://github.com/dlthomas/snowdrift/compare/5094b1ca0e5002c0d12a7f714b083d88d450ddb3...e7bf8ac8b6320fcc46233248170920fc97d805c9 https://github.com/dlthomas/snowdrift/compare/5094b1ca0e5002...
- judk 12y agoTH also ruins Haskell by inserting inivisble code with spooky action at a distance, where you have to pre-import symbols that TH will insert later, and compiler errors refer to code defined somewhere else but print line numbers of where the generated code is inserted. TH needs a compiler!
- danschuller 12y agoMetalua is a good example of a macros with an Algol-like syntax. It's built on top of Lua - as you might guess from the name. http://metalua.luaforge.net/quicktour.html http://metalua.luaforge.net/quicktour.html
- klibertp 12y agoSweet.js (http://sweetjs.org http://sweetjs.org) and MacroPy (https://github.com/lihaoyi/macropy https://github.com/lihaoyi/macropy) come to mind. Actually "real macros" are just macros which operate on AST instead of on strings, they are hard to do if you have to re-implement language parser, but rather easy if the language exposes its AST api, like in Python case.