A tiny but powerful interpreter that allows a user to code, save and run programs in BASIC.
- Required
COMMAND
in 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
keyPressEvent
function receives the line input by the user and trigger theprocessLine
function. - If this line begins with a number, it trigger
parseStatement
function and store it in the program. - Else, it trigger the
processCommand
function to process the command.
- First store the program, then use
parseStatement
function exported in theparser
class to parse this line. - According to the command, the
parseStatement
function new different Subclass ofStatement
. - Each constructor of
Statement
check 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 callsetParsed
function implemented byprogram.h
file to decide whelter to remove this line of code.
- According to the command, the
processCommand
function call different functions. listProgram
print the lines stored in program out on the window,clear
delete all the previous information andprintHelpMsg
print all the help information.run
has 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.
Expression
class create a Expression tree, each node is aIdentifierExp
class, aCompoundExp
class or aConstantExp
class.- the evalution method use recursive algorithm to return the value of a expression tree.
- the value of variables are stored in
evaluation
class.
- 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
waitSec
method. - Check the validity of the input, if it is invalid, repeat again.
- Store the function name, their starting line and end line in
evaluation
class. - When meet
CALL
command, 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; }