Thread: Can it be detected if stack space is about to expire?

  1. #46
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by King Mir View Post
    I'd say that's fine, especially if you might pop different amounts of data during error recovery.

    Though you should probably have (at least) two stacks, one for tokens, and one for function call data. I get the impression that you don't.
    No I haven't.. because that'd make searching for identifiers *much* more cumbersome.(And managing MACROs would be difficult).
    (What I do now..is to just go back in the scope sequentially until the required variable is found(..or not))
    And..as lisp is a functional language.. everything is list evaluation! ...So..no ideological difference between the two.

    (Btw.. I do have separate stacks for looking up macro and function names. )

  2. #47
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    So how do you implement a function call? Especially a function call that takes a function?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #48
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by King Mir View Post
    So how do you implement a function call?
    I posted the operator()(...) member function of the function class a few posts ago. That is ...AFAI'm concerned, the function call.
    If you mean list evaluation, here it is :
    Code:
    std::string list::eval()
    {
        if(size_val==0)return "nil"; //When an empty list is received
    
        std::string name = car(),temp;
    
        std::istringstream is1(name);
        is1>>temp;
        if(temp=="(")
            name = list(name).eval(); //If the name itself is a list
    
        list args = cdr();
        std::istringstream is(name);
        double d;
    
        auto v = var_scope.find(name); 
        if(v!=nullptr)return *v; //If the list contains a single variable (This isn't acceptable in the formal lisps)
    
        auto f = f_scope.find(name); //f_scope relates the function names to the function objects
        if(f!=nullptr)
        {
            for(auto x=args.begin();x!=args.end();x++) //this loop 'simplifies' the arguments to a function...recursively.
            {
                std::string val = *x,temp;
                std::istringstream is2(val);
                is2>>temp;
                if(temp=="(")
                {
                    list l(val);
                    *x = l.eval();
                }
            }
            return (*f)(args);
        }
        auto m = m_scope.find(name); //Macros
        if(m!=nullptr)return (*m)(args);
    
        else if(is>>d)return name; //If the list contains a single number..
    
        else throw(exception(name+" : name not found."));
    }

    Especially a function call that takes a function?
    As an argument ? (...which would be analogous to taking functors or function pointers ?)
    I did not do it yet, as it should depend upon lambda functions and some associated macros which I'm yet to implement.
    Last edited by manasij7479; 12-03-2011 at 01:19 PM.

  4. #49
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Oh right, cause your treating interpreted functions as actual functions. Which is bad for recursion, because you are limited by the stack. I recommend not doing that.

    Wherever you use recursion use a loop: push the arguments to the called function on the stack, and start eval again. Recursive compiled functions should work the same way, unless you can implement them using loops more directly.
    Last edited by King Mir; 12-03-2011 at 02:44 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #50
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by King Mir View Post
    Which is bad for recursion, because you are limited by the stack.
    I thought recursion is by definition, limited by the stack...(?)

    Wherever you use recursion use a loop: push the arguments to the called function on the stack, and start eval again. Recursive compiled functions should work the same way, unless you can implement them using loops more directly.
    I'll have to give this some serious thought...as there is a possibility that this model can not be implemented on what I have currently.

  6. #51
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Recursion is limited by the program stack unless you don't implement it as actual recursion. Tail recursion can be implemented as a loop with no stack, and other recursive cases can be implemented with a loop and separate stack as I described above.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Smashing Detected
    By halexh in forum C Programming
    Replies: 13
    Last Post: 05-19-2010, 02:10 AM
  2. *** stack smashing detected ***
    By chakra in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 09:12 PM
  3. *** stack smashing detected ***
    By Martin_HS in forum C Programming
    Replies: 9
    Last Post: 05-29-2009, 04:01 AM
  4. help on *** stack smashing detected ***
    By jodelson in forum C Programming
    Replies: 8
    Last Post: 08-16-2007, 06:25 PM
  5. Managing Stack space?
    By Aidman in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2003, 02:29 PM