Parallel Interpreter For "Treehugger" Programming Language Not Functioning As Intended

74 Views Asked by At

Treehugger is a variant of the infamous Brainf*** programming language that, instead of running on a tape, runs on a binary tree that is infinite downwards. For this question on the Programming Puzzles and Code Golf stack exchange, I'm attempting to write a Parallel interpreter for it that will eventually output every syntactically valid, halting program in the language, excluding those which require input and those that produce output.

For some reason, it also seems to omit all non-empty programs shorter than about 5 characters while also including some with blatantly invalid syntax. Of course, these are problems that need to be fixed before I can start golfing it.

Here's the source for it:

import java.util.*;

public class I{
    static class NODE{
        public NODE l=null,r=null;
        public byte v=0;
    }
    static class PROGRAM extends Stack<NODE>{
        public int i=0;
        public char[]s;
        public boolean h=false;
    }
    static void step(PROGRAM t){
        if(t.i>=t.s.length){
            t.h=true;return ;
        }
        char c=t.s[t.i];
        if(c=='<'){if(t.peek().l==null)t.peek().l=new NODE();t.push(t.peek().l);}
        if(c=='>'){if(t.peek().r==null)t.peek().r=new NODE();t.push(t.peek().r);}
        if(c=='^')t.pop();
        if(c=='+')t.peek().v++;
        if(c=='-')t.peek().v--;
        if(c=='['&&t.i==0){
            int i=1;
            while(i>0){
                t.i++;
                if(t.s[t.i]==']')i--;
                if(t.s[t.i]=='[')i++;
            }
            return;
        }
        if(c==']'&&t.i!=0){
            int i=1;
            while(i>0){
                t.i--;
                if(t.s[t.i]==']')i++;
                if(t.s[t.i]=='[')i--;
            }
            return ;
        }
        t.i++;
    }
    static char[]n(char[]a){
        String b="<^>+-[]";
        for(int i=a.length-1;i>=0;i--){
            int j=b.indexOf(a[i]);
            if(j<6){a[i]=b.charAt(j+1);return a;}
            a[i]='<';
        }
        char[]c=Arrays.copyOf(a,a.length+1);
        c[a.length]='<';
        return c;
    }
    public static void main(String[]a){
        List<PROGRAM>programs=new ArrayList<PROGRAM>();
        char[]c={};
        while(true){
            PROGRAM t=new PROGRAM();
            t.s=c;
            if(isBalanced(c))programs.add(t);
            c=n(c);
            for(PROGRAM u:programs){
                try{step(u);if(u.h){programs.remove(u);System.out.println(String.valueOf(u.s));break ;}}catch(Exception e){
                    programs.remove(u);break ;
                }
            }
        }
    }
    static boolean isBalanced(char[]c) {
        int i=0;
        for(char d:c){
            if(d=='[')i++;
            if(d==']')i--;
            if(i<0)return false;
        }
        return i==0;
    }
}

So where exactly is the problem in this? I can't seem to find it, but, based on the output, it's obviously there somewhere.

Edit: I was able to find the problem on my own. What should I do with this question now?

0

There are 0 best solutions below