import java.io.*;
import java.util.Scanner;
import java.util.regex.*;

public class Editor
{
  private LL<TextLine> theText;
  private String prompt;
  Pattern pattern;
  Variables variables = new Variables();
  boolean immediate;
  
  public Editor(String prompt)
  {
    this.theText = new LL<TextLine>();
    this.prompt = prompt;
    this.immediate = true;
  }

  public void prompt()
  {
    System.out.print(this.prompt);
  }

  private void mainLoop(String prompt)
  {
    String commands[] = {"save", "read", "quit", "resequence", "list", "print", "exit", "let", "run", "stop"};
    Scanner kb = new Scanner(System.in);
    String line;
    boolean done = false;
    boolean isCommand = false;
    int lineNum = 0;

    while(!done)
    {
      if(immediate)
      {
        this.prompt();
        line = kb.nextLine();
      }
      else
      {
        if(theText.hasNext())
        {
          TextLine tl = theText.getNext();
          line = tl.getContents();
        }
        else
        {
          theText.reset();
          immediate = true;
          continue;
        }
      }  
      pattern = Pattern.compile("^\\w*\\d+");
      Matcher matcher = pattern.matcher(line);
      if(matcher.find())
      {
        isCommand = false;
      }
      else
      {
        isCommand = true;
      }
      if(isCommand)
      {
        int command = findCommand(commands, line);
        dispatch(command, line);
      }
      else
      {
        lineNum = Integer.parseInt(line.substring(0, matcher.end()));
        line = line.substring(matcher.end());
        TextLine tl =new TextLine(lineNum, line);
        theText.insertSorted(tl);
      }
    }
  }

  private void dispatch(int i, String line)
  {
    switch(i)
    {
case 0: //save
    SubLine sl = nextWord(line, 0); 
    sl = nextWord(line, sl.end);
    String filename = sl.word;
    PrintWriter pw = null;
    try
    {
      pw = new PrintWriter(new File(filename)); 
      pw.print(theText.toString());
    }
    catch (FileNotFoundException e)
    {
      System.out.println("Save failed.");
    }
    pw.close();
    break;
case 1: //read
/*
    SubLine sl = nextWord(line, 0); 
    sl = nextWord(line, sl.end);
    String filename = sl.word;
    try
    {
      Scanner in = new Scanner(new File(filename));
    }
    catch (FileNotFoundException e)
    {
      System.out.println(filename + " not found.");
      continue;
    }
    while(in.hasNext())
    {
      TextLine tl =new TextLine(    
*/
    break;
case 2:
case 6:
    System.exit(0);
    break;
case 3:
    System.out.println("Resequence");
    break;
case 4:
    System.out.println(this.theText);
    break;
case 5:
    sl = nextWord(line, 0);
    String exp = line.substring(sl.end);
    System.out.println("" + Expressions2.stringToQueue(exp, variables));
    break;
case 7:
    sl = nextWord(line, 0);
    SubLine varName = nextWord(line, sl.end);
    int where = varName.end;
    while(where < line.length() && line.charAt(where) != '=')
      where++;
    where++;
    if(where >= line.length())
      throw new MalformedExpressionException("No = after let command");
    exp = line.substring(where);
    variables.setValue(varName.word, Expressions2.stringToQueue(exp, variables));
    break;
case 8: //run
System.out.println("Run command issued");
    immediate = false;
    break;
case 9: //stop
    immediate = true;
    theText.reset();
    break;
default:
    System.out.println("Undefined command");
    }
  }

  private static int findCommand(String c[], String line)
  {
    int retval = -1;
    SubLine sl  = nextWord(line, 0);    
    String firstWord = sl.word;
    for(int k = 0; k < c.length; k++)
    {
      if(c[k].equals(firstWord))
      {
        retval = k;
        break;
      }
    }
    return retval;
  }

  private static  SubLine nextWord(String line, int start)
  {
    int i = start;
    while(i < line.length() && line.charAt(i) == ' ')
    {
      i++;
    }
    int j = i;
    while(j < line.length() && line.charAt(j) != ' ')
    {
      j++;
    }
    return new SubLine(line.substring(i, j), i, j);
  }

  public static void main(String args[])
  {
    Editor myEditor = new Editor(">");
    myEditor.mainLoop(">");
  }
}  
      
class SubLine
{
  public int start, end;
  public String word;

  public SubLine(String w, int start, int end)
  {
    this.word = w;
    this.start = start;
    this.end = end;
  }
}
