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

Username:   Password: 
 RegisterRegister   
 [Tutorial] Extending Turing to include custom functions.
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tony




PostPosted: Thu Oct 06, 2005 8:33 am   Post subject: [Tutorial] Extending Turing to include custom functions.

I've seen the following question too often: my version of Turing is missing Math.Distance() or the such. I have v4.0.5, others might not. That's not the point - you can extend your Turing yourself.

This is really easy.

1) Locate where your copy of Turing is installed. Mine is in C:\Program Files\Turing\
2) Follow on to Turing\Support\predefs\
3) Create a new file, for example - new_function.tu
4) Create your module and functions
Turing:

unit
module Tony
    export talk

    function talk : string
        result "words words words.."
    end talk

end Tony

5) Save, then open Turing\Support\predefs\Predefs.lst. Everything referenced in this file is included in your program.
6) At the very end of the file, add a reference to the module file you've created in step 3. "%oot/support/predefs/new_function.tu". Save.

Now you can use your Module.function in any program.

Turing:

put "Hello Tony, how are you doing?"
put Tony.talk


Just keep in mind that those functions are stored on your local drive, not everybody else's -- so let others know of the modifications you're using when you're posting your source code.

Oh, and for those who are still looking for that elusive Math.Distance -- here are the contents of the Math. file
Turing:

    %
    % Calculate the distance between two points
    function Distance (x1, y1, x2, y2 : real) : real
        var dx : real := x1 - x2
        var dy : real := y1 - y2
        result sqrt (dx * dx + dy * dy)
    end Distance

    %
    % Calculate the distance between a point and a line segment
    %
    function DistancePointLine (px, py, x1, y1, x2, y2 : real) : real
        var lineSize : real := Distance (x1, y1, x2, y2)
        if lineSize = 0 then
            result Distance (px, py, x1, y1)
        end if

        var u : real := ((px - x1) * (x2 - x1) +
            (py - y1) * (y2 - y1)) / (lineSize * lineSize)

        if u < 0.0 then
            result Distance (px, py, x1, y1)
        elsif u > 1.0 then
            result Distance (px, py, x2, y2)
        else
            var ix : real := x1 + u * (x2 - x1)
            var iy : real := y1 + u * (y2 - y1)
            result Distance (px, py, ix, iy)
        end if
    end DistancePointLine
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Thu Oct 06, 2005 10:46 am   Post subject: (No subject)

Nice...

but i messed somthing up so ill do with out Smile

ill go the old fation way...

Also when you **compile** a file with your custom funtions, will it work on others computers?
Tony




PostPosted: Thu Oct 06, 2005 10:47 am   Post subject: (No subject)

yes, compiled programs are independent of resources (as in -- one doesn't need Turing to be installed to run an .exe)
Cervantes




PostPosted: Thu Oct 06, 2005 4:05 pm   Post subject: (No subject)

Good idea, Tony. Now, some more!
This is effectively the same as what Tony described. I have, however, expanded on where to place the new line in predefs.lst and also discussed export unqualified and pervasive.

Creating New Variable Types

Now that we know how to add procedures/functions to the Turing environment, we should take that a step further. What if you want to add your own variable type?

The one I use most frequently is called coord2D. Another good one to add is vector2D. These could be extended to 3D as well.

Adding custom data types is much the same as adding custom functions. Create a new file in Turing\Support\predefs\ called Custom_Data_Types.tu or something like that. Insert this code:
Turing:

unit
module Custom_Data_Type  % Note that this needs to match the file name, except without the '.tu'
        export coord2D, vector2D
       
        type coord2D :
                record
                        x, y : real
                end record

        type vector2D :
                record
                        mag, ang : real  % Magnitude, Angle
                end record

end Custom_Data_Type

Change the type if you wish. Add some more types if you wish. Just make sure to export them in the top line.

Save.

Open up Turing\Support\predefs\predefs.lst.
Add "%oot/support/predefs/Custom_Data_Types.tu" just after the anyclass (which is the first line). Adjust the filename (Custom_Data_Type.tu) as appropriate.
I say to put this near the top because doing this will allow you to use your custom types within the other modules. For example, I have a good many of the Mouse functions to work with coordinates rather than two seperate x and y parameters.If you want to use your custom functions in other modules, you'll need to save them near the top, not at the bottom.

You're done. Keep in mind though that you didn't make these things pervasive, or export unqualified. In other words, you will have to import them into modules/class etc., and you will have to attach the module name before the type.

To make these things export unqualified and pervasive, add a ~. and * before the type/function name. Like this:
Turing:

unit
module Custom_Data_Type  % Note that this needs to match the file name, except without the '.tu'
        export ~.*coord2D, ~.*vector2D
       
        type coord2D :
                record
                        x, y : real
                end record

        type vector2D :
                record
                        mag, ang : real  % Magnitude, Angle
                end record

end Custom_Data_Type
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: