Argent: Cog for Ruby

Tuesday 29 March 2005This is over 19 years old. Be careful.

Cullen O’Neil wrote to tell me that he liked Cog enough to re-implement it in Ruby. The result is Argent. Now you can use Ruby for code generation:

// This is my C++ file.
...
/*[[$argent
  ['DoSomething', 'DoAnotherThing', 'DoLastThing'].each do |fn|
    $argent.outl("void #{fn}();")
  end
  $]]*/
//[[$end$]]
...

Reading the Argent code was an interesting experience: I don’t really know Ruby, but I’m familiar with how Cog is implemented, so I “knew” the code without knowing the language. It helped me understand more about Ruby.

Also cool: Cullen uses Argent to manage its own Makefile. He generates test classes and Makefile entries for them using Argent itself.

Comments

[gravatar]
Cool stuff Ned. I really think Cog has big potential. The really powerful part about it is that the code-generation code is embedded right in the host source file, and then of course you get to see exactly what it generated right in the same file. The biggest thing that's missing is the ability to run generation scripts right from the IDE so that the Cog generation step is easily separated from the build step

I think eventually we'll see Cog like functionality and support right in the IDE (whatever IDE that may be. I'm rhyming, wheee!). My feeling is some vendor is going to do a slick job integrating that functionality and then more vendors and projects will follow suit. Eventually it will be like "intellisense" and people will take it for granted.

Unrealistic predictions perhaps, but I'm really sold on the Cog approach.
[gravatar]
Cullen is ahead of you: I forgot to mention another thing he added to argent: a -t switch which toggles between generating the code and excising the generated code. He binds "argent -t" to a key in his editor, and then can hit the key to toggle between having generated code and not.
[gravatar]
Any interest in slightly modifying the Cog header part to identify the language being used and then merge the two? :)

Actually there is probably no point and then that would add the painful requirement of the command-line script having to know how to delegate to another interpreter if it came across another language block.
[gravatar]
Trent: a multi-language cog would be very cool, but the problem is how to execute the generators? It's very easy in cog.py to execute Python generators, and in cog.rb it's easy to execute Ruby generators, but how do you do it vice-versa?
[gravatar]
One approach to building a multi-lingual Cog would be to redo the Cog engine to use Active Scripting. There are Active Scripting versions of Python and Ruby as well as several other languages.

Alternatively, the Bean Scripting Framework (BSF) could be used. Only caveat is that it's written in Java. so it would require Jython and JRuby.

That said, either approach may be overkill. Cog is a simple and powerful idea that isn't tied to Python or Ruby. I think the idea is more important than the specifics of the implementation. This is analogous to how Ant inspired a .NET version called Nant and JUnit inspired a .NET version called NUnit. Good ideas are portable.

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.