#include
#include
#define Py_INCREF(obj) obj->ob_refcnt++
#define Py_DECREF(obj) obj->ob_refcnt--;
#define Py_XINCREF(obj) {if obj==NULL ; \
else Py_INCREF(obj)}
#define Py_XDECREF(obj) {if obj==NULL ; \
else Py_DECREF(obj)}
#define Trim(str) { string tmp=str.substr(str.find_first_not_of(" "),\
str.find_last_not_of(" ")+1-str.find_first_not_of(" ")); str=tmp; }
struct _type_object;
typedef struct _object{
int ob_refcnt;
struct _type_object* ob_type;
}py_object;
typedef void (*print_object)(py_object* obj);
void print_int(py_object *obj);
void print_string(py_object *obj);
typedef struct _intobject{
int ob_refcnt;
struct _type_object* ob_type;
int ob_ival;
}py_intobject;
typedef struct _stringobject{
int ob_refcnt;
struct _type_object* ob_type;
int ob_size;
char ob_str[50];
}py_stringobject;
typedef struct _type_object{
int ob_refcnt;
struct _type_object * ob_type;
int ob_size;
// char ob_name[20];
string ob_name;
print_object ob_print;
int ob_basesize;
int ob_itemsize;
}py_typeobject;
py_typeobject type_type={
1,
&type_type,
0,
"type_type",
NULL,
0,
0
};
py_typeobject py_int_typeobject={
1,
&type_type,
0,
"int_type",
print_int,
sizeof(py_intobject),
0
};
py_typeobject py_string_typeobject={
1,
&type_type,
0,
"string_type",
print_string,
sizeof(py_stringobject),
1
};
map global_map;
py_object* allocate();
void dealloc(py_object *obj);
py_intobject* create_intobject(int val);
py_stringobject* create_stringobject(string str);
void dealloc_int(py_intobject *obj);
void dealloc_string(py_stringobject *obj);
py_object* excute_command(string str);
void excute(string line);
py_object* excute_command(string str);
bool all_isalpha(string str);
bool all_isdigit(string str);
void python_env();
void dealloc_pyton_env();
///////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
py_intobject* create_intobject(int val)
{
py_intobject *p;
p=new py_intobject;
if (!p)
{
fprintf(stderr,"内存分配出错\n");
exit(1);
}
p->ob_refcnt=1;
p->ob_type=&py_int_typeobject;
p->ob_ival=val;
return p;
}
py_stringobject* create_stringobject(const char *str)
{
py_stringobject *p;
p=new py_stringobject;
if (!p)
{
fprintf(stderr,"内存分配出错\n");
exit(1);
}
p->ob_refcnt=1;
p->ob_type=&py_string_typeobject;
p->ob_size=strlen(str);
strncpy(p->ob_str,str,p->ob_size);
p->ob_str[p->ob_size]='\0';
return p;
}
void dealloc(py_object *obj)
{
delete obj;
}
void dealloc_int(py_intobject *obj)
{
delete obj;
}
void dealloc_string(py_stringobject *obj)
{
delete obj;
}
void print_int(py_object *obj)
{
py_intobject *p;
p=(py_intobject*)obj;
cout<ob_ival;
}
void print_string(py_object *obj)
{
py_stringobject *p;
p=(py_stringobject *)obj;
cout<ob_str;
}
void excute(string line)
{
string rusult;
string right_str;
string str;
string str_blank;
string::size_type pos;
if ( (pos=line.find("=")) != string::npos )
{
string left(line,0,pos);
string right(line,pos+1);
Trim(left);
Trim(right);
py_object* left_obj;
py_object* right_obj;
size_t count=global_map.count(left);
if (count != 0)
{
py_object *obj;
obj=global_map[left];
if (--obj->ob_refcnt == 0)
delete obj;
}
right_obj= excute_command(right);
if ( right_obj != NULL)
global_map[left]=right_obj;
}
else if ( (pos=line.find("print")) != string::npos )
{
if (line.size()==5)
{
cout<
return ;
}
string right(line,pos+6);
Trim(right);
string::size_type position;
if ( (position=right.find("+")) != string::npos )
{
py_object *result = excute_command(right);
if (result != NULL)
{
result->ob_type->ob_print(result);
cout<
}
else
return ;
}
else
{
if ( global_map.count(right) == 0 )
{
cout<
return ;
}
else
{
py_object *obj;
obj=global_map[right];
if (obj != NULL)
{
obj->ob_type->ob_print(obj);
cout<
}
}
}
}
}
py_object* excute_command(string str)
{
string::size_type pos;
string::size_type pos_begin;
string::size_type pos_end;
if ( (pos=str.find("+")) == string::npos &&
(pos_begin = str.find("\"")) != string::npos )
{
if ( (pos_end=str.substr(pos_begin+1).find("\"")) != string::npos )
{
string tmp_str(str,pos_begin+1,pos_end);
return (py_object*)create_stringobject(tmp_str.c_str());
}
else
{
cout<<"SyntaxError: invalid syntax"<
return NULL;
}
}
else if ( all_isdigit(str) )
return (py_object*)create_intobject(atoi(str.c_str()));
else if ( (pos=str.find("+")) != string::npos)
{
string::size_type pos_tmp;
string left(str,0,pos);
string right(str,pos+1);
Trim(left);
Trim(right);
//////左右两边都是数字
if ( all_isdigit(left) && all_isdigit(right) )
{
int left_digit;
int right_digit;
int sum;
left_digit=atoi(left.c_str());
right_digit=atoi(right.c_str());
sum = left_digit + right_digit;
return (py_object*)create_intobject(sum);
}
///////左边是数字右边是符合
else if ( all_isdigit(left) && global_map.count(right)!= 0 )
{
py_intobject* obj;
obj=(py_intobject*)global_map[right];
if ( obj->ob_type->ob_name==string("int_type") )
{
int left_digit;
int sum;
left_digit=atoi(left.c_str());
sum = left_digit + obj->ob_ival;
return (py_object*)create_intobject(sum);
}
else
{
cout<<"SyntaxError: invalid syntax"<
return NULL;
}
}
////////左边是符合右边是数字
else if ( all_isdigit(right) && global_map.count(left)!= 0 )
{
py_intobject* obj;
obj=(py_intobject*)global_map[left];
if ( obj->ob_type->ob_name==string("int_type") )
{
int right_digit;
int sum;
right_digit=atoi(right.c_str());
sum = right_digit + obj->ob_ival;
return (py_object*)create_intobject(sum);
}
else
{
cout<<"SyntaxError: invalid syntax"<
return NULL;
}
}
/////////左右两边都是符号,都是整数或都是字符串
else if ( global_map.count(left)!=0 && global_map.count(right)!=0 )
{
py_object *left_obj;
py_object *right_obj;
left_obj=global_map[left];
right_obj=global_map[right];
if ( left_obj->ob_type->ob_name==string("int_type") &&
left_obj->ob_type->ob_name==string("int_type") )
{
py_intobject *result;
int sum;
sum = ((py_intobject*)left_obj)->ob_ival+((py_intobject*)right_obj)->ob_ival;
result=create_intobject(sum);
return (py_object*)result;
}
else if (left_obj->ob_type->ob_name==string("string_type") &&
left_obj->ob_type->ob_name==string("string_type") )
{
py_stringobject *result;
string sum;
sum = string(((py_stringobject*)left_obj)->ob_str)+string(((py_stringobject*)right_obj)->ob_str);
result=create_stringobject(sum.c_str());
return (py_object*)result;
}
}
string::size_type pos_begin_left;
string::size_type pos_end_left;
string::size_type pos_begin_right;
string::size_type pos_end_right;
////////左边是字符串
if ( (pos_begin_left=left.find("\"")) != string::npos )
{
// cout<<"in 1"<
if ( (pos_end_left=left.substr(pos_begin_left+1).find("\""))==string::npos )
{
cout<<"left.substr(pos_begin_left+1):"<
cout<<"left.substr(pos_begin_left+1).find(转义杠)"<
cout<<"SyntaxError: invalid syntax"<
return NULL;
}
//////右边也是字符串
if ( (pos_begin_right=right.find("\"")) != string::npos )
{
if ( (pos_end_right=right.substr(pos_begin_right+1).find("\"")) == string::npos )
{
cout<<"SyntaxError: invalid syntax"<
return NULL;
}
// cout<<"in 2"<
string tmp_str_left(left,pos_begin_left+1,pos_end_left);
string tmp_str_right(right,pos_begin_right+1,pos_end_right);
string sum="";
sum += tmp_str_left + tmp_str_right;
return (py_object*)create_stringobject(sum.c_str());
}
///////右边是符号
else if (global_map.count(right)!=0)
{
py_object *obj;
obj=global_map[right];
if ( obj->ob_type->ob_name != string("string_type") )
{
cout<<"SyntaxError: invalid syntax"<
return NULL;
}
else
{
string tmp_str_left(left,pos_begin_left+1,pos_end_left);
string sum="";
sum +=tmp_str_left+string(((py_stringobject*)obj)->ob_str);
return (py_object*)create_stringobject(sum.c_str());
}
}
else
{
cout<<"SyntaxError: invalid syntax"<
return NULL;
}
}
///////左边是符号右边是字符串
else if ( (pos_begin_right=right.find("\"")) != string::npos )
{
// cout<<"in 1"<
if ( (pos_end_right=right.substr(pos_begin_right+1).find("\""))==string::npos )
{
cout<<"SyntaxError: invalid syntax"<
return NULL;
}
if (global_map.count(left) !=0 )
{
py_object* obj;
obj=global_map[left];
string tmp_str_right=right.substr(pos_begin_right+1,pos_end_right);
string sum="";
sum +=string(((py_stringobject*)obj)->ob_str) + tmp_str_right;
return (py_object*)create_stringobject(sum.c_str());
}
else
{
cout<<"SyntaxError: invalid syntax"<
return NULL;
}
}
else
{
printf("SyntaxError: invalid syntax\n");
return NULL;
}
}
return NULL;
}
bool all_isalpha(string str)
{
for(string::size_type i=0; i != str.size(); i++)
if (!isalpha(str[i]))
return false;
return true;
}
bool all_isdigit(string str)
{
for(string::size_type i=0; i != str.size(); i++)
if (!isdigit(str[i]))
return false;
return true;
}
void python_env()
{
string line;
string promt=">>>";
cout<
do
{
getline(cin,line);
if ( line==string("quit") ) return ;
else
excute(line);
cout<
}while(1);
}
void dealloc_pyton_env()
{
map::iterator iter;
for(iter= global_map.begin(); iter != global_map.end(); iter++)
{
delete(iter->second);
}
}
int main()
{
cout<<"hello,world"<
for (long i=0; i < 1; i++)
{
py_intobject *a=create_intobject(5);
a->ob_type->ob_print((py_object*)a);
py_stringobject *b=create_stringobject("brooks");
b->ob_type->ob_print((py_object*)b);
dealloc((py_object*)a);
dealloc((py_object*)b);
}
cout<
string s1=" helloe ";
cout<
cout<<"len(s1)="<
Trim(s1);
cout<
cout<<"len(s1)="<
python_env();
dealloc_pyton_env();
return 0;
}
阅读(460) | 评论(0) | 转发(0) |