📄 interpreter.cpp
字号:
/* * * * */#include "interpreter.h"#include "exception.h"#include "builtin.h"#include "math.h"#include "logic.h"#include "advmath.h"using namespace std;//////void DisplayHelp(){ BasicMathHelp(); TrigHelp(); AdvancedMathHelp(); LogicalHelp(); ConstantsHelp(); StackHelp(); FormatHelp(); cout << endl;}//////Interpreter::Interpreter(){ program = NULL; SetPrompt("<sc> "); // Load operations LoadBuiltin(oplist); LoadMath(oplist); LoadLogic(oplist); LoadAdvMath(oplist);}//////Interpreter::~Interpreter(){ if (program != NULL) delete program;}//////string Interpreter::GetPrompt(){ return (varlist.Retrieve("_prompt")->ToString());}//////void Interpreter::SetPrompt(string p){ varlist.Store(new String(p), "_prompt");}//////bool Interpreter::Execute(string cmd){ int i, p, q; bool res = false, inQuotes=false; p = -1; for (i=0;i<int(cmd.length());i++) { if ((cmd[i] == ' ') && (!inQuotes)) { q = i-1; if (p > -1) res = (res || ExecuteToken(cmd.substr(p, (q-p)+1))); p = -1; } else { if (cmd[i] == '\"') inQuotes = !inQuotes; if (p == -1) p = i; } } if (p > -1) { q = i-1; res = (res || ExecuteToken(cmd.substr(p, (q-p)+1))); } return res;}//////Item *Interpreter::ParseToken(string str){ Item *p; // Check if string if (str[0] == '\"') return new String(str.substr(1, str.length()-2)); // Check the list of operations for a match for (p=oplist.First();p!=NULL;p=p->next) if (str == p->ToString()) return p->Clone(); // Check if numeric, Old C Black Magic for converting string to numeric value const char *cp; char *cq = NULL; double x = strtod(cp = str.c_str(), &cq); if (cq != cp) { long lv = long(x); if (double(lv) == x) return new Integer(lv); else return new Real(x); } return NULL; // The parser has been defeated :)}//////bool Interpreter::ExecuteToken(string cmd){ bool isExit = false; Item *p = ParseToken(cmd); if (p == NULL) throw new UnknownOperation(cmd); if (p->GetType() == OPERATION) { Operation *op = dynamic_cast<Operation *>(p); if (op->code == UNBRACE) { Enter(program); program = NULL; } if (program != NULL) program->Add(p); else Execute(*op); if (op->code == BRACE) program = new ItemList(); if (op->code == EXIT) return isExit = true; } else { if (program != NULL) program->Add(p); else Enter(p); } return isExit;}//////double Interpreter::GetResult(){ return stack.PopDouble();}void Interpreter::DisplayStack(int levels){ stack.Display(levels);}void Interpreter::Execute(Operation p){ switch(p.code) { case HELP: DisplayHelp(); break; default: Calculator::Execute(p); break; };}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -