
-----------------------------------
wtd
Fri Nov 12, 2004 8:03 pm

Turing preprocessor.
-----------------------------------
Well, it's written in Ruby, but it effects Turing, so I figured this was the best place to post this.  

Currently, Turing only allows integers to be used in "case" (or "switch" for the C-ish) statements.  The ability to use strings in such places can be useful, though.  With that in mind, I decided to write a simple program which reads in a Turing program, finds a case statement using strings, and translates it into an equivalent if...elsif...else statement.

So something like:

var foo := "hello"

case foo of
   label "foo": put "foo"
   label "bar": put "bar"
   label "hello": put "hello"
   label "world": put "world"
   label: put "Nothing!"
end case

Becomes:

var foo := "hello"

if foo = "foo" then
   put "foo"
elsif foo = "bar" then
   put "bar"
elsif foo = "hello" then
   put "hello"
elsif foo = "world" then
   put "world"
else
   put "Nothing!"
end if

To run the program:

C:\> ruby turing_extender.rb my_turing_file.t

This will create the new code in "my_turing_file.t" and save a backup of the not-exactly-Turing code in "my_turing_file.t.bak".

-----------------------------------
wtd
Fri Nov 12, 2004 8:09 pm


-----------------------------------
Oh, and you can mix integers and reals in with strings:

case "12" of
   label 12: put "It's there."
   label: put "It isn't there."
end case

becomes:

if "12" = intstr(12) then
   put "It's there."
else
   put "It isn't there."
end if

-----------------------------------
Delos
Fri Nov 12, 2004 10:45 pm


-----------------------------------
Umm...I'm not sure if I understood or not...but when I ran


var foo := "hello" 

case foo of 
   label "foo": put "foo" 
   label "bar": put "bar" 
   label "hello": put "hello" 
   label "world": put "world" 
   label: put "Nothing!" 
end case



in OOT, it ran, it worked, and I got a "hello" as the output...no syntax error or nothing...
Am I missing something?  (Perhaps I should dl the .rb file to find out...hmm...)

-----------------------------------
wtd
Fri Nov 12, 2004 11:09 pm


-----------------------------------
Huh... I was under the impression only ints were allowed in Turing's case statements.  Live and learn.

Still, it provided an interesting exercise in finding variable declarations.  :)

-----------------------------------
Delos
Sat Nov 13, 2004 12:15 am


-----------------------------------
I'd say...I was looking through the code...and well...yeah, I still have a loooong way to go w/ Ruby.

-----------------------------------
wtd
Sat Nov 13, 2004 12:24 am


-----------------------------------
The biggest thing that will help you with respect to the program I wrote is understanding regular expressions.

Of course, I also make heavy use of blocks, but then, every good Ruby programmer should.  :)

I'm especially fond of select.  Consider an array of arrays of numbers:

arr = [[1, 2, 3], [42, 53, 12], [678, 32, 15]]

Let's say you want each array in that array where the second element is greater than 4.

new_arr = arr.select { |sub_arr| sub_arr[1] > 4 }

The collect method is fun too.  What if you want to multiply each element in each subarray by 2?

new_arr2 = arr.collect { |sub_arr| sub.arr.collect { |element| element * 2 } }

But nothing's more fun (or mind-warping) than inject.  To sum an array:

arr2 = [1,2,4,2,9,87]
sum = arr2.inject(0) { |a, b| a + b }

-----------------------------------
Viper
Mon Nov 22, 2004 1:27 pm


-----------------------------------
i find it very confusing (its way outta ma league lol)

-----------------------------------
wtd
Mon Nov 22, 2004 1:52 pm


-----------------------------------
i find it very confusing (its way outta ma league lol

Probably one of the biggest stumbling blocks there is the idea of arrays in Ruby, and the fact that they can be represented literally.

Instead of:

var arr : arry 1 .. 10 of string
arr(1) = "hello"
arr(2) = "world"
% yada yada

You can just have:

arr = ["hello", "world"]
