0

This is not about BrainF*ck. Please refer to the last section for the "ultimate" question.

I am terribly sorry, if that has been asked before, but I just couldn't find a page where this has been asked before.

I basically have a java project set up, that let's you write BrainF*ck code and then execute it. For those who don't know, BrainF*ck is a very simple programming language that only uses one-character commands.

I have set it up to where all the implemented letters have individual classes that extend to a Token superclass.

public abstract class Token {

    private final char token;

    public Token(char c) {
        this.token = c;
    }

    public final char getToken() {
        return token;
    }

    public abstract void tokenCalled();
}

So as an example, for the Token '+' I would have the class set up like this.

public class PlusToken extends Token {

    public PlusToken() {
        super('+');
    }

    @Override
    public void tokenCalled() {
        //increment value by 1
    }
}

This works all fabulously well.

Now for my project, I wanted the user to be able to create his own classes and put them in a pre-existent folder where my program will loop through the classes and include those Tokens into my projects. I have an arraylist set up that contains all the Tokens, so the only problem I'm having is: How do I read those classes, check if they are instances of Token and save those in my arraylist to use them?

felix fritz
  • 462
  • 2
  • 5
  • 21
  • "BrainF*ck is a very simple programming language". Oh yeah, totally. To your question: are you saying you want users to write their own classes? That does not seem wise. What is it exactly you want to do? – Jeroen Vannevel Nov 30 '13 at 03:15
  • @JeroenVannevel BrainF*ck is not easy, but simple. Can you put it like that? - I just wanted to give the user some freedom to be able to produce their own Tokens that they could use. (maybe a token '*' to play a "ding" sound, I don't know) – felix fritz Nov 30 '13 at 03:21
  • Have you considered an `enum`? Your tokens are actually just characters. And this language is considered "trivial", although interesting because it's turning complete when implemented correctly. – Elliott Frisch Nov 30 '13 at 03:22
  • @ElliottFrisch Thanks for your suggestion! I wanted to use "normal" classes in that case, because at the end of the day, there should be folder with some classes that the user created on his own that can be dynamically put into my project and removed again. – felix fritz Nov 30 '13 at 03:28
  • You need a mechanism to determine if your token is one of `+, -, <, >, ,, ., [ or ]`. I really don't understand where you're having a problem? Reading input files of text? – Elliott Frisch Nov 30 '13 at 03:32
  • @ElliottFrisch My interpreter works perfectally fine. I think I've set up this question wrong. Let's just say it's not about BrainF*ck. There is some external class that the user has created. The user tells me about this class and says it should be put into the program and executed. How can I just use this class, the user has given me, and execute a certain method? – felix fritz Nov 30 '13 at 03:37
  • How do you mean exactly? Add it to your classpath, instantiate it, and call the method. What are you missing? – Elliott Frisch Nov 30 '13 at 03:41
  • ´List´ <--- There are some classes in an external path that could be added to that list. The user created those classes. I just don't know, how to read those classes / load them in, check for their super-class and then add those classes to the list. – felix fritz Nov 30 '13 at 03:47

1 Answers1

0

This is about Java's reflection. Keywords for search: "java reflect all subclasses", and you will find a lot.

How do you find all subclasses of a given class in Java?

The "ClassPathScanningCandidateComponentProvider" answer may meet your need.

But in my opinion, there are simpler word-arounds. Something like following will simplely do the job:

public abstract class Token {

    public static HashMap<Character, Class<Token>> all = new HashMap<Character, Class<Token>>();

    private final char token;

    public Token(char c) {
        this.token = c;
        all.put(c, this.getClass());
    }

    public final char getToken() {
        return token;
    }

    public abstract void tokenCalled();
}
Community
  • 1
  • 1
kuroz
  • 287
  • 2
  • 9