Macros

38 views
Skip to first unread message

Rib Rdb

unread,
Sep 14, 2010, 8:46:36 PM9/14/10
to mi...@googlegroups.com
I've finally checked in the first feature to take advantage of the new parser.  Now macros can use backticks just about anywhere, which means you can generate most ast without resorting to writing ruby code.  We should be able to use this to really clean up a lot of the current code base.  Here's an example of how to implement something like ruby's attr_accessor:

      import duby.lang.compiler.StringNode
      class Foo
        macro def attr_accessor(name_node, type)
          name = StringNode(name_node).literal
          quote do
            def `name`
              @`name`
            end
            def `"#{name}_set"`(`name`:`type`)
              @`name` = `name`
            end
          end
        end

        make_attr :foo, :int
      end

      x = Foo.new
      puts x.foo
      x.foo = 3
      puts x.foo

Michal Hantl

unread,
Sep 15, 2010, 1:20:18 AM9/15/10
to mi...@googlegroups.com
Looks awesome!

--
S pozdravem, Regards
Michal Hantl

gtalk/jabber: mic...@hantl.cz
icq: 241813215

Reply all
Reply to author
Forward
0 new messages