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

Username:   Password: 
 RegisterRegister   
 "Prototype" of functions
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Ratinho




PostPosted: Mon Jun 07, 2010 5:03 pm   Post subject: "Prototype" of functions

What is it you are trying to achieve?
I want to call 2 functions: one from each other


What is the problem you are having?
The compiler doesnt "know" the function if it defined lower then the place i try to call it, and i cant defined two function mixed:S
its like one of the reason for function`s prototype in c/c/++


Describe what you have tried to solve this problem
i tried export and things like that...


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Plz dont suggest me other solutions...im trying to build compiler, and i need that to implement my grammer...

Turing:


function term () : boolean
    var retVal : boolean
    retVal := false
    put : outF, "<term>"
    if (integerConstant () or stringConstant () or keywordConstant ()) then
        nextTok ()
    elsif (varName ()) then
        nextTok ()
        if (cur_tok = '[') then
            nextTok ()
            if (expression ()) then
                nextTok ()
                if (cur_tok = ']') then
                    nextTok ()
                    retVal := true
                end if
            end if
        end if
    elsif (subroutineCall ()) then
        retVal := true
    elsif (cur_tok = '(') then
        nextTok ()
        if (expression ()) then
            nextTok ()
            if (cur_tok = ')') then
                nextTok ()
                retVal := true
            end if
        end if
    elsif (unaryOp ()) then
        nextTok ()
        if (term ()) then
            nextTok ()
            retVal := true
        end if
    else
        retVal := false
    end if
    put : outF, "</term>"
    result retVal
end term

function expression () : boolean
    put : outF, "<expression>"
    if ( ~term ()) then
        result false
    end if
    loop
        exit when ( ~ (op () and term ()))
    end loop
    put : outF, "</expression>"
    result true
end expression


Notice to the relationship between term() and expression()
Please specify what version of Turing you are using
the newest



Thank you very much guys Smile
Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: Mon Jun 07, 2010 5:20 pm   Post subject: RE:"Prototype" of functions

You can do this using the forward and body keywords like this:

Turing:
forward proc foo()

proc bar()
    foo()
end bar

bar()

body proc foo()
    put "Win!!!"
end foo
Cezna




PostPosted: Tue Jun 08, 2010 5:39 am   Post subject: RE:"Prototype" of functions

If you are OK using procedures (at least for one of the things), then use forward procedure for the declaration at the top, and body procedure for the actual procedure.

I don't think there's any way to do this with functions.

By the way, TerranceN, how do you have everything in the code formatted right.
Did you just do it manually by tagging everything a certain way?
TerranceN




PostPosted: Tue Jun 08, 2010 6:21 am   Post subject: Re: "Prototype" of functions

I know you can do this with functions, I have done it before. I'm on Ubuntu (linux, no Turing) at the moment so forgive me if my syntax is wrong:

Turing:

forward fcn GimmeAWin() : string

proc Main()
    put GimmieAWin()
end Main

Main()

body fcn GimmeAWin() : string
    result "Win!!!"
end GimmeAWin


As for the formatting use syntax tags
code:
[syntax="Turing"]
% your code here
[/syntax]
Cezna




PostPosted: Tue Jun 08, 2010 10:50 am   Post subject: RE:"Prototype" of functions

Ok, so you can do it with functions.
So Ratinho, all you have to do is put this at the top:
Turing:

forward function function_name


and this at the function

Turing:

body function function_name
<function code here>
end function_name


The forward function line acts as the declaration statement, so as long as this is above any calls to the function, there shouldn't be any problems.
Ratinho




PostPosted: Sun Jun 20, 2010 2:12 pm   Post subject: RE:"Prototype" of functions

hey guys tnx!
it was great solution (the forward and body Smile)
i used it in the day after u post it, sry that i reply only now =]
u r really useful guys!
Cezna




PostPosted: Sun Jun 20, 2010 6:58 pm   Post subject: RE:"Prototype" of functions

Maybe useful enough for +bits or +karma?
Smile
Monduman11




PostPosted: Sun Jun 20, 2010 7:24 pm   Post subject: Re: RE:"Prototype" of functions

Cezna @ Sun Jun 20, 2010 6:58 pm wrote:
Maybe useful enough for +bits or +karma?
Smile

lol quick question what are bits and karma used for anyways?
Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: Sun Jun 20, 2010 7:39 pm   Post subject: RE:"Prototype" of functions

umm... Feeling badass?

I don't think its used for anything.
USEC_OFFICER




PostPosted: Sun Jun 20, 2010 7:42 pm   Post subject: RE:"Prototype" of functions

It used for giving to people as a way of saying 'Great job' or 'You're awsome' or something like that.
Cezna




PostPosted: Sun Jun 20, 2010 7:45 pm   Post subject: Re: "Prototype" of functions

Go here to read about bits.
Karma is just to show how helpful/unhelpful a person is in general.
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 1  [ 11 Posts ]
Jump to:   


Style:  
Search: