⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opcode.c

📁 lua解释器早期1.0版本
💻 C
📖 第 1 页 / 共 2 页
字号:
      lua_error ("stack overflow");      return 1;     }    }    else if (tag(b-1) == T_CFUNCTION)    {     int nparam;      lua_debugline = 0;			/* always reset debug flag */     nvalue(b) = (base-stack);		/* store base value */     base = b+1;     nparam = top-base;			/* number of parameters */     (fvalue(b-1))();			/* call C function */          /* shift returned values */     {       int i;      int nretval = top - base - nparam;      top = base - 2;      base = stack + (int) nvalue(base-1);      for (i=0; i<nretval; i++)      {       *top = *(top+nparam+2);       ++top;      }     }    }    else    {     lua_reportbug ("call expression not a function");     return 1;    }   }   break;      case RETCODE:   {    int i;    int shift = *pc++;    int nretval = top - base - shift;    top = base - 2;    pc = bvalue(base-2);    base = stack + (int) nvalue(base-1);    for (i=0; i<nretval; i++)    {     *top = *(top+shift+2);     ++top;    }   }   break;      case HALT:   return 0;		/* success */      case SETFUNCTION:   {    int file, func;    file = *((Word *)(pc));    pc += sizeof(Word);    func = *((Word *)(pc));    pc += sizeof(Word);    if (lua_pushfunction (file, func))     return 1;   }   break;      case SETLINE:    lua_debugline = *((Word *)(pc));    pc += sizeof(Word);   break;      case RESET:    lua_popfunction ();   break;      default:    lua_error ("internal error - opcode didn't match");   return 1;  } }}/*** Mark all strings and arrays used by any object stored at stack.*/void lua_markstack (void){ Object *o; for (o = top-1; o >= stack; o--)  lua_markobject (o);}/*** Open file, generate opcode and execute global statement. Return 0 on** success or 1 on error.*/int lua_dofile (char *filename){ if (lua_openfile (filename)) return 1; if (lua_parse ()) { lua_closefile (); return 1; } lua_closefile (); return 0;}/*** Generate opcode stored on string and execute global statement. Return 0 on** success or 1 on error.*/int lua_dostring (char *string){ if (lua_openstring (string)) return 1; if (lua_parse ()) return 1; return 0;}/*** Execute the given function. Return 0 on success or 1 on error.*/int lua_call (char *functionname, int nparam){ static Byte startcode[] = {CALLFUNC, HALT}; int i;  Object func = s_object(lua_findsymbol(functionname)); if (tag(&func) != T_FUNCTION) return 1; for (i=1; i<=nparam; i++)  *(top-i+2) = *(top-i); top += 2; tag(top-nparam-1) = T_MARK; *(top-nparam-2) = func; return (lua_execute (startcode));}/*** Get a parameter, returning the object handle or NULL on error.** 'number' must be 1 to get the first parameter.*/Object *lua_getparam (int number){ if (number <= 0 || number > top-base) return NULL; return (base+number-1);}/*** Given an object handle, return its number value. On error, return 0.0.*/real lua_getnumber (Object *object){ if (tonumber (object)) return 0.0; else                   return (nvalue(object));}/*** Given an object handle, return its string pointer. On error, return NULL.*/char *lua_getstring (Object *object){ if (tostring (object)) return NULL; else                   return (svalue(object));}/*** Given an object handle, return a copy of its string. On error, return NULL.*/char *lua_copystring (Object *object){ if (tostring (object)) return NULL; else                   return (strdup(svalue(object)));}/*** Given an object handle, return its cfuntion pointer. On error, return NULL.*/lua_CFunction lua_getcfunction (Object *object){ if (tag(object) != T_CFUNCTION) return NULL; else                            return (fvalue(object));}/*** Given an object handle, return its user data. On error, return NULL.*/void *lua_getuserdata (Object *object){ if (tag(object) != T_USERDATA) return NULL; else                           return (uvalue(object));}/*** Given an object handle and a field name, return its field object.** On error, return NULL.*/Object *lua_getfield (Object *object, char *field){ if (tag(object) != T_ARRAY)  return NULL; else {  Object ref;  tag(&ref) = T_STRING;  svalue(&ref) = lua_createstring(lua_strdup(field));  return (lua_hashdefine(avalue(object), &ref)); }}/*** Given an object handle and an index, return its indexed object.** On error, return NULL.*/Object *lua_getindexed (Object *object, float index){ if (tag(object) != T_ARRAY)  return NULL; else {  Object ref;  tag(&ref) = T_NUMBER;  nvalue(&ref) = index;  return (lua_hashdefine(avalue(object), &ref)); }}/*** Get a global object. Return the object handle or NULL on error.*/Object *lua_getglobal (char *name){ int n = lua_findsymbol(name); if (n < 0) return NULL; return &s_object(n);}/*** Pop and return an object*/Object *lua_pop (void){ if (top <= base) return NULL; top--; return top;}/*** Push a nil object*/int lua_pushnil (void){ if ((top-stack) >= MAXSTACK-1) {  lua_error ("stack overflow");  return 1; } tag(top) = T_NIL; return 0;}/*** Push an object (tag=number) to stack. Return 0 on success or 1 on error.*/int lua_pushnumber (real n){ if ((top-stack) >= MAXSTACK-1) {  lua_error ("stack overflow");  return 1; } tag(top) = T_NUMBER; nvalue(top++) = n; return 0;}/*** Push an object (tag=string) to stack. Return 0 on success or 1 on error.*/int lua_pushstring (char *s){ if ((top-stack) >= MAXSTACK-1) {  lua_error ("stack overflow");  return 1; } tag(top) = T_STRING;  svalue(top++) = lua_createstring(lua_strdup(s)); return 0;}/*** Push an object (tag=cfunction) to stack. Return 0 on success or 1 on error.*/int lua_pushcfunction (lua_CFunction fn){ if ((top-stack) >= MAXSTACK-1) {  lua_error ("stack overflow");  return 1; } tag(top) = T_CFUNCTION; fvalue(top++) = fn; return 0;}/*** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.*/int lua_pushuserdata (void *u){ if ((top-stack) >= MAXSTACK-1) {  lua_error ("stack overflow");  return 1; } tag(top) = T_USERDATA; uvalue(top++) = u; return 0;}/*** Push an object to stack.*/int lua_pushobject (Object *o){ if ((top-stack) >= MAXSTACK-1) {  lua_error ("stack overflow");  return 1; } *top++ = *o; return 0;}/*** Store top of the stack at a global variable array field. ** Return 1 on error, 0 on success.*/int lua_storeglobal (char *name){ int n = lua_findsymbol (name); if (n < 0) return 1; if (tag(top-1) == T_MARK) return 1; s_object(n) = *(--top); return 0;}/*** Store top of the stack at an array field. Return 1 on error, 0 on success.*/int lua_storefield (lua_Object object, char *field){ if (tag(object) != T_ARRAY)  return 1; else {  Object ref, *h;  tag(&ref) = T_STRING;  svalue(&ref) = lua_createstring(lua_strdup(field));  h = lua_hashdefine(avalue(object), &ref);  if (h == NULL) return 1;  if (tag(top-1) == T_MARK) return 1;  *h = *(--top); } return 0;}/*** Store top of the stack at an array index. Return 1 on error, 0 on success.*/int lua_storeindexed (lua_Object object, float index){ if (tag(object) != T_ARRAY)  return 1; else {  Object ref, *h;  tag(&ref) = T_NUMBER;  nvalue(&ref) = index;  h = lua_hashdefine(avalue(object), &ref);  if (h == NULL) return 1;  if (tag(top-1) == T_MARK) return 1;  *h = *(--top); } return 0;}/*** Given an object handle, return if it is nil.*/int lua_isnil (Object *object){ return (object != NULL && tag(object) == T_NIL);}/*** Given an object handle, return if it is a number one.*/int lua_isnumber (Object *object){ return (object != NULL && tag(object) == T_NUMBER);}/*** Given an object handle, return if it is a string one.*/int lua_isstring (Object *object){ return (object != NULL && tag(object) == T_STRING);}/*** Given an object handle, return if it is an array one.*/int lua_istable (Object *object){ return (object != NULL && tag(object) == T_ARRAY);}/*** Given an object handle, return if it is a cfunction one.*/int lua_iscfunction (Object *object){ return (object != NULL && tag(object) == T_CFUNCTION);}/*** Given an object handle, return if it is an user data one.*/int lua_isuserdata (Object *object){ return (object != NULL && tag(object) == T_USERDATA);}/*** Internal function: return an object type. */void lua_type (void){ Object *o = lua_getparam(1); lua_pushstring (lua_constant[tag(o)]);}/*** Internal function: convert an object to a number*/void lua_obj2number (void){ Object *o = lua_getparam(1); lua_pushobject (lua_convtonumber(o));}/*** Internal function: print object values*/void lua_print (void){ int i=1; void *obj; while ((obj=lua_getparam (i++)) != NULL) {  if      (lua_isnumber(obj))    printf("%g\n",lua_getnumber (obj));  else if (lua_isstring(obj))    printf("%s\n",lua_getstring (obj));  else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction (obj));  else if (lua_isuserdata(obj))  printf("userdata: %p\n",lua_getuserdata (obj));  else if (lua_istable(obj))     printf("table: %p\n",obj);  else if (lua_isnil(obj))       printf("nil\n");  else			         printf("invalid value to print\n"); }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -