Changeset 2710 for code/trunk/src/tolua/lua/operator.lua
- Timestamp:
- Feb 28, 2009, 7:46:37 PM (16 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:ignore deleted
- Property svn:mergeinfo changed
-
code/trunk/src/tolua/lua/operator.lua
r2087 r2710 16 16 -- kind = set of character representing the operator (as it appers in C++ code) 17 17 classOperator = { 18 kind = '',18 kind = '', 19 19 } 20 20 classOperator.__index = classOperator … … 22 22 23 23 -- table to transform operator kind into the appropriate tag method name 24 _TM = {['+'] = 'add', 25 ['-'] = 'sub', 26 ['*'] = 'mul', 27 ['/'] = 'div', 28 ['<'] = 'lt', 29 ['<='] = 'le', 30 ['=='] = 'eq', 31 ['[]'] = 'geti', 32 ['&[]'] = 'seti', 33 --['->'] = 'flechita', 34 } 24 _TM = { 25 ['+'] = 'add', 26 ['-'] = 'sub', 27 ['*'] = 'mul', 28 ['/'] = 'div', 29 ['<'] = 'lt', 30 ['<='] = 'le', 31 ['=='] = 'eq', 32 ['[]'] = 'geti', 33 ['&[]'] = 'seti', 34 --['->'] = 'flechita', 35 } 35 36 36 37 37 38 -- Print method 38 39 function classOperator:print (ident,close) 39 print(ident.."Operator{")40 print(ident.." kind = '"..self.kind.."',")41 print(ident.." mod = '"..self.mod.."',")42 print(ident.." type = '"..self.type.."',")43 print(ident.." ptr = '"..self.ptr.."',")44 print(ident.." name = '"..self.name.."',")45 print(ident.." const = '"..self.const.."',")46 print(ident.." cname = '"..self.cname.."',")47 print(ident.." lname = '"..self.lname.."',")48 print(ident.." args = {")49 local i=150 while self.args[i] do51 self.args[i]:print(ident.." ",",")52 i = i+153 end54 print(ident.." }")55 print(ident.."}"..close)40 print(ident.."Operator{") 41 print(ident.." kind = '"..self.kind.."',") 42 print(ident.." mod = '"..self.mod.."',") 43 print(ident.." type = '"..self.type.."',") 44 print(ident.." ptr = '"..self.ptr.."',") 45 print(ident.." name = '"..self.name.."',") 46 print(ident.." const = '"..self.const.."',") 47 print(ident.." cname = '"..self.cname.."',") 48 print(ident.." lname = '"..self.lname.."',") 49 print(ident.." args = {") 50 local i=1 51 while self.args[i] do 52 self.args[i]:print(ident.." ",",") 53 i = i+1 54 end 55 print(ident.." }") 56 print(ident.."}"..close) 56 57 end 57 58 58 59 function classOperator:supcode_tmp() 59 60 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 61 if not _TM[self.kind] then 62 return classFunction.supcode(self) 63 end 64 65 -- no overload, no parameters, always inclass 66 output("/* method:",self.name," of class ",self:inclass()," */") 67 68 output("#ifndef TOLUA_DISABLE_"..self.cname) 69 output("\nstatic int",self.cname,"(lua_State* tolua_S)") 70 71 if overload < 0 then 72 output('#ifndef TOLUA_RELEASE\n') 73 end 74 output(' tolua_Error tolua_err;') 75 output(' if (\n') 76 -- check self 77 output(' !'..'tolua_isusertype(tolua_S,1,"'..self.parent.type..'",0,&tolua_err) ||\n') 78 output(' !tolua_isnoobj(tolua_S,2,&tolua_err)\n )') 79 output(' goto tolua_lerror;') 80 81 output(' else\n') 82 output('#endif\n') -- tolua_release 83 output(' {') 84 85 -- declare self 86 output(' ',self.const,self.parent.type,'*','self = ') 87 output('(',self.const,self.parent.type,'*) ') 88 output('tolua_tousertype(tolua_S,1,0);') 89 90 -- check self 91 output('#ifndef TOLUA_RELEASE\n') 92 output(' if (!self) tolua_error(tolua_S,"invalid \'self\' in function \''..self.name..'\'",NULL);'); 93 output('#endif\n') 94 95 -- cast self 96 output(' ',self.mod,self.type,self.ptr,'tolua_ret = ') 97 output('(',self.mod,self.type,self.ptr,')(*self);') 98 99 -- return value 100 local t,ct = isbasic(self.type) 101 if t then 102 output(' tolua_push'..t..'(tolua_S,(',ct,')tolua_ret);') 103 else 104 t = self.type 105 new_t = string.gsub(t, "const%s+", "") 106 if self.ptr == '' then 107 output(' {') 108 output('#ifdef __cplusplus\n') 109 output(' void* tolua_obj = new',new_t,'(tolua_ret);') 110 output(' tolua_pushusertype_and_takeownership(tolua_S,tolua_obj,"',t,'");') 111 output('#else\n') 112 output(' void* tolua_obj = tolua_copy(tolua_S,(void*)&tolua_ret,sizeof(',t,'));') 113 output(' tolua_pushusertype_and_takeownership(tolua_S,tolua_obj,"',t,'");') 114 output('#endif\n') 115 output(' }') 116 elseif self.ptr == '&' then 117 output(' tolua_pushusertype(tolua_S,(void*)&tolua_ret,"',t,'");') 118 else 119 if local_constructor then 120 output(' tolua_pushusertype_and_takeownership(tolua_S,(void *)tolua_ret,"',t,'");') 121 else 122 output(' tolua_pushusertype(tolua_S,(void*)tolua_ret,"',t,'");') 123 end 124 end 125 end 126 127 output(' }') 128 output(' return 1;') 129 130 output('#ifndef TOLUA_RELEASE\n') 131 output('tolua_lerror:\n') 132 output(' tolua_error(tolua_S,"#ferror in function \''..self.lname..'\'.",&tolua_err);') 133 output(' return 0;') 134 output('#endif\n') 135 136 137 output('}') 138 output('#endif //#ifndef TOLUA_DISABLE\n') 139 output('\n') 139 140 end 140 141 141 142 -- Internal constructor 142 143 function _Operator (t) 143 setmetatable(t,classOperator)144 145 if t.const ~= 'const' and t.const ~= '' then146 error("#invalid 'const' specification")147 end148 149 append(t)150 if not t:inclass() then151 error("#operator can only be defined as class member")152 end153 154 --t.name = t.name .. "_" .. (_TM[t.kind] or t.kind)155 t.cname = t:cfuncname("tolua")..t:overload(t)156 t.name = "operator" .. t.kind -- set appropriate calling name157 return t144 setmetatable(t,classOperator) 145 146 if t.const ~= 'const' and t.const ~= '' then 147 error("#invalid 'const' specification") 148 end 149 150 append(t) 151 if not t:inclass() then 152 error("#operator can only be defined as class member") 153 end 154 155 --t.name = t.name .. "_" .. (_TM[t.kind] or t.kind) 156 t.cname = t:cfuncname("tolua")..t:overload(t) 157 t.name = "operator" .. t.kind -- set appropriate calling name 158 return t 158 159 end 159 160 … … 161 162 function Operator (d,k,a,c) 162 163 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 local t = split_c_tokens(strsub(a,2,strlen(a)-1),',') -- eliminate braces181 local i=1182 local l = {n=0}183 while t[i] do184 l.n = l.n+1185 l[l.n] = Declaration(t[i],'var')186 i = i+1187 end188 if k == '[]' then189 190 191 d = gsub(d,'&','')192 elseif k=='&[]' then193 l.n = l.n+1194 l[l.n] = Declaration(d,'var')195 l[l.n].name = 'tolua_value'196 end197 local f = Declaration(d,'func')198 if k == '[]' and (l[1]==nil or isbasic(l[1].type)~='number') then199 error('operator[] can only be defined for numeric index.')200 end201 f.args = l202 f.const = c203 f.kind = op_k204 f.lname = "."..(_TM[f.kind] or f.kind)205 if not _TM[f.kind] then206 207 end208 if f.kind == '[]' and ref=='&' and f.const~='const' then209 Operator(d,'&'..k,a,c)-- create correspoding set operator210 end211 return _Operator(f)212 end 213 214 164 local op_k = string.gsub(k, "^%s*", "") 165 op_k = string.gsub(k, "%s*$", "") 166 --if string.find(k, "^[%w_:%d<>%*%&]+$") then 167 if d == "operator" and k ~= '' then 168 169 d = k.." operator" 170 elseif not _TM[op_k] then 171 172 if flags['W'] then 173 error("tolua: no support for operator" .. f.kind) 174 else 175 warning("No support for operator "..op_k..", ignoring") 176 return nil 177 end 178 end 179 180 local ref = '' 181 local t = split_c_tokens(strsub(a,2,strlen(a)-1),',') -- eliminate braces 182 local i=1 183 local l = {n=0} 184 while t[i] do 185 l.n = l.n+1 186 l[l.n] = Declaration(t[i],'var') 187 i = i+1 188 end 189 if k == '[]' then 190 local _ 191 _, _, ref = strfind(d,'(&)') 192 d = gsub(d,'&','') 193 elseif k=='&[]' then 194 l.n = l.n+1 195 l[l.n] = Declaration(d,'var') 196 l[l.n].name = 'tolua_value' 197 end 198 local f = Declaration(d,'func') 199 if k == '[]' and (l[1]==nil or isbasic(l[1].type)~='number') then 200 error('operator[] can only be defined for numeric index.') 201 end 202 f.args = l 203 f.const = c 204 f.kind = op_k 205 f.lname = "."..(_TM[f.kind] or f.kind) 206 if not _TM[f.kind] then 207 f.cast_operator = true 208 end 209 if f.kind == '[]' and ref=='&' and f.const~='const' then 210 Operator(d,'&'..k,a,c) -- create correspoding set operator 211 end 212 return _Operator(f) 213 end 214 215
Note: See TracChangeset
for help on using the changeset viewer.