parsav  Diff

Differences From Artifact [c91733fef5]:

To Artifact [c8d105a016]:


    13     13   	fmt = terralib.externfunction('asprintf',
    14     14   		terralib.types.funcpointer({&rawstring,rawstring},{int},true));
    15     15   	bfmt = terralib.externfunction('sprintf',
    16     16   		terralib.types.funcpointer({rawstring,rawstring},{int},true));
    17     17   	span = terralib.externfunction('strspn',{rawstring, rawstring} -> rawstring);
    18     18   }
    19     19   
    20         -(lib.mem.ptr(int8)).metamethods.__cast = function(from,to,e)
    21         -	if from == &int8 then
    22         -		return `[lib.mem.ptr(int8)]{ptr = e, ct = m.sz(e)}
    23         -	elseif to == &int8 then
    24         -		return e.ptr
           20  +do local strptr = (lib.mem.ptr(int8))
           21  +	local byteptr = (lib.mem.ptr(uint8))
           22  +	strptr.metamethods.__cast = function(from,to,e)
           23  +		if from == &int8 then
           24  +			return `strptr {ptr = e, ct = m.sz(e)}
           25  +		elseif to == &int8 then
           26  +			return e.ptr
           27  +		end
           28  +	end
           29  +
           30  +	terra strptr:cmp(other: strptr)
           31  +		var sz = lib.math.biggest(self.ct, other.ct)
           32  +		for i = 0, sz do
           33  +			if self.ptr[i] == 0 and other.ptr[i] == 0 then return true end
           34  +			if self.ptr[i] ~= other.ptr[i] then return false end
           35  +		end
           36  +		return true
           37  +	end
           38  +
           39  +	terra byteptr:cmp(other: byteptr)
           40  +		var sz = lib.math.biggest(self.ct, other.ct)
           41  +		for i = 0, sz do
           42  +			if self.ptr[i] == 0 and other.ptr[i] == 0 then return true end
           43  +			if self.ptr[i] ~= other.ptr[i] then return false end
           44  +		end
           45  +		return true
    25     46   	end
    26     47   end
    27     48   
    28     49   struct m.acc {
    29     50   	buf: rawstring
    30     51   	sz: intptr
    31     52   	run: intptr
................................................................................
    65     86   	var pt: lib.mem.ptr(int8)
    66     87   	pt.ptr = self.buf
    67     88   	pt.ct = self.sz
    68     89   	self.buf = nil
    69     90   	self.sz = 0
    70     91   	return pt
    71     92   end;
           93  +
           94  +terra m.acc:cue(sz: intptr)
           95  +	if sz <= self.run then return end
           96  +	self.run = sz
           97  +	if self.space - self.sz < self.run then
           98  +		self.space = self.sz + self.run
           99  +		self.buf = [rawstring](lib.mem.heapr_raw(self.buf, self.space))
          100  +	end
          101  +end
    72    102   
    73    103   terra m.acc:push(str: rawstring, len: intptr)
    74    104   	--var llen = len
    75    105   	if str == nil then return self end
    76    106   	--if str[len - 1] == 0xA then llen = llen - 1 end -- don't display newlines in debug output
    77    107   	-- lib.dbg('pushing "',{str,llen},'" onto accumulator')
    78    108   	if self.buf == nil then self:init(self.run) end
................................................................................
    87    117   	return self
    88    118   end;
    89    119   
    90    120   m.lit = macro(function(str)
    91    121   	return `[lib.mem.ref(int8)] {ptr = [str:asvalue()], ct = [#(str:asvalue())]}
    92    122   end)
    93    123   
          124  +m.acc.methods.lpush = macro(function(self,str)
          125  +	return `self:push([str:asvalue()], [#(str:asvalue())]) end)
    94    126   m.acc.methods.ppush = terra(self: &m.acc, str: lib.mem.ptr(int8))
    95    127   	self:push(str.ptr, str.ct)            return self end;
    96    128   m.acc.methods.merge = terra(self: &m.acc, str: lib.mem.ptr(int8))
    97    129   	self:push(str.ptr, str.ct) str:free() return self end;
    98    130   m.acc.methods.compose = macro(function(self, ...)
    99    131   	local minlen = 0
   100    132   	local pstrs = {}