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

Username:   Password: 
 RegisterRegister   
 Including via variables?
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
StealthArcher




PostPosted: Sat Jan 12, 2008 12:21 pm   Post subject: Including via variables?

Is it possible? Or at least possible to choose to import a certain turing files' procedure via user input?
Sponsor
Sponsor
Sponsor
sponsor
Nick




PostPosted: Sat Jan 12, 2008 12:26 pm   Post subject: RE:Including via variables?

I don't belive so because according to memory imorpt has to be done before anything

then include has to be done right after that, thus creating a error if a varible has be declared before
StealthArcher




PostPosted: Sat Jan 12, 2008 12:27 pm   Post subject: RE:Including via variables?

Dang, now I'll have to figure out another way...
Clayton




PostPosted: Sat Jan 12, 2008 2:56 pm   Post subject: RE:Including via variables?

have you actually tried and tested some code? then you wouldn't have to ask this question.
StealthArcher




PostPosted: Sat Jan 12, 2008 3:02 pm   Post subject: RE:Including via variables?

I have tried, found nothing, then I asked.

Seeming this is as that, anyone know a way to execute a procedure off of user input, I haven't found any way to do it yet, except peicing apart the input letter by letter, and coding a thousand different cases for each possible letter combination...

Any ideas or am I stuck?
McKenzie




PostPosted: Sat Jan 12, 2008 3:22 pm   Post subject: Re: Including via variables?

I know what you are asking, there are a number of languages that allow this but Turing is not one of them. You are basically going to have to use big a case structure for all of your options.
StealthArcher




PostPosted: Sat Jan 12, 2008 3:41 pm   Post subject: RE:Including via variables?

Dang, Looks like I'll be porting my HBM Converter sooner than I wanted to...
ericfourfour




PostPosted: Sat Jan 12, 2008 6:58 pm   Post subject: Re: Including via variables?

This can be done in Turing but it is very static. The way I would do it might be advanced for the average Turing programmer. However, it greatly simplifies the process of adding more procedures.

First make a datatype that contains a procedure and a string representation (the name) of the procedure.

Turing:
type Procedure :
    record
        call : proc x
        name : string
    end record


The easiest thing to do from here would be to store this datatype in an array. You also need a function to search through the array with a specified string and find the matching name. This function will return the index in the array to simplify error checking (ie. 0=error, everything else is good):

Turing:
fcn strToProcIndex (str : string, procs : array 1 .. * of Procedure) : int
    for i : 1 .. upper (procs)
        if procs (i).name = str then
            result i
        end if
    end for
    result 0
end strToProcIndex


Next, a few simple test procedures will be needed. One will output "Hello World". Another will draw a random box. The third will allow the end-user to quit the program. A global variable will be needed for the latter because the procedures cannot take parameters.

Turing:
proc putHelloWorld
    put "Hello World"
end putHelloWorld

proc drawRandomBox
    Draw.FillBox (Rand.Int (1, maxx), Rand.Int (1, maxy), Rand.Int (1, maxx),
        Rand.Int (1, maxy), Rand.Int (1, maxcolour))
end drawRandomBox

var done : boolean := false

proc quitProgram
    done := true
end quitProgram


Now the basic structure is defined and set up. For the test section, an array will be required. This array will contain 3 elements, 1 for each procedure. If there were more procedures, there would be more elements in the array. To iniitialize the array, each element will be assigned a procedure and name of the procedure:

Turing:
var procs : array 1 .. 3 of Procedure
procs (1).call := putHelloWorld
procs (1).name := "putHelloWorld"
procs (2).call := drawRandomBox
procs (2).name := "drawRandomBox"
procs (3).call := quitProgram
procs (3).name := "quitProgram"


Now that the procedure array initialized, it's time to setup the user input. This will be looped so the end-user has the option to try more than once:

Turing:
var input : string

loop
    put "Enter a procedure name."
    get input


That is pretty simple. Next, the user input will be parsed. If you remember, the function declared at the top of the program takes a string and returns the index of the procedure with that name.

Turing:
    var procIndex : int := strToProcIndex (input, procs)


The end-user might have not entered the procedure name correctly. In this case, the function used will return 0 instead of an array index. This needs to be checked for. If the index returned is 0, an error message will be given. If it is not 0, the procedure at the returned index will be called:

Turing:
    if procIndex = 0 then
        put "Procedure does not exist."
    else
        procs (procIndex).call
    end if


Finally, an exit condition for the loop is needed. Otherwise the program would go on forever. During the test procedure declarations, a global variable called done was declared and initialized. The procedure named quitProgram changes the value of this variable to true. This works perfectly for the exit statement:

Turing:
    exit when done
end loop


This method simplifies calling procedures based on input greatly in a very static language like Turing. If this is too complex or you only are using one or two procedures, you should do what McKenzie suggested.



Procedure.t
 Description:
Source code

Download
 Filename:  Procedure.t
 Filesize:  1.11 KB
 Downloaded:  71 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
StealthArcher




PostPosted: Sat Jan 12, 2008 7:00 pm   Post subject: RE:Including via variables?

Wow, a bit of reading, but looks good. Thanks, that would've taken me a while to get, as I still am rather noobish with records. Thanks again, I'll mention you in the credits.
ericfourfour




PostPosted: Sat Jan 12, 2008 7:29 pm   Post subject: Re: Including via variables?

More information on records here. I don't remember seeing a tutorial specified on the procedure datatype but a simple google search on callback functions will provide some explanation. The Turing as a Functional Programming Language tutorial expands on using procedures as variables to allow functions as parameters. If you want to give credit, I think it would be better to cite this thread.
StealthArcher




PostPosted: Sat Jan 12, 2008 8:26 pm   Post subject: RE:Including via variables?

ATM, I'm just trying to see if there is a way to assign the call variable by a string, and have it call the procedure based on the string I write in or get. I don't want to have a predefined set of options, namely I want to be able to scan a directory for Compressor files (They'll be marked to assure no errors), then make a set of this, using the file's name as the procedure call.


Just in case I can't get it to work, what would be a lang. that can do this easily, I'm currently learning Python and C++....
OneOffDriveByPoster




PostPosted: Sat Jan 12, 2008 9:40 pm   Post subject: Re: RE:Including via variables?

StealthArcher @ Sat Jan 12, 2008 8:26 pm wrote:
namely I want to be able to scan a directory for Compressor files (They'll be marked to assure no errors), then make a set of this, using the file's name as the procedure call.
I am interested but confused. Exactly what do the procedures do? You mean, you will somehow have a procedure generated from the files?
StealthArcher




PostPosted: Sat Jan 12, 2008 10:16 pm   Post subject: Re: Including via variables?

In essence, something like other programs having plugins, I want my HBM converter to scan a directory at opening, checking for compressor files, which have a specific opening sequence, then allow the user later on to choose one to compress the file with, the user will input a string, so after the inclusion of all the files at the beginning, I need turing to be able to run a proc(in the compressor's file) named the same as the users input. All procs in com. files will be the same name as the file.
OneOffDriveByPoster




PostPosted: Sat Jan 12, 2008 10:31 pm   Post subject: Re: Including via variables?

I think you will get better luck with Python or C. Are these Compressor files supposed to be scripts/source code/executables?
For C, you will likely get stuck with OS-specific code though--and DLLs and the such.
StealthArcher




PostPosted: Sat Jan 12, 2008 10:34 pm   Post subject: Re: Including via variables?

AATM with turing, they are source code, however if I move to python they will be scripts.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 24 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: