From: <da...@us...> - 2003-08-09 03:58:37
|
Update of /cvsroot/binaryphp/binaryphp In directory sc8-pr-cvs1:/tmp/cvs-serv24741 Modified Files: convert.php php_var.cpp tokenflow.php Log Message: Added variable support. Index: convert.php =================================================================== RCS file: /cvsroot/binaryphp/binaryphp/convert.php,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** convert.php 5 Aug 2003 23:25:55 -0000 1.23 --- convert.php 9 Aug 2003 03:58:34 -0000 1.24 *************** *** 63,67 **** list($code, $flags) = $gen->Convert(); if(DEBUG) ! $flags .= ' -g'; $lines = explode("\n", $code); if(DEBUG == true) { --- 63,69 ---- list($code, $flags) = $gen->Convert(); if(DEBUG) ! $flag = ' -g'; ! else ! $flag = (string) null; $lines = explode("\n", $code); if(DEBUG == true) { *************** *** 86,91 **** shell_exec($comp); } else { ! $comp = 'g++ -o ' . $output . ' ' .$outputfile. ' '. $flags . ' >> log 2>> log'; ! echo $comp, "\n\n"; shell_exec($comp); } --- 88,99 ---- shell_exec($comp); } else { ! if(!file_exists('php_var.lo')) ! { ! $comp = 'g++ -c -o php_var.lo php_var.cpp ' . $flag . ' >> log 2>> log'; ! `echo $comp >> log`; ! shell_exec($comp); ! } ! $comp = 'g++ -o ' . $output . ' ' .$outputfile. ' php_var.lo '. $flags . $flag . ' >> log 2>> log'; ! `echo $comp >> log`; shell_exec($comp); } Index: php_var.cpp =================================================================== RCS file: /cvsroot/binaryphp/binaryphp/php_var.cpp,v retrieving revision 1.25 retrieving revision 1.26 diff -C2 -d -r1.25 -r1.26 *** php_var.cpp 6 Aug 2003 14:09:55 -0000 1.25 --- php_var.cpp 9 Aug 2003 03:58:34 -0000 1.26 *************** *** 1,14 **** ! #include <iostream> ! #include <string> ! #include <vector> ! #include <stdio.h> ! #include <iostream> ! #define PHP_NULL 0 ! #define PHP_STRING 1 ! #define PHP_INT 2 ! #define PHP_BOOL 3 ! #define PHP_ARRAY 4 ! #define PHP_RESOURCE 5 ! #define PHP_OBJECT 6 char* intstring(long i) --- 1,3 ---- ! #include "php_var.hpp" char* intstring(long i) *************** *** 24,422 **** return buf; } ! class php_var { ! public: ! php_var() // Constructor ! { ! type = PHP_NULL; // Make the var, but make it null. ! container = ""; ! } ! php_var(const char* str) ! { ! container = str; ! type = PHP_STRING; ! } ! php_var(double i) ! { ! type = PHP_INT; ! container = doublestring(i); ! } ! php_var(int i) ! { ! type = PHP_INT; ! container = intstring(i); ! } ! php_var(unsigned int i) ! { ! container = intstring(i); ! type = PHP_INT; ! } ! php_var(long i) ! { ! container = intstring(i); ! type = PHP_INT; ! } ! php_var(const php_var &temp) ! { ! type = temp.type; ! container = temp.container; ! keys = temp.keys; ! data = temp.data; ! res = temp.res; ! } ! php_var(char * str) ! { ! container = str; ! type = PHP_STRING; ! } ! php_var(string str) ! { ! container = str; ! type = PHP_STRING; ! } ! php_var(bool b) ! { ! if(b) ! container = "1"; ! else ! container = "0"; ! type = PHP_BOOL; ! } ! operator const char*() ! { ! if(type == PHP_STRING || type == PHP_INT) ! return container.c_str(); ! else ! return "Array"; ! } ! operator string() ! { ! if(type == PHP_STRING || type == PHP_INT) ! return container; ! else ! return string("Array"); ! } ! operator bool() ! { ! if(type != PHP_BOOL || (type == PHP_BOOL && container.compare("1") == 0)) ! return true; ! return false; ! } ! operator double() ! { ! return atof(container.c_str()); ! } ! operator float() ! { ! return atof(container.c_str()); ! } ! operator int() ! { ! return atoi(container.c_str()); ! } ! operator unsigned int() ! { ! return atoi(container.c_str()); ! } ! operator long() ! { ! return atol(container.c_str()); ! } ! php_var &operator[](int subscript) ! { ! if(type == PHP_STRING) ! { ! // return &container[subscript]; ! } ! else if(type == PHP_ARRAY) ! { ! php_var key = subscript; ! int i = 0; ! for(i = 0;i < keys.size(); ++i) ! { ! if(key.container.compare(keys[i].container) == 0) ! return data[i]; ! } ! php_var temp; ! keys.push_back(key); ! data.push_back(temp); ! return data[i]; ! } ! } ! php_var &operator[](unsigned int subscript) ! { ! if(type == PHP_STRING) ! { ! // return &container[subscript]; ! } ! else if(type == PHP_ARRAY) ! { ! php_var key = subscript; ! int i = 0; ! for(i = 0;i < keys.size(); ++i) ! { ! if(key.container.compare(keys[i].container) == 0) ! return data[i]; ! } ! php_var temp; ! keys.push_back(key); ! data.push_back(temp); ! return data[i]; ! } ! } ! php_var &operator[](long subscript) ! { ! if(type == PHP_STRING) ! { ! // return &container[subscript]; ! } ! else if(type == PHP_ARRAY) ! { ! php_var key = subscript; ! int i = 0; ! for(i = 0;i < keys.size(); ++i) ! { ! if(key.container.compare(keys[i].container) == 0) ! return data[i]; ! } ! php_var temp; ! keys.push_back(key); ! data.push_back(temp); ! return data[i]; ! } ! } ! php_var &operator[](const char* subscript) ! { ! if(type == PHP_STRING) ! { ! // return &container[subscript]; ! } ! else if(type == PHP_ARRAY) ! { ! php_var key = subscript; ! int i = 0; ! for(i = 0;i < keys.size(); ++i) ! { ! if(key.container.compare(keys[i].container) == 0) ! return data[i]; ! } ! php_var temp; ! keys.push_back(key); ! data.push_back(temp); ! return data[i]; ! } ! } ! php_var &operator[](char* subscript) ! { ! if(type == PHP_STRING) ! { ! // return &container[subscript]; ! } ! else if(type == PHP_ARRAY) ! { ! php_var key = subscript; ! int i = 0; ! for(i = 0;i < keys.size(); ++i) ! { ! if(key.container.compare(keys[i].container) == 0) ! return data[i]; ! } ! php_var temp; ! keys.push_back(key); ! data.push_back(temp); ! return data[i]; ! } ! } ! php_var &operator[](string subscript) ! { ! if(type == PHP_STRING) ! { ! // return &container[subscript]; ! } ! else if(type == PHP_ARRAY) ! { ! php_var key = subscript; ! int i = 0; ! for(i = 0;i < keys.size(); ++i) ! { ! if(key.container.compare(keys[i].container) == 0) ! return data[i]; ! } ! php_var temp; ! keys.push_back(key); ! data.push_back(temp); ! return data[i]; ! } ! } ! php_var &operator[](php_var subscript) { ! if(type == PHP_STRING) ! { ! // return (char *)&container[subscript]; ! } ! else if(type == PHP_ARRAY) ! { ! php_var key = subscript; ! int i = 0; ! for(i = 0;i < keys.size(); ++i) ! { ! if(key.container.compare(keys[i].container) == 0) ! return data[i]; ! } ! php_var temp; ! keys.push_back(key); ! data.push_back(temp); return data[i]; - } - } - bool operator<(int i) - { - if(atol(container.c_str()) < i) - return true; - return false; - } - bool operator>(int i) - { - if(atol(container.c_str()) > i) - return true; - return false; - } - bool operator<(php_var i) - { - if(atol(container.c_str()) < atol(i.container.c_str())) - return true; - return false; } ! bool operator>(php_var i) ! { ! if(atol(container.c_str()) > atol(i.container.c_str())) ! return true; ! return false; ! } ! bool operator!=(const char* cmp) ! { ! if(container.compare(cmp)) ! return true; ! return false; ! } ! bool operator!=(int i) ! { ! if(atol(container.c_str()) == i) ! return false; ! return true; ! } ! bool operator!=(php_var var) ! { ! if(!container.compare(var.container)) ! return false; ! return true; ! } ! bool operator==(const char* cmp) ! { ! if(container.compare(cmp) == 0) ! return true; ! return false; ! } ! bool operator==(int i) ! { ! if(atol(container.c_str()) == i) ! return true; ! return false; ! } ! bool operator==(const php_var& var) ! { ! if(this->container.compare(var.container) == 0) ! return true; ! return false; ! } ! bool operator==(string cmp) ! { ! return container == cmp; ! } ! int operator++(int i) ! { ! int ret = atol(container.c_str()); ! container = intstring(ret + i); ! return ret; ! } ! int operator++() ! { ! int ret = atol(container.c_str()) + 1; ! container = intstring(ret); ! return ret; ! } ! int operator--(int i) ! { ! int ret = atol(container.c_str()); ! container = intstring(ret); ! return ret; ! } ! int operator--() ! { ! int ret = atol(container.c_str()) + 1; ! container = intstring(ret); ! return ret; ! } ! void operator+=(int inc) ! { ! if(type == PHP_INT) ! { ! container = intstring(atol(container.c_str()) + inc); ! } ! } ! void operator+=(php_var str) ! { ! if(str.type == PHP_INT) ! { ! container = intstring(atol(container.c_str()) + atoi(str.container.c_str())); ! if(type != PHP_INT && type != PHP_STRING) ! type = PHP_INT; ! } ! else ! { ! container += str.container; ! if(type != PHP_STRING) ! type = PHP_STRING; ! } ! } ! void operator+=(string str) ! { ! container += str; ! } ! void operator+=(const char* str) ! { ! container += str; ! } ! void operator+=(char* str) ! { ! container += str; ! } ! void operator-=(int dec) ! { ! if(type == PHP_INT) ! { ! container = intstring(atol(container.c_str()) - dec); ! } ! } ! php_var operator-(php_var i) ! { type = PHP_INT; ! i.type = PHP_INT; ! *this -= i; ! return *this; ! } ! friend ostream &operator<<( ostream &out, const php_var &var ); ! void to_array() ! { ! type = PHP_ARRAY; ! } ! string container; // Contains value. ! vector<php_var> keys; ! vector<php_var> data; ! void *res; ! void *obj_type; ! int res_type; ! int type; // Contains current type. ! }; template<typename T> inline T * OBJ(php_var obj) { return (reinterpret_cast<T *>(obj.res)); } ostream &operator<<( ostream &out, const php_var &var ) --- 13,288 ---- return buf; } ! ! php_var::php_var() // Constructor { ! type = PHP_NULL; // Make the var, but make it null. ! container = ""; ! } ! php_var::php_var(const char* str) ! { ! container = str; ! type = PHP_STRING; ! } ! php_var::php_var(double i) ! { ! type = PHP_INT; ! container = doublestring(i); ! } ! php_var::php_var(int i) ! { ! type = PHP_INT; ! container = intstring(i); ! } ! php_var::php_var(unsigned int i) ! { ! container = intstring(i); ! type = PHP_INT; ! } ! php_var::php_var(long i) ! { ! container = intstring(i); ! type = PHP_INT; ! } ! php_var::php_var(const php_var &temp) ! { ! type = temp.type; ! container = temp.container; ! keys = temp.keys; ! data = temp.data; ! res = temp.res; ! } ! php_var::php_var(char * str) ! { ! container = str; ! type = PHP_STRING; ! } ! php_var::php_var(string str) ! { ! container = str; ! type = PHP_STRING; ! } ! php_var::php_var(bool b) ! { ! if(b) ! container = "1"; ! else ! container = "0"; ! type = PHP_BOOL; ! } ! php_var::operator const char*() ! { ! if(type == PHP_STRING || type == PHP_INT) ! return container.c_str(); ! else ! return "Array"; ! } ! php_var::operator string() ! { ! if(type == PHP_STRING || type == PHP_INT) ! return container; ! else ! return string("Array"); ! } ! php_var::operator bool() ! { ! if(type != PHP_BOOL || (type == PHP_BOOL && container.compare("1") == 0)) ! return true; ! return false; ! } ! php_var::operator double() ! { ! return atof(container.c_str()); ! } ! php_var::operator float() ! { ! return atof(container.c_str()); ! } ! php_var::operator int() ! { ! return atoi(container.c_str()); ! } ! php_var::operator unsigned int() ! { ! return atoi(container.c_str()); ! } ! php_var::operator long() ! { ! return atol(container.c_str()); ! } ! php_var &php_var::operator[](int subscript) ! { ! return (*this)[(php_var)subscript]; ! } ! php_var &php_var::operator[](unsigned int subscript) ! { ! return (*this)[(php_var)subscript]; ! } ! php_var &php_var::operator[](const char* subscript) ! { ! return (*this)[(php_var)subscript]; ! } ! php_var &php_var::operator[](char* subscript) ! { ! return (*this)[(php_var)subscript]; ! } ! php_var &php_var::operator[](string subscript) ! { ! return (*this)[(php_var)subscript]; ! } ! php_var &php_var::operator[](php_var subscript) ! { ! if(type == PHP_STRING) ! { ! // return &container[subscript]; ! } ! else if(type == PHP_ARRAY) ! { ! php_var key = subscript; ! int i = 0; ! for(i = 0;i < keys.size(); ++i) { ! if(key.container.compare(keys[i].container) == 0) return data[i]; } ! php_var temp; ! keys.push_back(key); ! data.push_back(temp); ! return data[i]; ! } ! } ! bool php_var::operator<(int i) ! { ! if(atol(container.c_str()) < i) ! return true; ! return false; ! } ! bool php_var::operator>(int i) ! { ! if(atol(container.c_str()) > i) ! return true; ! return false; ! } ! bool php_var::operator<(php_var i) ! { ! if(atol(container.c_str()) < atol(i.container.c_str())) ! return true; ! return false; ! } ! bool php_var::operator>(php_var i) ! { ! if(atol(container.c_str()) > atol(i.container.c_str())) ! return true; ! return false; ! } ! bool php_var::operator!=(const char* cmp) ! { ! if(container.compare(cmp)) ! return true; ! return false; ! } ! bool php_var::operator!=(int i) ! { ! if(atol(container.c_str()) == i) ! return false; ! return true; ! } ! bool php_var::operator!=(php_var var) ! { ! if(!container.compare(var.container)) ! return false; ! return true; ! } ! bool php_var::operator==(const char* cmp) ! { ! if(container.compare(cmp) == 0) ! return true; ! return false; ! } ! bool php_var::operator==(int i) ! { ! if(atol(container.c_str()) == i) ! return true; ! return false; ! } ! bool php_var::operator==(php_var var) ! { ! if(container.compare(var.container) == 0) ! return true; ! return false; ! } ! int php_var::operator++(int i) ! { ! int ret = atol(container.c_str()); ! container = intstring(ret + i); ! return ret; ! } ! int php_var::operator++() ! { ! int ret = atol(container.c_str()) + 1; ! container = intstring(ret); ! return ret; ! } ! int php_var::operator--(int i) ! { ! int ret = atol(container.c_str()); ! container = intstring(ret); ! return ret; ! } ! int php_var::operator--() ! { ! int ret = atol(container.c_str()) + 1; ! container = intstring(ret); ! return ret; ! } ! void php_var::operator+=(int inc) ! { ! if(type == PHP_INT) ! { ! container = intstring(atol(container.c_str()) + inc); ! } ! } ! void php_var::operator+=(php_var str) ! { ! if(str.type == PHP_INT) ! { ! container = intstring(atol(container.c_str()) + atoi(str.container.c_str())); ! if(type != PHP_INT && type != PHP_STRING) type = PHP_INT; ! } ! else ! { ! container += str.container; ! if(type != PHP_STRING) ! type = PHP_STRING; ! } ! } ! void php_var::operator+=(string str) ! { ! container += str; ! } ! void php_var::operator+=(const char* str) ! { ! container += str; ! } ! void php_var::operator+=(char* str) ! { ! container += str; ! } ! void php_var::operator-=(int dec) ! { ! if(type == PHP_INT) ! { ! container = intstring(atol(container.c_str()) - dec); ! } ! } ! void php_var::operator-=(php_var i) ! { ! if(type == PHP_INT) ! container = doublestring(atof(container.c_str()) - atof(i.container.c_str())); ! } ! void php_var::to_array() ! { ! type = PHP_ARRAY; ! } template<typename T> inline T * OBJ(php_var obj) { return (reinterpret_cast<T *>(obj.res)); } ostream &operator<<( ostream &out, const php_var &var ) *************** *** 435,438 **** --- 301,308 ---- { return(v < i); + } + php_var operator-(php_var l, php_var r) + { + return (php_var)((int) l - (int) r); } float operator/(php_var &l, php_var &r) Index: tokenflow.php =================================================================== RCS file: /cvsroot/binaryphp/binaryphp/tokenflow.php,v retrieving revision 1.52 retrieving revision 1.53 diff -C2 -d -r1.52 -r1.53 *** tokenflow.php 8 Aug 2003 06:59:06 -0000 1.52 --- tokenflow.php 9 Aug 2003 03:58:34 -0000 1.53 *************** *** 77,80 **** --- 77,86 ---- */ var $includes = array(); + /** + * Variable scope + * @param array + * @access private + */ + var $scope = array(); /** *************** *** 90,93 **** --- 96,100 ---- $this->Parse_Tokenstream(); $this->functions[0] = array('main', 'int', array(array('int', 'argc'), array('char**', 'argv'))); + $this->scope[0] = array(); /* * First element of each array in the functions array is the name of the function. *************** *** 115,119 **** if($end != null && $token == $end) { ! ++$this->token; break; } --- 122,132 ---- if($end != null && $token == $end) { ! if($this->token < count($this->tokens) - 2) ! { ! if($data == null) ! $code .= $token; ! else ! $code .= $data; ! } break; } *************** *** 122,126 **** $params[] = $code; $code = (string) null; - // ++$this->token; continue; } --- 135,138 ---- *************** *** 140,143 **** --- 152,165 ---- $code .= '"' . str_replace(array('\\\'', '"'), array('\'', '\\"'), substr($data, 1, -1)) . '"'; break; + case T_VARIABLE: + if($break == null && $end == null) + $this->B_Variable($this->Parse_Tokenstream('=', ';')); + else + { + if($this->Define($data)) + $code .= 'php_var '; + $code .= '_' . substr($data, 1); + } + break; case '-': case '+': *************** *** 160,164 **** } /** ! * Code generator for the echo function * * @param array $parameters Things to echo. --- 182,186 ---- } /** ! * Code generator for echo * * @param array $parameters Things to echo. *************** *** 183,186 **** --- 205,221 ---- } /** + * Code generator for variables + * + * @param array $parameters Things to echo. + * @return string + * @access private + */ + function B_variable($parameters) + { + $code = implode(' = ', $parameters); + $this->AddCode($code); + return $code; + } + /** * Adds a line of code to the current function. * *************** *** 207,210 **** --- 242,276 ---- } /** + * Checks the variable scope to see if a given variable is declared in this scope or not. + * + * @param string $var Name of variable. + * @param int $function Function to search in. + * @return bool + * @access public + */ + function IsDefined($var, $function = null) + { + if($function == null) + $function = $this->curfunction; + return in_array($var, $this->scope[$function]); + } + /** + * Adds a variable to the current scope. + * + * @param string $var Name of variable. + * @param int $function Function to add to. + * @return bool + * @access public + */ + function Define($var, $function = null) + { + if($function == null) + $function = $this->curfunction; + if($this->IsDefined($var, $function)) + return false; + $this->scope[$function][] = $var; + return true; + } + /** * Organizes and outputs proper C++ code. * *************** *** 215,222 **** { $code = (string) null; - if(!empty($this->namespace)) - $code .= 'using namespace ' . $this->namespace . ';' . "\n"; foreach($this->includes as $include) $code .= '#include <' . $include . '>' . "\n"; foreach($this->code as $func => $arr) { --- 281,289 ---- { $code = (string) null; foreach($this->includes as $include) $code .= '#include <' . $include . '>' . "\n"; + if(!empty($this->namespace)) + $code .= 'using namespace ' . $this->namespace . ';' . "\n"; + $code .= '#include "php_var.hpp"' . "\n"; foreach($this->code as $func => $arr) { |