|
| 1 | +#ifndef __PYTHONREADING_ENGINE_H_ |
| 2 | +#define __PYTHONREADING_ENGINE_H_ |
| 3 | + |
| 4 | +#include "object.h" |
| 5 | +#include "intObject.h" |
| 6 | +#include "strObject.h" |
| 7 | + |
| 8 | +#include <iostream> |
| 9 | +#include <string> |
| 10 | +#include <map> |
| 11 | + |
| 12 | +char* info = "********** Python Research **********\nInput 'exit' to exit\n"; |
| 13 | +char* prompt = ">>> "; |
| 14 | + |
| 15 | +class ExcuteEngine |
| 16 | +{ |
| 17 | +public: |
| 18 | + void Excute() |
| 19 | + { |
| 20 | + cout << info; |
| 21 | + cout << prompt; |
| 22 | + while(getline(cin, m_Command)) |
| 23 | + { |
| 24 | + if(m_Command.size() == 0) |
| 25 | + { |
| 26 | + cout << prompt; |
| 27 | + continue; |
| 28 | + } |
| 29 | + else if(m_Command == "exit") |
| 30 | + { |
| 31 | + return; |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + ExcuteCommand(m_Command); |
| 36 | + } |
| 37 | + cout << prompt; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | +private: |
| 42 | + void ExcuteCommand(string& command) |
| 43 | + { |
| 44 | + string::size_type pos = 0; |
| 45 | + if((pos = command.find("print ")) != string::npos) |
| 46 | + { |
| 47 | + ExcutePrint(command.substr(6)); |
| 48 | + } |
| 49 | + else if((pos = command.find(" = ")) != string::npos) |
| 50 | + { |
| 51 | + string target = command.substr(0, pos); |
| 52 | + string source = command.substr(pos+3); |
| 53 | + ExcuteAdd(target, source); |
| 54 | + } |
| 55 | + else { |
| 56 | + cout << "[Error] : Can not recognize : " << command << endl; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + void ExcutePrint(string symbol) |
| 61 | + { |
| 62 | + PyObject* object = GetObjectFromSymbol(symbol); |
| 63 | + if(object != NULL) |
| 64 | + { |
| 65 | + PyTypeObject* type = object->type; |
| 66 | + type->print(object); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + void ExcuteAdd(string& target, string& source) |
| 71 | + { |
| 72 | + string::size_type pos; |
| 73 | + if(IsSourceAllDigit(source)) |
| 74 | + { |
| 75 | + PyObject* intObject = CreatePyIntObject(atoi(source.c_str())); |
| 76 | + if(m_Symbol2Object.find(target) == m_Symbol2Object.end()) { |
| 77 | + m_Symbol2Object.insert(map<string, PyObject*>::value_type(target, intObject)); |
| 78 | + } |
| 79 | + else { |
| 80 | + m_Symbol2Object[target] = intObject; |
| 81 | + } |
| 82 | + } |
| 83 | + else if(source.find("\"") != string::npos || source.find("'") != string::npos) |
| 84 | + { |
| 85 | + PyObject* strObject = CreatePyStrObject(source.substr(1, source.size()-2).c_str()); |
| 86 | + if(m_Symbol2Object.find(target) == m_Symbol2Object.end()) { |
| 87 | + m_Symbol2Object.insert(map<string, PyObject*>::value_type(target, strObject)); |
| 88 | + } |
| 89 | + else { |
| 90 | + m_Symbol2Object[target] = strObject; |
| 91 | + } |
| 92 | + } |
| 93 | + else if((pos = source.find("+")) != string::npos) |
| 94 | + { |
| 95 | + PyObject* leftObject = GetObjectFromSymbol(Trim(source.substr(0, pos))); |
| 96 | + PyObject* rightObject = GetObjectFromSymbol(Trim(source.substr(pos+1))); |
| 97 | + if(leftObject != NULL && right != NULL && leftObject->type == rightObject->type) |
| 98 | + { |
| 99 | + PyObject* result = (leftObject->type)->add(leftObject, rightObject); |
| 100 | + m_Symbol2Object.insert(map<string, PyObject*>::value_type(target, result)); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + bool IsSourceAllDigit(string& source) |
| 106 | + { |
| 107 | + string::size_type len = source.size(); |
| 108 | + for(string::size_type i = 0; i < len; ++i) |
| 109 | + { |
| 110 | + if(!isdigit(source[i])) |
| 111 | + { |
| 112 | + return false; |
| 113 | + } |
| 114 | + } |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + PyObject* GetObjectFromSymbol(string symbol) |
| 119 | + { |
| 120 | + map<string, PyObject*>::iterator it = m_Symbol2Object.find(symbol); |
| 121 | + if(it == m_Symbol2Object.end()) |
| 122 | + { |
| 123 | + cout << "[Error] : " << symbol << " is not defined!!" << endl; |
| 124 | + return NULL; |
| 125 | + } |
| 126 | + return it->second; |
| 127 | + } |
| 128 | + |
| 129 | + string Trim(string symbol) { |
| 130 | + int length = symbol.length(); |
| 131 | + |
| 132 | + int start = 0; |
| 133 | + while(symbol[start] == ' ') { |
| 134 | + ++start; |
| 135 | + } |
| 136 | + |
| 137 | + int end = length-1; |
| 138 | + while(symbol[end] == ' ') { |
| 139 | + --end; |
| 140 | + } |
| 141 | + return symbol.substr(start, end+1); |
| 142 | + } |
| 143 | +private: |
| 144 | + string m_Command; |
| 145 | + map<string, PyObject*> m_Symbol2Object; |
| 146 | +}; |
| 147 | +#endif |
0 commit comments