Post-Programming for Dummies

February 4, 2008

continuation * nontrivial

Filed under: Uncategorized — ppfd @ 3:31 pm

So that previous post kind of set me to work on developing a set of meta-programming tools for ruby. This is very experimental. Really I’m just having fun writing my first compiler which tokenizes ruby with extra symbols and implements changes to the code following a set of rules. The goal is to set it up so that one can write “meta-programming strategies” (kind of like LISP macros, but different in a way that I’ll explain).

Here are the two examples I’ve implemented so far:

First, a string literal reverser:

meta: StringLiteralReverse

a = “abcdef”

b = “he said \”yo!\”"

endMeta

outputs to

a = “fedcba”

b = “\”!oy\” dias eh”

Cool, huh?

Next example is more sophisticated and highlights the “power” of these types of mini meta-programs w/r/t Lisp macros:

meta: BindingsCapture

a = 1

b = “hello!”

c = [what's your name?]

endMeta

meta: BindingsCapture

eachBinding(sym)

if (sym.class == Array)

sym << “yo!”

elsif (sym.class == String)

sym += “yo!”

else

sym += Char(‘y’) + Char(‘o’)

end

symCopy = sym

endMeta

outputs to:

a = 1

b = “hello!”

c = [what's your name?]

if a.class == Array

a << “yo!”

elsif a.class == String

a += “yo!”

else

a += Char(‘y’) + Char(‘o’)

end

aCopy = a

etc.

I’m totally eschewing the normal Compiler methodology (Parser -> Tokenizer -> Syntax Tree -> etc.) because really all I’m doing amounts to is implementing a language for writing FSM’s which use regular expressions to search in code. One of the reasons I want to write this ruby extension is that I like writing programs which use globally defined functional predicates. For example:

$startsWithColon = Proc.new {|x| x.class == String && x[0..0] = “:”}

$endsWithPeriod = Proc.new {|x| x.class == String && x[-1..-1] = “.”}

But I also like writing combination methods, for example:

$andPreds = Proc.new {|*x| Proc.new {|*y| val = true ; x.each do {|proc| val = val && proc[*y]}}}

But let’s say I’ve made a lot of the above predicates (startsWithColon, endsWithPeriod, etc.) and I want to form the and predicates (so that I can easily refer to, for instance startsWithColonEndsWithPeriod). This obviously requires some scripting (since it’s just a matter of calling the functions correctly. I want to be able to write something like:

meta: CombineProcs

setProcs(procs) // startsWithBlah, endsWithBlah

setCombinators(combinators) // and, or, xor, etc.

writeCombinations // writes out the combinations in the form startsWithBlahAndEndsWithBlah, etc. – will write lots

endMeta

You might ask what is the value with this. I say there are two values:

1.) Ease of reference (it is annoying to combine predicates, especially in “pseudo-functional” languages like Ruby)

2.) Verifiability (because it is done automatically I can rest assured that only the basic definitions need to be correct)

Then we can start combining things. For instance, how do we get the “procs” and “combinators” variables above? Well, we simply write a meta flag around our (named) predicates and they are automatically collected (nice!) E.g. we might write something like:

meta: BindingsCapture -> procs // puts the bindings in a named array

$startsWithBlah = Proc.new blah

$endsWithBlah = Proc.new blah

endMeta

etc.

Since I eschew the normal methodologies associated with tokenization (which creates the false dichotomy of symbol/operator) I am free to play games with variable names and crap.

I think that this sort of play has been largely missing in the programming world and I will tell you why – there is a strong understanding of the computer-related elements of semantics/syntax, but there is a somewhat weak understanding of the relation of those elements to their human-oriented correlates. How does a variable assignment correspond to what is going on in my brain? What is the semantic value of a variable name? Etc. Time will prove how useful (or unuseful) this approach is.

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.