Quantcast

Understanding BinOpr

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Understanding BinOpr

Emeka

Hello All,

I can't figure out what  "enterlevel" function is doing here. I am  aware that the below has implicit stack. I am also aware that "nCcalls" is the number of  of nested calls.

https://www.lua.org/source/5.2/lparser.c.html

static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
  BinOpr op;
  UnOpr uop;
  enterlevel(ls);
  uop = getunopr(ls->t.token);
  if (uop != OPR_NOUNOPR) {
    int line = ls->linenumber;
    luaX_next(ls);
    subexpr(ls, v, UNARY_PRIORITY);
    luaK_prefix(ls->fs, uop, v, line);
  }
  else simpleexp(ls, v);
  /* expand while operators have priorities higher than `limit' */
  op = getbinopr(ls->t.token);
  while (op != OPR_NOBINOPR && priority[op].left > limit) {
    expdesc v2;
    BinOpr nextop;
    int line = ls->linenumber;
    luaX_next(ls);
    luaK_infix(ls->fs, op, v);
    /* read sub-expression with higher priority */
    nextop = subexpr(ls, &v2, priority[op].right);
    luaK_posfix(ls->fs, op, v, &v2, line);
    op = nextop;
  }
  leavelevel(ls);
  return op;  /* return first untreated operator */
}

Regards, Emeka
--
P.S Please join our groups:  [hidden email]
 or [hidden email]  These are platforms for learning and sharing  of knowledge.                                                                                      www.satajanus.com | Satajanus  Nig. Ltd


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Understanding BinOpr

Roberto Ierusalimschy
> I can't figure out what  "enterlevel" function is doing here. I am  aware
> that the below has implicit stack. I am also aware that "nCcalls" is the
> number of  of nested calls.

It avoids a stack overflow in cases of deep recursion in expressions,
such as this:

  s = "x = " .. string.rep("(", 2^20)
  print(load(s))

Without 'enterlevel', you can get a segmentation fault. With it, you
get a nice error ("too many C levels").

-- Roberto

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Error with 'combine' in 'luac.c' with 'ltests' enabled.

Paige DePol
I do not normally compile more than one script at a time with 'luac',
however, today I needed to do so and ran into an issue.

In the function 'combine' in the file 'luac.c' there is the following line:

f->sizelineinfo=0;

This is setting the size of the 'source line to code instruction' mapping
array to zero... without actually freeing the memory first. When running
with the 'ltests.c' module enabled the realloc function throws an assert
when trying to free the prototype as f->lineinfo still contains an allocated
block but f->sizelineinfo has been set to zero and no longer matches the
value in the memory header.

When coding and testing under 'ltests' it is inconvenient to have 'luac'
assert when compiling more than one file at a time!

I can see two possible solutions:

1) Remove the line and accept the 'luac' generated main function will
   show a source code line mapping of '1' instead of '-'.

2) Change the code as follows to free the mapping array memory:

   luaM_freearray(L, f->lineinfo, f->sizelineinfo);
   f->lineinfo = NULL; f->sizelineinfo = 0;

I have patched my version of Lua to free the memory and display a '-'
for the line number, which was the originally intended behavior.

~Paige


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error with 'combine' in 'luac.c' with 'ltests' enabled.

Luiz Henrique de Figueiredo
> When running with the 'ltests.c' module enabled the realloc function
> throws an assert when trying to free the prototype as f->lineinfo
> still contains an allocated block but f->sizelineinfo has been set to
> zero and no longer matches the value in the memory header.

Thanks for the report.

Loading...