aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWaldemar Celes <celes@tecgraf.puc-rio.br>1993-12-17 16:41:19 -0200
committerWaldemar Celes <celes@tecgraf.puc-rio.br>1993-12-17 16:41:19 -0200
commit64097041c48070deb2e9d88c55efb41ba77cdd9b (patch)
tree45f4d62fdb4c42a0020c233010304e5bd3468606
parent75ed5043820e07eeb6405e42ac3c9cbad990466d (diff)
downloadlua-64097041c48070deb2e9d88c55efb41ba77cdd9b.tar.gz
lua-64097041c48070deb2e9d88c55efb41ba77cdd9b.tar.bz2
lua-64097041c48070deb2e9d88c55efb41ba77cdd9b.zip
LUA intermediate code interpreter
-rw-r--r--opcode.c41
-rw-r--r--opcode.h5
2 files changed, 42 insertions, 4 deletions
diff --git a/opcode.c b/opcode.c
index 97975ba1..ab470fb8 100644
--- a/opcode.c
+++ b/opcode.c
@@ -1,9 +1,10 @@
1/* 1/*
2** opcode.c 2** opcode.c
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4** 26 Apr 93
5*/ 4*/
6 5
6char *rcs_opcode="$Id: $";
7
7#include <stdio.h> 8#include <stdio.h>
8#include <stdlib.h> 9#include <stdlib.h>
9#include <string.h> 10#include <string.h>
@@ -137,6 +138,8 @@ static int lua_tostring (Object *obj)
137*/ 138*/
138int lua_execute (Byte *pc) 139int lua_execute (Byte *pc)
139{ 140{
141 Object *oldbase = base;
142 base = top;
140 while (1) 143 while (1)
141 { 144 {
142 switch ((OpCode)*pc++) 145 switch ((OpCode)*pc++)
@@ -252,7 +255,7 @@ int lua_execute (Byte *pc)
252 case STOREFIELD: 255 case STOREFIELD:
253 if (tag(top-3) != T_ARRAY) 256 if (tag(top-3) != T_ARRAY)
254 { 257 {
255 lua_error ("internal error - table expected"); 258 lua_reportbug ("internal error - table expected");
256 return 1; 259 return 1;
257 } 260 }
258 *(lua_hashdefine (avalue(top-3), top-2)) = *(top-1); 261 *(lua_hashdefine (avalue(top-3), top-2)) = *(top-1);
@@ -516,6 +519,7 @@ int lua_execute (Byte *pc)
516 break; 519 break;
517 520
518 case HALT: 521 case HALT:
522 base = oldbase;
519 return 0; /* success */ 523 return 0; /* success */
520 524
521 case SETFUNCTION: 525 case SETFUNCTION:
@@ -577,6 +581,7 @@ int lua_dostring (char *string)
577{ 581{
578 if (lua_openstring (string)) return 1; 582 if (lua_openstring (string)) return 1;
579 if (lua_parse ()) return 1; 583 if (lua_parse ()) return 1;
584 lua_closestring();
580 return 0; 585 return 0;
581} 586}
582 587
@@ -612,6 +617,7 @@ Object *lua_getparam (int number)
612*/ 617*/
613real lua_getnumber (Object *object) 618real lua_getnumber (Object *object)
614{ 619{
620 if (object == NULL || tag(object) == T_NIL) return 0.0;
615 if (tonumber (object)) return 0.0; 621 if (tonumber (object)) return 0.0;
616 else return (nvalue(object)); 622 else return (nvalue(object));
617} 623}
@@ -621,6 +627,7 @@ real lua_getnumber (Object *object)
621*/ 627*/
622char *lua_getstring (Object *object) 628char *lua_getstring (Object *object)
623{ 629{
630 if (object == NULL || tag(object) == T_NIL) return NULL;
624 if (tostring (object)) return NULL; 631 if (tostring (object)) return NULL;
625 else return (svalue(object)); 632 else return (svalue(object));
626} 633}
@@ -630,6 +637,7 @@ char *lua_getstring (Object *object)
630*/ 637*/
631char *lua_copystring (Object *object) 638char *lua_copystring (Object *object)
632{ 639{
640 if (object == NULL || tag(object) == T_NIL) return NULL;
633 if (tostring (object)) return NULL; 641 if (tostring (object)) return NULL;
634 else return (strdup(svalue(object))); 642 else return (strdup(svalue(object)));
635} 643}
@@ -639,6 +647,7 @@ char *lua_copystring (Object *object)
639*/ 647*/
640lua_CFunction lua_getcfunction (Object *object) 648lua_CFunction lua_getcfunction (Object *object)
641{ 649{
650 if (object == NULL) return NULL;
642 if (tag(object) != T_CFUNCTION) return NULL; 651 if (tag(object) != T_CFUNCTION) return NULL;
643 else return (fvalue(object)); 652 else return (fvalue(object));
644} 653}
@@ -648,6 +657,7 @@ lua_CFunction lua_getcfunction (Object *object)
648*/ 657*/
649void *lua_getuserdata (Object *object) 658void *lua_getuserdata (Object *object)
650{ 659{
660 if (object == NULL) return NULL;
651 if (tag(object) != T_USERDATA) return NULL; 661 if (tag(object) != T_USERDATA) return NULL;
652 else return (uvalue(object)); 662 else return (uvalue(object));
653} 663}
@@ -658,6 +668,7 @@ void *lua_getuserdata (Object *object)
658*/ 668*/
659Object *lua_getfield (Object *object, char *field) 669Object *lua_getfield (Object *object, char *field)
660{ 670{
671 if (object == NULL) return NULL;
661 if (tag(object) != T_ARRAY) 672 if (tag(object) != T_ARRAY)
662 return NULL; 673 return NULL;
663 else 674 else
@@ -675,6 +686,7 @@ Object *lua_getfield (Object *object, char *field)
675*/ 686*/
676Object *lua_getindexed (Object *object, float index) 687Object *lua_getindexed (Object *object, float index)
677{ 688{
689 if (object == NULL) return NULL;
678 if (tag(object) != T_ARRAY) 690 if (tag(object) != T_ARRAY)
679 return NULL; 691 return NULL;
680 else 692 else
@@ -931,3 +943,28 @@ void lua_print (void)
931 } 943 }
932} 944}
933 945
946/*
947** Internal function: do a file
948*/
949void lua_internaldofile (void)
950{
951 lua_Object obj = lua_getparam (1);
952 if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
953 lua_pushnumber(1);
954 else
955 lua_pushnil();
956}
957
958/*
959** Internal function: do a string
960*/
961void lua_internaldostring (void)
962{
963 lua_Object obj = lua_getparam (1);
964 if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
965 lua_pushnumber(1);
966 else
967 lua_pushnil();
968}
969
970
diff --git a/opcode.h b/opcode.h
index b32969d5..e7d505f3 100644
--- a/opcode.h
+++ b/opcode.h
@@ -1,7 +1,6 @@
1/* 1/*
2** opcode.h
3** TeCGraf - PUC-Rio 2** TeCGraf - PUC-Rio
4** 16 Apr 92 3** $Id: $
5*/ 4*/
6 5
7#ifndef opcode_h 6#ifndef opcode_h
@@ -140,5 +139,7 @@ int lua_parse (void); /* from "lua.stx" module */
140void lua_type (void); 139void lua_type (void);
141void lua_obj2number (void); 140void lua_obj2number (void);
142void lua_print (void); 141void lua_print (void);
142void lua_internaldofile (void);
143void lua_internaldostring (void);
143 144
144#endif 145#endif