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

📄 opcode.c

📁 lua解释器早期1.0版本
💻 C
📖 第 1 页 / 共 2 页
字号:
/*** opcode.c** TecCGraf - PUC-Rio** 26 Apr 93*/#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef __GNUC__#include <floatingpoint.h>#endif#include "opcode.h"#include "hash.h"#include "inout.h"#include "table.h"#include "lua.h"#define tonumber(o) ((tag(o) != T_NUMBER) && (lua_tonumber(o) != 0))#define tostring(o) ((tag(o) != T_STRING) && (lua_tostring(o) != 0))#ifndef MAXSTACK#define MAXSTACK 256#endifstatic Object stack[MAXSTACK] = {{T_MARK, {NULL}}};static Object *top=stack+1, *base=stack+1;/*** Concatenate two given string, creating a mark space at the beginning.** Return the new string pointer.*/static char *lua_strconc (char *l, char *r){ char *s = calloc (strlen(l)+strlen(r)+2, sizeof(char)); if (s == NULL) {  lua_error ("not enough memory");  return NULL; } *s++ = 0; 			/* create mark space */ return strcat(strcpy(s,l),r);}/*** Duplicate a string,  creating a mark space at the beginning.** Return the new string pointer.*/char *lua_strdup (char *l){ char *s = calloc (strlen(l)+2, sizeof(char)); if (s == NULL) {  lua_error ("not enough memory");  return NULL; } *s++ = 0; 			/* create mark space */ return strcpy(s,l);}/*** Convert, if possible, to a number tag.** Return 0 in success or not 0 on error.*/ static int lua_tonumber (Object *obj){ char *ptr; if (tag(obj) != T_STRING) {  lua_reportbug ("unexpected type at conversion to number");  return 1; } nvalue(obj) = strtod(svalue(obj), &ptr); if (*ptr) {  lua_reportbug ("string to number convertion failed");  return 2; } tag(obj) = T_NUMBER; return 0;}/*** Test if is possible to convert an object to a number one.** If possible, return the converted object, otherwise return nil object.*/ static Object *lua_convtonumber (Object *obj){ static Object cvt;  if (tag(obj) == T_NUMBER) {  cvt = *obj;  return &cvt; }   tag(&cvt) = T_NIL; if (tag(obj) == T_STRING) {  char *ptr;  nvalue(&cvt) = strtod(svalue(obj), &ptr);  if (*ptr == 0)   tag(&cvt) = T_NUMBER; } return &cvt;}/*** Convert, if possible, to a string tag** Return 0 in success or not 0 on error.*/ static int lua_tostring (Object *obj){ static char s[256]; if (tag(obj) != T_NUMBER) {  lua_reportbug ("unexpected type at conversion to string");  return 1; } if ((int) nvalue(obj) == nvalue(obj))  sprintf (s, "%d", (int) nvalue(obj)); else  sprintf (s, "%g", nvalue(obj)); svalue(obj) = lua_createstring(lua_strdup(s)); if (svalue(obj) == NULL)  return 1; tag(obj) = T_STRING; return 0;}/*** Execute the given opcode. Return 0 in success or 1 on error.*/int lua_execute (Byte *pc){ while (1) {  switch ((OpCode)*pc++)  {   case NOP: break;      case PUSHNIL: tag(top++) = T_NIL; break;      case PUSH0: tag(top) = T_NUMBER; nvalue(top++) = 0; break;   case PUSH1: tag(top) = T_NUMBER; nvalue(top++) = 1; break;   case PUSH2: tag(top) = T_NUMBER; nvalue(top++) = 2; break;   case PUSHBYTE: tag(top) = T_NUMBER; nvalue(top++) = *pc++; break;      case PUSHWORD:     tag(top) = T_NUMBER; nvalue(top++) = *((Word *)(pc)); pc += sizeof(Word);   break;      case PUSHFLOAT:    tag(top) = T_NUMBER; nvalue(top++) = *((float *)(pc)); pc += sizeof(float);   break;   case PUSHSTRING:   {    int w = *((Word *)(pc));    pc += sizeof(Word);    tag(top) = T_STRING; svalue(top++) = lua_constant[w];   }   break;      case PUSHLOCAL0: *top++ = *(base + 0); break;   case PUSHLOCAL1: *top++ = *(base + 1); break;   case PUSHLOCAL2: *top++ = *(base + 2); break;   case PUSHLOCAL3: *top++ = *(base + 3); break;   case PUSHLOCAL4: *top++ = *(base + 4); break;   case PUSHLOCAL5: *top++ = *(base + 5); break;   case PUSHLOCAL6: *top++ = *(base + 6); break;   case PUSHLOCAL7: *top++ = *(base + 7); break;   case PUSHLOCAL8: *top++ = *(base + 8); break;   case PUSHLOCAL9: *top++ = *(base + 9); break;      case PUSHLOCAL: *top++ = *(base + (*pc++)); break;      case PUSHGLOBAL:     *top++ = s_object(*((Word *)(pc))); pc += sizeof(Word);   break;      case PUSHINDEXED:    --top;    if (tag(top-1) != T_ARRAY)    {     lua_reportbug ("indexed expression not a table");     return 1;    }    {     Object *h = lua_hashdefine (avalue(top-1), top);     if (h == NULL) return 1;     *(top-1) = *h;    }   break;      case PUSHMARK: tag(top++) = T_MARK; break;      case PUSHOBJECT: *top = *(top-3); top++; break;      case STORELOCAL0: *(base + 0) = *(--top); break;   case STORELOCAL1: *(base + 1) = *(--top); break;   case STORELOCAL2: *(base + 2) = *(--top); break;   case STORELOCAL3: *(base + 3) = *(--top); break;   case STORELOCAL4: *(base + 4) = *(--top); break;   case STORELOCAL5: *(base + 5) = *(--top); break;   case STORELOCAL6: *(base + 6) = *(--top); break;   case STORELOCAL7: *(base + 7) = *(--top); break;   case STORELOCAL8: *(base + 8) = *(--top); break;   case STORELOCAL9: *(base + 9) = *(--top); break;       case STORELOCAL: *(base + (*pc++)) = *(--top); break;      case STOREGLOBAL:    s_object(*((Word *)(pc))) = *(--top); pc += sizeof(Word);   break;   case STOREINDEXED0:    if (tag(top-3) != T_ARRAY)    {     lua_reportbug ("indexed expression not a table");     return 1;    }    {     Object *h = lua_hashdefine (avalue(top-3), top-2);     if (h == NULL) return 1;     *h = *(top-1);    }    top -= 3;   break;      case STOREINDEXED:   {    int n = *pc++;    if (tag(top-3-n) != T_ARRAY)    {     lua_reportbug ("indexed expression not a table");     return 1;    }    {     Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);     if (h == NULL) return 1;     *h = *(top-1);    }    --top;   }   break;      case STOREFIELD:    if (tag(top-3) != T_ARRAY)    {     lua_error ("internal error - table expected");     return 1;    }    *(lua_hashdefine (avalue(top-3), top-2)) = *(top-1);    top -= 2;   break;      case ADJUST:   {    Object *newtop = base + *(pc++);    if (top != newtop)    {     while (top < newtop) tag(top++) = T_NIL;     top = newtop;    }   }   break;      case CREATEARRAY:    if (tag(top-1) == T_NIL)      nvalue(top-1) = 101;    else     {     if (tonumber(top-1)) return 1;     if (nvalue(top-1) <= 0) nvalue(top-1) = 101;    }    avalue(top-1) = lua_createarray(lua_hashcreate(nvalue(top-1)));    if (avalue(top-1) == NULL)     return 1;    tag(top-1) = T_ARRAY;   break;      case EQOP:   {    Object *l = top-2;    Object *r = top-1;    --top;    if (tag(l) != tag(r))      tag(top-1) = T_NIL;    else    {     switch (tag(l))     {      case T_NIL:       tag(top-1) = T_NUMBER; break;      case T_NUMBER:    tag(top-1) = (nvalue(l) == nvalue(r)) ? T_NUMBER : T_NIL; break;      case T_ARRAY:     tag(top-1) = (avalue(l) == avalue(r)) ? T_NUMBER : T_NIL; break;      case T_FUNCTION:  tag(top-1) = (bvalue(l) == bvalue(r)) ? T_NUMBER : T_NIL; break;      case T_CFUNCTION: tag(top-1) = (fvalue(l) == fvalue(r)) ? T_NUMBER : T_NIL; break;      case T_USERDATA:  tag(top-1) = (uvalue(l) == uvalue(r)) ? T_NUMBER : T_NIL; break;      case T_STRING:    tag(top-1) = (strcmp (svalue(l), svalue(r)) == 0) ? T_NUMBER : T_NIL; break;      case T_MARK:      return 1;     }    }    nvalue(top-1) = 1;   }   break;       case LTOP:   {    Object *l = top-2;    Object *r = top-1;    --top;    if (tag(l) == T_NUMBER && tag(r) == T_NUMBER)     tag(top-1) = (nvalue(l) < nvalue(r)) ? T_NUMBER : T_NIL;    else    {     if (tostring(l) || tostring(r))      return 1;     tag(top-1) = (strcmp (svalue(l), svalue(r)) < 0) ? T_NUMBER : T_NIL;    }    nvalue(top-1) = 1;    }   break;      case LEOP:   {    Object *l = top-2;    Object *r = top-1;    --top;    if (tag(l) == T_NUMBER && tag(r) == T_NUMBER)     tag(top-1) = (nvalue(l) <= nvalue(r)) ? T_NUMBER : T_NIL;    else    {     if (tostring(l) || tostring(r))      return 1;     tag(top-1) = (strcmp (svalue(l), svalue(r)) <= 0) ? T_NUMBER : T_NIL;    }    nvalue(top-1) = 1;    }   break;      case ADDOP:   {    Object *l = top-2;    Object *r = top-1;    if (tonumber(r) || tonumber(l))     return 1;    nvalue(l) += nvalue(r);    --top;   }   break;       case SUBOP:   {    Object *l = top-2;    Object *r = top-1;    if (tonumber(r) || tonumber(l))     return 1;    nvalue(l) -= nvalue(r);    --top;   }   break;       case MULTOP:   {    Object *l = top-2;    Object *r = top-1;    if (tonumber(r) || tonumber(l))     return 1;    nvalue(l) *= nvalue(r);    --top;   }   break;       case DIVOP:   {    Object *l = top-2;    Object *r = top-1;    if (tonumber(r) || tonumber(l))     return 1;    nvalue(l) /= nvalue(r);    --top;   }   break;       case CONCOP:   {    Object *l = top-2;    Object *r = top-1;    if (tostring(r) || tostring(l))     return 1;    svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));    if (svalue(l) == NULL)     return 1;    --top;   }   break;       case MINUSOP:    if (tonumber(top-1))     return 1;    nvalue(top-1) = - nvalue(top-1);   break;       case NOTOP:    tag(top-1) = tag(top-1) == T_NIL ? T_NUMBER : T_NIL;   break;       case ONTJMP:   {    int n = *((Word *)(pc));    pc += sizeof(Word);    if (tag(top-1) != T_NIL) pc += n;   }   break;      case ONFJMP:	      {    int n = *((Word *)(pc));    pc += sizeof(Word);    if (tag(top-1) == T_NIL) pc += n;   }   break;      case JMP: pc += *((Word *)(pc)) + sizeof(Word); break;       case UPJMP: pc -= *((Word *)(pc)) - sizeof(Word); break;       case IFFJMP:   {    int n = *((Word *)(pc));    pc += sizeof(Word);    top--;    if (tag(top) == T_NIL) pc += n;   }   break;   case IFFUPJMP:   {    int n = *((Word *)(pc));    pc += sizeof(Word);    top--;    if (tag(top) == T_NIL) pc -= n;   }   break;   case POP: --top; break;      case CALLFUNC:   {    Byte *newpc;    Object *b = top-1;    while (tag(b) != T_MARK) b--;    if (tag(b-1) == T_FUNCTION)    {     lua_debugline = 0;			/* always reset debug flag */     newpc = bvalue(b-1);     bvalue(b-1) = pc;		        /* store return code */     nvalue(b) = (base-stack);		/* store base value */     base = b+1;     pc = newpc;     if (MAXSTACK-(base-stack) < STACKGAP)     {

⌨️ 快捷键说明

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