A tiny but powerful interpreter that allows a user to code, save and run programs in BASIC.
- Required
COMMANDin the document and add support for function. - Immediately print the comstant expression and expression only contain constant number and defined varivables.
HINT: Each token should split by a blankspace. -1 can either prepresent as -1 or - 1. support expression like 1 * - 2
- SYNTAX ERROR will be printed on the console during writing program. RUNTIME ERROR will be printed and stop the running at once.
- basic: Export a QTextEdit class to implement a console to interact with the user. Process the command and print information.
- tokenizer: Provide support to scan strings and divide it into a list of tokens.
- parser: Provide support to scan statements (create different statements according to the command scaned) and to scan expressions (create a binary expression tree according to the tokens scanned).
- statement: Export class Statement and its concrete subclasses.
- expression: Export class Expression and its three concrete subclasses: ConstantExp, IdentifierExp, CompoundExp.
- evaluation: Export a class called Evaluation. A space storing all additional information required by the evaluator(e.g. the declared functions and variables). Act like %rip, we use it to change the execution order of programs.
- The
keyPressEventfunction receives the line input by the user and trigger theprocessLinefunction. - If this line begins with a number, it trigger
parseStatementfunction and store it in the program. - Else, it trigger the
processCommandfunction to process the command.
- First store the program, then use
parseStatementfunction exported in theparserclass to parse this line. - According to the command, the
parseStatementfunction new different Subclass ofStatement. - Each constructor of
Statementcheck the expression of the program and stored the information (like the variable name, the GOTO line number) it needed when being executed. If the expression is valid, it callsetParsedfunction implemented byprogram.hfile to decide whelter to remove this line of code.
- According to the command, the
processCommandfunction call different functions. listProgramprint the lines stored in program out on the window,cleardelete all the previous information andprintHelpMsgprint all the help information.runhas the following structures:int index = first line number of this program while(index != -1){ execute the statment if (eval->getNextLineNum() != -1){ index = eval->getNextLineNum(); }else { index = program->getNextLineNum(index); } }
  eval is used to change the line of the program to be executed, which supports the GOTO and IF THEN command.
Expressionclass create a Expression tree, each node is aIdentifierExpclass, aCompoundExpclass or aConstantExpclass.- the evalution method use recursive algorithm to return the value of a expression tree.
- the value of variables are stored in
evaluationclass.
- Use RUNNING and UNRUNNING flag in console to mark whelter the program is running. When running, if get a value inputted, store it.
- Check whelter the value is inputted every 0.1s by implementing
waitSecmethod. - Check the validity of the input, if it is invalid, repeat again.
- Store the function name, their starting line and end line in
evaluationclass. - When meet
CALLcommand, push the return line in the stack. When reach the end of the function again, pop the line and set next line to it.
-
process SYNTAX ERROR
try{ processLine(Line); }catch (const QString error) { this->write(error); } -
process RUNTIME ERROR
try{ getParsedStatement(index)->execute(eval); }catch(int){ break; }catch(const QString error) { this->write(error); break; }