package scripting;

import simulator.*;

import java.io.*;
import java.util.*;

/**
   Basically, this is just to be able to create a set of modules flexibly
   The scripts are formatted thus:
   1. comments are standard C/C++/Java comments
   2. whitespace is ignored
   3. each command is a (fully specified) module name
   4. We'll get back to you on arguments and dividers
*/

public class ScriptProcFactory implements ProcessorFactory {

  public ScriptProcFactory(File f) {
    init(f);
  }
  public ScriptProcFactory(String s) {
    init(s);
  }

  public Processor MakeProcessor(Simulator s, double x, double y) {
    try {
      PortedProcessor pp = new PortedProcessor(s);
      Iterator i = modules.iterator();
      while(i.hasNext()) {
        ProcessorModule pm = (ProcessorModule)((Class)i.next()).newInstance();
        pp.addModule(pm);
      }
      return pp;
    } catch(Exception e) {
      System.out.println("Big failure! "+e);
      return null;
    }
  }

  Vector modules=new Vector(); // list of classes to be instantiated
  void init(File f) {
    try {
      FileReader fr = new FileReader(f);
      StreamTokenizer tok = new StreamTokenizer(fr); 
      tok.slashSlashComments(true); tok.slashStarComments(true);
      while(tok.nextToken()!=StreamTokenizer.TT_EOF) {
        modules.add(Class.forName(tok.sval));
      }
    } catch(Exception e) {
      System.out.println("Big failure!"+e);
    }
  }
  void init(String s) {
    try {
      StringTokenizer tok = new StringTokenizer(s); 
      while(tok.hasMoreTokens()) {
        modules.add(Class.forName(tok.nextToken()));
      }
    } catch(Exception e) {
      System.out.println("Big failure!"+e);
    }
  }
}
