Ejemplo n.º 1
0
void processLine(string line, Program & program, EvalState & state) {
	TokenScanner scanner;
	scanner.ignoreWhitespace();
	scanner.scanNumbers();
	scanner.setInput(line);
    string str=scanner.nextToken();
    if (scanner.getTokenType(str)==NUMBER) {
		
        int lineNumber=stringToInteger(str);
        string token=scanner.nextToken();
        scanner.saveToken(token);
        if (token!="") {
            program.addSourceLine(lineNumber, line);
            program.setParsedStatement(lineNumber, parseStatement(scanner));
        }
        else {
            program.removeSourceLine(lineNumber);
        }
		
		
    }
	
    else if (str=="LIST") {
        for (int i=program.getFirstLineNumber(); i!=-1; i=program.getNextLineNumber(i)) {
            cout << program.getSourceLine(i)<<endl;
        }
    }
    else if (str=="CLEAR") {
        program.clear();
    }
    else if(str=="QUIT") exit(0);
    else if (str=="HELP") cout<<"This is a minimal BASIC interpreter."<<endl;
	
    else if (str=="RUN") {
        int currentLine=program.getFirstLineNumber();
        state.setCurrentLine(currentLine);
        while (currentLine!=-1) {                       
            program.getParsedStatement(currentLine)->execute(state);
			
            if(currentLine!=state.getCurrentLine()) {
                currentLine=state.getCurrentLine();
            }
            else {
                currentLine=program.getNextLineNumber(currentLine);
                state.setCurrentLine(currentLine);
            }
        }
    }
	
	
}
Ejemplo n.º 2
0
void processLine(string line, Program & program, EvalState & state) {
   TokenScanner scanner;
   scanner.ignoreWhitespace();
   scanner.scanNumbers();
   scanner.setInput(line);
//   Expression *exp = parseExp(scanner);
//   int value = exp->eval(state);
//   cout << value << endl;
//   delete exp;
   if (!scanner.hasMoreTokens()) {
       return;
   }
   string firstToken = toUpperCase(scanner.nextToken());
   if (firstToken == "RUN") {
       runProgram(program, state);
   }
   else if (firstToken == "QUIT") {
       exit(0);
   }
   else if (firstToken == "HELP") {
       dspHelp();
   }
   else if (firstToken == "CLEAR") {
       program.clear();
   }
   else if (firstToken == "LIST") {
       listCmd(program);
   }
   else if (firstToken == "INPUT" || firstToken == "PRINT" || firstToken == "LET") {
       scanner.saveToken(firstToken);
       Statement * stmt = parseStatement(scanner);
       stmt->execute(state);
       delete stmt;
   }
   else {
       int lineNumber = stringToInteger(firstToken);
       if (!scanner.hasMoreTokens()) {
           program.removeSourceLine(lineNumber);
       }
       else {
           int length = firstToken.length();
           string source = line.substr(length);
           program.addSourceLine(lineNumber, source);
           Statement * stmt = parseStatement(scanner);
           program.setParsedStatement(lineNumber, stmt);
       }
   }
}