Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Turing preprocessor.
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Fri Nov 12, 2004 8:03 pm   Post subject: 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:

code:
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:

code:
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:

code:
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".



turing_extender.rb.txt
 Description:
Remove the ".txt", which was added to get through CompSci extension filters.

Download
 Filename:  turing_extender.rb.txt
 Filesize:  7.12 KB
 Downloaded:  261 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Nov 12, 2004 8:09 pm   Post subject: (No subject)

Oh, and you can mix integers and reals in with strings:

code:
case "12" of
   label 12: put "It's there."
   label: put "It isn't there."
end case


becomes:

code:
if "12" = intstr(12) then
   put "It's there."
else
   put "It isn't there."
end if
Delos




PostPosted: Fri Nov 12, 2004 10:45 pm   Post subject: (No subject)

Umm...I'm not sure if I understood or not...but when I ran
Quote:

code:

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




PostPosted: Fri Nov 12, 2004 11:09 pm   Post subject: (No subject)

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. Smile
Delos




PostPosted: Sat Nov 13, 2004 12:15 am   Post subject: (No subject)

I'd say...I was looking through the code...and well...yeah, I still have a loooong way to go w/ Ruby.
wtd




PostPosted: Sat Nov 13, 2004 12:24 am   Post subject: (No subject)

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. Smile

I'm especially fond of select. Consider an array of arrays of numbers:

code:
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.

code:
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?

code:
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:

code:
arr2 = [1,2,4,2,9,87]
sum = arr2.inject(0) { |a, b| a + b }
Viper




PostPosted: Mon Nov 22, 2004 1:27 pm   Post subject: (No subject)

i find it very confusing (its way outta ma league lol)
wtd




PostPosted: Mon Nov 22, 2004 1:52 pm   Post subject: (No subject)

Viper wrote:
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:

code:
var arr : arry 1 .. 10 of string
arr(1) = "hello"
arr(2) = "world"
% yada yada


You can just have:

code:
arr = ["hello", "world"]
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: