misc/liblua/print.c
changeset 10017 de822cd3df3a
parent 2812 0a24853de796
equal deleted inserted replaced
10015:4feced261c68 10017:de822cd3df3a
    13 #include "ldebug.h"
    13 #include "ldebug.h"
    14 #include "lobject.h"
    14 #include "lobject.h"
    15 #include "lopcodes.h"
    15 #include "lopcodes.h"
    16 #include "lundump.h"
    16 #include "lundump.h"
    17 
    17 
    18 #define PrintFunction	luaU_print
    18 #define PrintFunction   luaU_print
    19 
    19 
    20 #define Sizeof(x)	((int)sizeof(x))
    20 #define Sizeof(x)   ((int)sizeof(x))
    21 #define VOID(p)		((const void*)(p))
    21 #define VOID(p)     ((const void*)(p))
    22 
    22 
    23 static void PrintString(const TString* ts)
    23 static void PrintString(const TString* ts)
    24 {
    24 {
    25  const char* s=getstr(ts);
    25  const char* s=getstr(ts);
    26  size_t i,n=ts->tsv.len;
    26  size_t i,n=ts->tsv.len;
    37    case '\f': printf("\\f"); break;
    37    case '\f': printf("\\f"); break;
    38    case '\n': printf("\\n"); break;
    38    case '\n': printf("\\n"); break;
    39    case '\r': printf("\\r"); break;
    39    case '\r': printf("\\r"); break;
    40    case '\t': printf("\\t"); break;
    40    case '\t': printf("\\t"); break;
    41    case '\v': printf("\\v"); break;
    41    case '\v': printf("\\v"); break;
    42    default:	if (isprint((unsigned char)c))
    42    default: if (isprint((unsigned char)c))
    43    			putchar(c);
    43             putchar(c);
    44 		else
    44         else
    45 			printf("\\%03u",(unsigned char)c);
    45             printf("\\%03u",(unsigned char)c);
    46   }
    46   }
    47  }
    47  }
    48  putchar('"');
    48  putchar('"');
    49 }
    49 }
    50 
    50 
    52 {
    52 {
    53  const TValue* o=&f->k[i];
    53  const TValue* o=&f->k[i];
    54  switch (ttype(o))
    54  switch (ttype(o))
    55  {
    55  {
    56   case LUA_TNIL:
    56   case LUA_TNIL:
    57 	printf("nil");
    57     printf("nil");
    58 	break;
    58     break;
    59   case LUA_TBOOLEAN:
    59   case LUA_TBOOLEAN:
    60 	printf(bvalue(o) ? "true" : "false");
    60     printf(bvalue(o) ? "true" : "false");
    61 	break;
    61     break;
    62   case LUA_TNUMBER:
    62   case LUA_TNUMBER:
    63 	printf(LUA_NUMBER_FMT,nvalue(o));
    63     printf(LUA_NUMBER_FMT,nvalue(o));
    64 	break;
    64     break;
    65   case LUA_TSTRING:
    65   case LUA_TSTRING:
    66 	PrintString(rawtsvalue(o));
    66     PrintString(rawtsvalue(o));
    67 	break;
    67     break;
    68   default:				/* cannot happen */
    68   default:              /* cannot happen */
    69 	printf("? type=%d",ttype(o));
    69     printf("? type=%d",ttype(o));
    70 	break;
    70     break;
    71  }
    71  }
    72 }
    72 }
    73 
    73 
    74 static void PrintCode(const Proto* f)
    74 static void PrintCode(const Proto* f)
    75 {
    75 {
   153   }
   153   }
   154   printf("\n");
   154   printf("\n");
   155  }
   155  }
   156 }
   156 }
   157 
   157 
   158 #define SS(x)	(x==1)?"":"s"
   158 #define SS(x)   (x==1)?"":"s"
   159 #define S(x)	x,SS(x)
   159 #define S(x)    x,SS(x)
   160 
   160 
   161 static void PrintHeader(const Proto* f)
   161 static void PrintHeader(const Proto* f)
   162 {
   162 {
   163  const char* s=getstr(f->source);
   163  const char* s=getstr(f->source);
   164  if (*s=='@' || *s=='=')
   164  if (*s=='@' || *s=='=')
   166  else if (*s==LUA_SIGNATURE[0])
   166  else if (*s==LUA_SIGNATURE[0])
   167   s="(bstring)";
   167   s="(bstring)";
   168  else
   168  else
   169   s="(string)";
   169   s="(string)";
   170  printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
   170  printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
   171  	(f->linedefined==0)?"main":"function",s,
   171     (f->linedefined==0)?"main":"function",s,
   172 	f->linedefined,f->lastlinedefined,
   172     f->linedefined,f->lastlinedefined,
   173 	S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));
   173     S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));
   174  printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
   174  printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
   175 	f->numparams,f->is_vararg?"+":"",SS(f->numparams),
   175     f->numparams,f->is_vararg?"+":"",SS(f->numparams),
   176 	S(f->maxstacksize),S(f->nups));
   176     S(f->maxstacksize),S(f->nups));
   177  printf("%d local%s, %d constant%s, %d function%s\n",
   177  printf("%d local%s, %d constant%s, %d function%s\n",
   178 	S(f->sizelocvars),S(f->sizek),S(f->sizep));
   178     S(f->sizelocvars),S(f->sizek),S(f->sizep));
   179 }
   179 }
   180 
   180 
   181 static void PrintConstants(const Proto* f)
   181 static void PrintConstants(const Proto* f)
   182 {
   182 {
   183  int i,n=f->sizek;
   183  int i,n=f->sizek;