Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite recursion in test programs #2

Open
bl0ckeduser opened this issue Jul 2, 2011 · 3 comments
Open

Infinite recursion in test programs #2

bl0ckeduser opened this issue Jul 2, 2011 · 3 comments

Comments

@bl0ckeduser
Copy link

Hi, this is to report that the following test programs --
reverseif.ns
namespace.ns,
have an infinite recursion problem.

For example, in "reverseif.ns":

&if &_if =
{ rot _if } &if = #The 'reversed if'.

"_if" and "if" point to the same
object, thus, the following:
{ rot _if } &if =
is self-referential, so its expansion
leads to infinite recursion.

The true problem seems to be
that the "if" object's contents don't
get copied to a new location proper
to "_if".

@nikki93
Copy link
Owner

nikki93 commented Jul 2, 2011

I noticed this too... It's because earlier &x pushed the function object onto stack, while now it just pushes a symbol and then 'follows' the symbol later, so no copying is involved.

One way to fix this is to also have another prefix, like 'x for example, that pushes a function object onto the stack... But I think this makes things messier.

What do you suggest?

@nikki93 nikki93 closed this as completed Jul 2, 2011
@nikki93 nikki93 reopened this Jul 2, 2011
@nikki93
Copy link
Owner

nikki93 commented Jul 2, 2011

(closed issue by mistake - reopened it)

@bl0ckeduser
Copy link
Author

I suggest that the interpreter handle this as a special case and make copies of function objects when function symbol assignment is encountered.

This is pretty easily coded, and as a proof-of-concept, the patch below, which implements the idea, seems to work pretty
well, as after applying it, all the test programs run correctly.

diff --git a/src/nsbuiltins.c b/src/nsbuiltins.c
index 46e0b13..80b0427 100644
--- a/src/nsbuiltins.c
+++ b/src/nsbuiltins.c
@@ -39,6 +39,23 @@ void ns_assign()

     struct ns_obj *p = ns_searchNamespace(ns_currNamespace, s.u.sym->arr);

+    if(NS_ISSYM(o))
+    {
+        struct ns_obj *q = ns_searchNamespace(ns_builtinsSpace, o.u.sym->arr);
+    
+        if(q->type == TY_FUNC)
+        {
+                if(p->type != TY_EMPTY)
+                {
+                        p->type = TY_FUNC;
+                        p->u.f = q->u.f;  
+                }
+                else
+                        ns_addToNamespace(ns_currNamespace, s.u.sym->arr, ns_makeFuncObj(q->u.f));
+                return;
+        }
+    }
+
     if (p->type != TY_EMPTY)
         *p = o;
     else

(Following this scheme, ns_parentAssign also has to be patched in a very similar way).

(EDIT: rephrased first paragraph for clarity)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants