Browse Source

functions now may be declared with any "var" as a name;
therefore they do not have a "baptism" name.
Changes in debug API to acomodate that.

Roberto Ierusalimschy 30 years ago
parent
commit
15d48576ea
8 changed files with 137 additions and 124 deletions
  1. 20 0
      func.c
  2. 2 2
      func.h
  3. 26 13
      iolib.c
  4. 60 64
      lua.stx
  5. 4 3
      luadebug.h
  6. 3 8
      opcode.c
  7. 21 31
      table.c
  8. 1 3
      table.h

+ 20 - 0
func.c

@@ -1,7 +1,10 @@
 #include <stdio.h>
+
+#include "luadebug.h"
 #include "table.h"
 #include "mem.h"
 #include "func.h"
+#include "opcode.h"
 
 static TFunc *function_root = NULL;
 
@@ -57,3 +60,20 @@ Long luaI_funccollector (void)
   }
   return counter;
 }
+
+
+void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
+{
+  Object *f = luaI_Address(func);
+  if (f->tag == LUA_T_MARK || f->tag == LUA_T_FUNCTION)
+  {
+    *filename = f->value.tf->fileName;
+    *linedefined = f->value.tf->lineDefined;
+  }
+  else if (f->tag == LUA_T_CMARK || f->tag == LUA_T_CFUNCTION)
+  {
+    *filename = "(C)";
+    *linedefined = -1;
+  }
+}
+

+ 2 - 2
func.h

@@ -2,6 +2,7 @@
 #define func_h
 
 #include "types.h"
+#include "lua.h"
 
 /*
 ** Header para funcoes.
@@ -13,12 +14,11 @@ typedef struct TFunc
   int		size;
   Byte		*code;
   int		lineDefined;
-  char		*name1;  /* function or method name (or null if main) */
-  char		*name2;  /* object name (or null if not method) */
   char		*fileName;
 } TFunc;
 
 Long luaI_funccollector (void);
 void luaI_insertfunction (TFunc *f);
 
+
 #endif

+ 26 - 13
iolib.c

@@ -3,7 +3,7 @@
 ** Input/output library to LUA
 */
 
-char *rcs_iolib="$Id: iolib.c,v 1.24 1995/10/17 14:12:45 roberto Exp roberto $";
+char *rcs_iolib="$Id: iolib.c,v 1.25 1995/10/23 13:53:48 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <ctype.h>
@@ -612,20 +612,33 @@ static void print_message (void)
   fprintf(stderr, "Active Stack:\n");
   while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT)
   {
-    char *filename; char *funcname;
-    char *objname; int linedefined;
-    lua_funcinfo(func, &filename, &funcname, &objname, &linedefined);
-    if (objname == NULL)
-      if (funcname)
-        fprintf(stderr, "\t%s", funcname);
-      else
+    char *name;
+    int currentline;
+    fprintf(stderr, "\t");
+    switch (*getobjname(func, &name))
+    {
+      case 'g':
+        fprintf(stderr, "function %s", name);
+        break;
+      case 'f':
+        fprintf(stderr, "fallback %s", name);
+        break;
+      default:
       {
-        fprintf(stderr, "\tmain of %s\n", filename);
-        continue;
+        char *filename;
+        int linedefined;
+        lua_funcinfo(func, &filename, &linedefined);
+        if (linedefined == 0)
+          fprintf(stderr, "main of %s", filename);
+        else if (linedefined < 0)
+          fprintf(stderr, "%s", filename);
+        else
+          fprintf(stderr, "function (%s:%d)", filename, linedefined);
       }
-    else
-      fprintf(stderr, "\t%s:%s", objname, funcname);
-    fprintf(stderr, "\t(defined in %s)\n", filename);
+    }
+    if ((currentline = lua_currentline(func)) > 0)
+      fprintf(stderr, "  at line %d", currentline);
+    fprintf(stderr, "\n");
   }
 }
 

+ 60 - 64
lua.stx

@@ -1,6 +1,6 @@
 %{
 
-char *rcs_luastx = "$Id: lua.stx,v 3.22 1995/10/25 13:05:51 roberto Exp roberto $";
+char *rcs_luastx = "$Id: lua.stx,v 3.23 1995/10/25 14:33:25 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -54,6 +54,14 @@ static int     nfields=0;
 
 /* Internal functions */
 
+static void yyerror (char *s)
+{
+ static char msg[256];
+ sprintf (msg,"%s near \"%s\" at line %d in file `%s'",
+          s, lua_lasttext (), lua_linenumber, lua_parsedfile);
+ lua_error (msg);
+}
+
 static void code_byte (Byte c)
 {
  if (pc>maxcurr-2)  /* 1 byte free to code HALT of main code */
@@ -148,6 +156,8 @@ static void add_localvar (Word name)
 
 static void store_localvar (Word name, int n)
 {
+ if (*initcode == basepc)
+   yyerror("local variable outside function body");
  if (nlocalvar+n < MAXLOCALS)
   localvar[nlocalvar+n] = name;
  else
@@ -249,6 +259,20 @@ static void savemain (void)
   maincode=pc; *initcode=basepc; maxmain=maxcurr;
 }
 
+static void init_func (void)
+{
+  if (funcCode == NULL)	/* first function */
+  {
+   funcCode = newvector(CODE_BLOCK, Byte);
+   maxcode = CODE_BLOCK;
+  }
+  savemain();  /* save main values */
+  /* set func values */
+  pc=0; basepc=funcCode; maxcurr=maxcode; 
+  nlocalvar = 0;
+  luaI_codedebugline(lua_linenumber);
+}
+
 static void codereturn (void)
 {
   if (nlocalvar == 0)
@@ -300,23 +324,31 @@ static void adjust_mult_assign (int vars, Long exps, int temps)
     lua_codeadjust(temps);
 }
 
-static void lua_codestore (int i)
+static void storesinglevar (Long v)
 {
- if (varbuffer[i] > 0)		/* global var */
+ if (v > 0)		/* global var */
  {
-  code_byte(STOREGLOBAL);
-  code_word(varbuffer[i]-1);
+   code_byte(STOREGLOBAL);
+   code_word(v-1);
  }
- else if (varbuffer[i] < 0)      /* local var */
+ else if (v < 0)      /* local var */
  {
-  int number = (-varbuffer[i]) - 1;
-  if (number < 10) code_byte(STORELOCAL0 + number);
-  else
-  {
-   code_byte(STORELOCAL);
-   code_byte(number);
-  }
+   int number = (-v) - 1;
+   if (number < 10) code_byte(STORELOCAL0 + number);
+   else
+   {
+     code_byte(STORELOCAL);
+     code_byte(number);
+   }
  }
+ else 
+   code_byte(STOREINDEXED0);
+}
+
+static void lua_codestore (int i)
+{
+ if (varbuffer[i] != 0)  /* global or local var */
+  storesinglevar(varbuffer[i]);
  else				  /* indexed var */
  {
   int j;
@@ -352,14 +384,6 @@ static void codeIf (Long thenAdd, Long elseAdd)
   code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1));
 }
 
-static void yyerror (char *s)
-{
- static char msg[256];
- sprintf (msg,"%s near \"%s\" at line %d in file `%s'",
-          s, lua_lasttext (), lua_linenumber, lua_parsedfile);
- lua_error (msg);
-}
-
 
 /*
 ** Parse LUA code.
@@ -419,8 +443,8 @@ void lua_parse (TFunc *tf)
 %type <vInt>  fieldlist, localdeclist, decinit
 %type <vInt>  ffieldlist, ffieldlist1, semicolonpart
 %type <vInt>  lfieldlist, lfieldlist1
-%type <vInt>  functiontoken
-%type <vLong> var, singlevar
+%type <vInt>  parlist
+%type <vLong> var, singlevar, funcname
 %type <pFunc> body
 
 %left AND OR
@@ -438,59 +462,29 @@ void lua_parse (TFunc *tf)
 functionlist : /* empty */
 	     | functionlist globalstat
 	     | functionlist function
-	     | functionlist method
 	     ;
 
 globalstat   : stat sc
 	     | setdebug
 	     ;
 
-function     : functiontoken NAME body 	 
+function     : FUNCTION funcname body 	 
 	       { 
 		code_byte(PUSHFUNCTION);
 		code_code($3);
-		code_byte(STOREGLOBAL);
-		code_word(luaI_findsymbol($2));
-		$3->lineDefined = $1;
-		$3->name1 = $2->ts.str;
-		$3->name2 = NULL;
-		$3->fileName = lua_parsedfile;
+		storesinglevar($2);
 	       }
 	       ;
 
-method         : functiontoken NAME methkind NAME body
-	       {
-	        /* assign function to table field */
-		lua_pushvar(luaI_findsymbol($2)+1);
-                code_byte(PUSHSTRING);
-		code_word(luaI_findconstant($4));
-                code_byte(PUSHFUNCTION);
-                code_code($5);
-                code_byte(STOREINDEXED0);
-		$5->lineDefined = $1;
-		$5->name1 = $4->ts.str;
-		$5->name2 = $2->ts.str;
-		$5->fileName = lua_parsedfile;
-	       }
-	       ;
-
-functiontoken	: FUNCTION
+funcname	: var { $$ =$1; init_func(); }
+		| varexp ':' NAME
 	{
-	 if (funcCode == NULL)	/* first function */
-	 {
-	  funcCode = newvector(CODE_BLOCK, Byte);
-	  maxcode = CODE_BLOCK;
-	 }
-	 savemain();  /* save main values */
-	 /* set func values */
-	 pc=0; basepc=funcCode; maxcurr=maxcode; 
-	 nlocalvar=0;
-	 $$ = lua_linenumber;
+	  code_byte(PUSHSTRING);
+	  code_word(luaI_findconstant($3));
+	  $$ = 0;  /* indexed variable */
+	  init_func();
+	  add_localvar(luaI_findsymbolbyname("self"));
 	}
-	        ;
-
-methkind	: ':'  { add_localvar(luaI_findsymbolbyname("self")); }
-		| '.'  /* no self */
 		;
 
 body :  '(' parlist ')' block END
@@ -499,6 +493,8 @@ body :  '(' parlist ')' block END
 	  $$ = new(TFunc);
 	  $$->size = pc;
 	  $$->code = newvector(pc, Byte);
+	  $$->fileName = lua_parsedfile;
+	  $$->lineDefined = $2;
 	  memcpy($$->code, basepc, pc*sizeof(Byte));
 	  /* save func values */
 	  funcCode = basepc; maxcode=maxcurr;
@@ -674,8 +670,8 @@ exprlist1 :  expr	{ if ($1 != 0) $$ = $1; else $$ = -1; }
 	}
 	  ;
 
-parlist  :	/* empty */ { lua_codeadjust(0); }
-	  |	parlist1    { lua_codeadjust(0); }
+parlist  :	/* empty */ { lua_codeadjust(0); $$ = lua_linenumber; }
+	  |	parlist1    { lua_codeadjust(0); $$ = lua_linenumber; }
 	  ;
 		
 parlist1 :	NAME		  

+ 4 - 3
luadebug.h

@@ -2,7 +2,7 @@
 ** LUA - Linguagem para Usuarios de Aplicacao
 ** Grupo de Tecnologia em Computacao Grafica
 ** TeCGraf - PUC-Rio
-** $Id: $
+** $Id: luadebug.h,v 1.1 1995/10/17 14:12:45 roberto Exp roberto $
 */
 
 
@@ -12,8 +12,9 @@
 #include "lua.h"
 
 lua_Object lua_stackedfunction(int level);
-void lua_funcinfo (lua_Object func, char **filename, char **funcname,
-                    char **objname, int *linedefined);
+void lua_funcinfo (lua_Object func, char **filename, int *linedefined);
+int lua_currentline (lua_Object func);
+char *getobjname (lua_Object o, char **name);
 
 
 #endif

+ 3 - 8
opcode.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
 
-char *rcs_opcode="$Id: opcode.c,v 3.46 1995/10/17 14:30:05 roberto Exp roberto $";
+char *rcs_opcode="$Id: opcode.c,v 3.47 1995/10/25 13:05:51 roberto Exp roberto $";
 
 #include <setjmp.h>
 #include <stdlib.h>
@@ -355,17 +355,13 @@ lua_Object lua_stackedfunction (int level)
 }
 
 
-void lua_funcinfo (lua_Object func, char **filename, char **funcname,
-                    char **objname, int *line)
+int lua_currentline (lua_Object func)
 {
   Object *f = Address(func);
-  luaI_funcInfo(f, filename, funcname, objname, line);
-  *line = (f+1 < top && (f+1)->tag == LUA_T_LINE) ?
-          (f+1)->value.i : -1;
+  return (f+1 < top && (f+1)->tag == LUA_T_LINE) ? (f+1)->value.i : -1;
 }
 
 
-
 /*
 ** Execute a protected call. Assumes that function is at CBase and
 ** parameters are on top of it. Leave nResults on the stack. 
@@ -406,7 +402,6 @@ static int do_protectedmain (void)
   stack[CBase].tag = LUA_T_FUNCTION;
   stack[CBase].value.tf = &tf;
   tf.lineDefined = 0;
-  tf.name1 = tf.name2 = NULL;
   tf.fileName = lua_parsedfile;
   tf.code = NULL;
   if (setjmp(myErrorJmp) == 0)

+ 21 - 31
table.c

@@ -3,9 +3,9 @@
 ** Module to control static tables
 */
 
-char *rcs_table="$Id: table.c,v 2.35 1995/10/17 11:58:41 roberto Exp roberto $";
+char *rcs_table="$Id: table.c,v 2.36 1995/10/23 13:53:48 roberto Exp roberto $";
 
-#include <string.h>
+/*#include <string.h>*/
 
 #include "mem.h"
 #include "opcode.h"
@@ -15,6 +15,7 @@ char *rcs_table="$Id: table.c,v 2.35 1995/10/17 11:58:41 roberto Exp roberto $";
 #include "inout.h"
 #include "lua.h"
 #include "fallback.h"
+#include "luadebug.h"
 
 
 #define BUFFER_BLOCK 256
@@ -254,39 +255,28 @@ static void getglobal (void)
 }
 
 
-static lua_CFunction cfunc = NULL;
+static Object *functofind;
 static int checkfunc (Object *o)
 {
-  return ((o->tag == LUA_T_CMARK || o->tag == LUA_T_CFUNCTION) &&
-           o->value.f == cfunc);
+  if (o->tag == LUA_T_FUNCTION)
+    return
+       ((functofind->tag == LUA_T_FUNCTION || functofind->tag == LUA_T_MARK)
+            && (functofind->value.tf == o->value.tf));
+  if (o->tag == LUA_T_CFUNCTION)
+    return
+       ((functofind->tag == LUA_T_CFUNCTION || functofind->tag == LUA_T_CMARK)
+            && (functofind->value.f == o->value.f));
+  return 0;
 }
 
 
-void luaI_funcInfo (struct Object *func, char **filename, char **funcname,
-                    char **objname, int *linedefined)
-{
-  if (func->tag == LUA_T_MARK || func->tag == LUA_T_FUNCTION)
-  {
-    TFunc *f = func->value.tf;
-    *filename = f->fileName;
-    *funcname = f->name1;
-    *objname = f->name2;
-    *linedefined = f->lineDefined;
-  }
-  else if (func->tag == LUA_T_CMARK || func->tag == LUA_T_CFUNCTION)
-  {
-    /* temporario: */
-    cfunc = func->value.f;
-    *filename = "(C)";
-    *linedefined = 0;
-    *funcname = lua_travsymbol(checkfunc);
-    if (*funcname)
-      *objname = 0;
-    else
-    {
-      *funcname = luaI_travfallbacks(checkfunc);
-      *objname = "(FB)";
-    }
-  }
+char *getobjname (lua_Object o, char **name)
+{ /* try to find a name for given function */
+  functofind = luaI_Address(o);
+  if ((*name = lua_travsymbol(checkfunc)) != NULL)
+    return "global";
+  else if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
+    return "fallback";
+  else return "";
 }
 

+ 1 - 3
table.h

@@ -1,7 +1,7 @@
 /*
 ** Module to control static tables
 ** TeCGraf - PUC-Rio
-** $Id: table.h,v 2.11 1995/10/13 15:16:25 roberto Exp roberto $
+** $Id: table.h,v 2.12 1995/10/17 11:58:41 roberto Exp roberto $
 */
 
 #ifndef table_h
@@ -25,7 +25,5 @@ Word  luaI_findconstantbyname (char *name);
 int   lua_markobject      (Object *o);
 void  lua_pack            (void);
 
-void luaI_funcInfo (Object *func, char **filename, char **funcname,
-                    char **objname, int *linedefined);
 
 #endif