How do I print a block without it being evaluated in Rebol

146 Views Asked by At

I'm trying to put together a code generator and need to print a block (that contains some code for a target language) without it (i.e, the block) being evaluated. How should I go about doing this?

Edit 1 -- I understand that this could be easily accomplished if I were to embed the Ruby code as a string but since the Rebol parser doesn't seem to mind, I thought why not?

rebol []

x: [
    [sym0 [(1..10).map{|n| puts n}]]
    [sym1 [foo << [1, 2, 3]]]
]

print x/1/1 ;prints sym0
print x/1/2 ;fails as print tries to evaluate the block
2

There are 2 best solutions below

1
On

Try MOLD. It generates a string from Rebol data:

>> mold [1 + abc + 12-Dec-2012]
== "[1 + abc + 12-Dec-2012]"

Note that MOLD is not a perfect counterpart to LOAD. In the above case, you could LOAD that structure back out of the string...but some of the binding information is going to be lost in the general case.


Edit 1: Well what you are trying to do is pretty wacky. What you get from mold is:

>> mold x/1/2
== {[(1.0.10) .map "|n| puts n"]}

It speaks to Rebol's curious nature that so many things are legal that other languages would generate syntax errors over. That's part of what makes it a nice adaptable English-like system for writing DSLs.

But... Rebol simply isn't Ruby. The lexical interpretation of symbols is totally different. That means you either have to do something Rubol-like to "shoehorn" Ruby code into Rebol's model as a kind of "Ruby DOM"... and then coerce it back out with a ruby-mold (which could be cool, I'm for it). Or you have to use a string.

1
On

Use PROBE. PROBE doesn't evaluate data it prints. Also it returns the same data you feed it, so you can put PROBE anywhere in your code to check your values which is great for debugging.

MY-FUNC MY-DATA
MY-FUNC PROBE MY-DATA ; will print unevaluated MY-DATA to console
PROBE MY-FUNC MY-DATA ; will print unevaluated output of MY-FUNC to console