コード例 #1
0
void IfStmt::execute(EvalState & state) {
    if(op == "=") {
        if (lhsExp->eval(state) == rhsExp->eval(state)) {
            state.setCurrentLine(lineNumber);
        }
    } else if (op == ">") {
        if(lhsExp->eval(state) > rhsExp->eval(state))
            state.setCurrentLine(lineNumber);
    } else {
        if(lhsExp->eval(state) < rhsExp->eval(state))
            state.setCurrentLine(lineNumber);
    }
}
コード例 #2
0
ファイル: Basic.cpp プロジェクト: sjmjulia/basic
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);
            }
        }
    }
	
	
}
コード例 #3
0
ファイル: statement.cpp プロジェクト: mariolew/CS106B
void IfStmt::execute(EvalState &state) {
    if (op == "=") {
        if (lhs->eval(state) == rhs->eval(state))
            state.setCurrentLine(nextLine);
    }
    else if (op == ">") {
        if (lhs->eval(state) > rhs->eval(state))
            state.setCurrentLine(nextLine);
    }
    else if (op == "<") {
        if (lhs->eval(state) < rhs->eval(state))
            state.setCurrentLine(nextLine);
    }
}
コード例 #4
0
ファイル: Basic.cpp プロジェクト: mariolew/CS106B
void runProgram(Program & program, EvalState & state) {
    int lineNumber = program.getFirstLineNumber();
    while (lineNumber != -1) {
        Statement * stmt = program.getParsedStatement(lineNumber);
        state.setCurrentLine(program.getNextLineNumber(lineNumber));
        stmt->execute(state);
        lineNumber = state.getCurrentLine();
    }
}
コード例 #5
0
void EndStmt::execute(EvalState & state) {
    state.setCurrentLine(-1);
}
コード例 #6
0
void GotoStmt::execute(EvalState & state) {
    state.setCurrentLine(line);
}
コード例 #7
0
ファイル: Basic.cpp プロジェクト: jwhurt/Projects
/*
 * Function: processLine
 * Usage: processLine(line, program, state);
 * -----------------------------------------
 * Processes a single line entered by the user.  In this version,
 * the implementation does exactly what the interpreter program
 * does in Chapter 19: read a line, parse it as an expression,
 * and then print the result.  In your implementation, you will
 * need to replace this method with one that can respond correctly
 * when the user enters a program line (which begins with a number)
 * or one of the BASIC commands, such as LIST or RUN.
 */
void processLine(string line, Program & program, EvalState & state) {
   TokenScanner scanner;
   scanner.ignoreWhitespace();
   scanner.scanNumbers();
   scanner.setInput(line);
   string token=scanner.nextToken();
   TokenType type=scanner.getTokenType(token);
   int lineNumb;
   int priorLineNumb;
   if(type==NUMBER){
     if(scanner.hasMoreTokens()){
       program.addSourceLine(stringToInteger(token),line);
       Statement *stmt=parseStatement(scanner); 
       program.setParsedStatement(stringToInteger(token),stmt);  //link statement to line number
     }
     else
       program.removeSourceLine(stringToInteger(token));
   }
   if(type==WORD){
     if(token=="LIST"){
       lineNumb=program.getFirstLineNumber();
       while(lineNumb!=-1){
	 cout<<program.getSourceLine(lineNumb)<<endl;
	 lineNumb=program.getNextLineNumber(lineNumb);
       }
     }
     else if(token=="RUN"){
       state.setCurrentLine(program.getFirstLineNumber());
       lineNumb=state.getCurrentLine();
       while(lineNumb!=-1){	 
	 if(program.hasLineNumber(lineNumb)==false)
	   error("Invalid line reference at program line "+integerToString(priorLineNumb));
	 Statement *stmt=program.getParsedStatement(lineNumb);
	 state.setCurrentLine(program.getNextLineNumber(lineNumb));
	 stmt->execute(state);
	 priorLineNumb=lineNumb;
	 lineNumb=state.getCurrentLine();
       }
     }
     else if(token=="CLEAR"){
       program.clear();
     }
     else if(token=="HELP"){
       cout<<"Minimal Basic Statements"<<endl; 
       cout<<"REM   This statement used for comments"<<endl;
       cout<<"LET   This statement is BASIC's assigment statement"<<endl;
       cout<<"PRINT In minimal BASIC, the PRINT statement has the form: "<<endl;
       cout<<"         PRINT exp"<<endl;
       cout<<"      where exp is an expression."<<endl;
       cout<<"INPUT In the minimal version of the BASIC interpreter, the INPUT statement has the form:"<<endl;
       cout<<"         INPUT var"<<endl;
       cout<<"      where var is a variable read in from the user"<<endl;
       cout<<"GOTO  This statement has the syntax"<<endl;
       cout<<"         GOTO n"<<endl;
       cout<<"      which forces the program to continue from line n instead of continuing with the next statement"<<endl;
       cout<<"IF    This statement provides conditional control. The syntax for this statement is: "<<endl;
       cout<<"      IF exp1 op exp2 THEN n"<<endl;
       cout<<"      where exp1 and exp2 are expressions and op is one of the conditional operators =, <, or >."<<endl;
       cout<<"END   This statment marks the end of the program."<<endl;
       cout<<"Commands to control the BASIC interpreter"<<endl;
       cout<<"RUN   This command starts program execution beginning at the lowest-numbered line. "<<endl;
       cout<<"LIST  This command lists the steps in the program in numerical sequence."<<endl;
       cout<<"CLEAR This command deletes the program so the user can start entering a new one."<<endl;
       cout<<"HELP  This command provides a simple help message describing the interpreter."<<endl;
       cout<<"QUIT  Typing QUIT exits from the BASIC interpreter by calling exit(0)."<<endl;	 
     }
     else if(token=="QUIT"){
       exit(0);
     }
     else
       cout<<"Invalid command"<<endl;     
   }
}