diff options
Diffstat (limited to 'src/inout.c')
-rw-r--r-- | src/inout.c | 130 |
1 files changed, 78 insertions, 52 deletions
diff --git a/src/inout.c b/src/inout.c index c73030e3..85722c3c 100644 --- a/src/inout.c +++ b/src/inout.c @@ -5,31 +5,21 @@ ** Also provides some predefined lua functions. */ -char *rcs_inout="$Id: inout.c,v 2.25 1995/10/25 13:05:51 roberto Exp $"; +char *rcs_inout="$Id: inout.c,v 2.36 1996/03/19 22:28:37 roberto Exp $"; #include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include "mem.h" +#include "lex.h" #include "opcode.h" -#include "hash.h" #include "inout.h" #include "table.h" #include "tree.h" #include "lua.h" - - -#ifndef MAXFUNCSTACK -#define MAXFUNCSTACK 100 -#endif - -#define MAXMESSAGE MAXFUNCSTACK*80 +#include "mem.h" /* Exported variables */ Word lua_linenumber; -Bool lua_debug = 0; char *lua_parsedfile; @@ -54,9 +44,9 @@ static int stringinput (void) /* ** Function to open a file to be input unit. -** Return 0 on success or error message on error. +** Return the file. */ -char *lua_openfile (char *fn) +FILE *lua_openfile (char *fn) { lua_setinput (fileinput); if (fn == NULL) @@ -67,14 +57,10 @@ char *lua_openfile (char *fn) else fp = fopen (fn, "r"); if (fp == NULL) - { - static char buff[255]; - sprintf(buff, "unable to open file `%.200s'", fn); - return buff; - } + return NULL; lua_linenumber = 1; - lua_parsedfile = lua_constcreate(fn)->ts.str; - return NULL; + lua_parsedfile = luaI_createfixedstring(fn)->str; + return fp; } /* @@ -97,7 +83,7 @@ void lua_openstring (char *s) lua_setinput (stringinput); st = s; lua_linenumber = 1; - lua_parsedfile = lua_constcreate("(string)")->ts.str; + lua_parsedfile = luaI_createfixedstring("(string)")->str; } /* @@ -126,33 +112,57 @@ void lua_internaldostring (void) void lua_internaldofile (void) { lua_Object obj = lua_getparam (1); - if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj))) + char *fname = NULL; + if (lua_isstring(obj)) + fname = lua_getstring(obj); + else if (obj != LUA_NOOBJECT) + lua_error("invalid argument to function `dofile'"); + /* else fname = NULL */ + if (!lua_dofile(fname)) lua_pushnumber(1); else lua_pushnil(); } -/* -** Internal function: print object values -*/ -void lua_print (void) + +static char *tostring (lua_Object obj) { - int i=1; - lua_Object obj; - while ((obj=lua_getparam (i++)) != LUA_NOOBJECT) - { - if (lua_isnumber(obj)) printf("%g\n",lua_getnumber(obj)); - else if (lua_isstring(obj)) printf("%s\n",lua_getstring(obj)); - else if (lua_isfunction(obj)) printf("function: %p\n",(luaI_Address(obj))->value.tf); - 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",avalue(luaI_Address(obj))); - else if (lua_isnil(obj)) printf("nil\n"); - else printf("invalid value to print\n"); - } + char *buff = luaI_buffer(20); + if (lua_isstring(obj)) /* get strings and numbers */ + return lua_getstring(obj); + else switch(lua_type(obj)) + { + case LUA_T_FUNCTION: + sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf); + break; + case LUA_T_CFUNCTION: + sprintf(buff, "cfunction: %p", lua_getcfunction(obj)); + break; + case LUA_T_ARRAY: + sprintf(buff, "table: %p", avalue(luaI_Address(obj))); + break; + case LUA_T_NIL: + sprintf(buff, "nil"); + break; + default: + sprintf(buff, "userdata: %p", lua_getuserdata(obj)); + break; + } + return buff; } +void luaI_tostring (void) +{ + lua_pushstring(tostring(lua_getparam(1))); +} + +void luaI_print (void) +{ + int i = 1; + lua_Object obj; + while ((obj = lua_getparam(i++)) != LUA_NOOBJECT) + printf("%s\n", tostring(obj)); +} /* ** Internal function: return an object type. @@ -196,16 +206,7 @@ void lua_obj2number (void) { lua_Object o = lua_getparam(1); if (lua_isnumber(o)) - lua_pushobject(o); - else if (lua_isstring(o)) - { - char c; - float f; - if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1) - lua_pushnumber(f); - else - lua_pushnil(); - } + lua_pushnumber(lua_getnumber(o)); else lua_pushnil(); } @@ -218,3 +219,28 @@ void luaI_error (void) lua_error(s); } +void luaI_assert (void) +{ + lua_Object p = lua_getparam(1); + if (p == LUA_NOOBJECT || lua_isnil(p)) + lua_error("assertion failed!"); +} + +void luaI_setglobal (void) +{ + lua_Object name = lua_getparam(1); + lua_Object value = lua_getparam(2); + if (!lua_isstring(name)) + lua_error("incorrect argument to function `setglobal'"); + lua_pushobject(value); + lua_storeglobal(lua_getstring(name)); + lua_pushobject(value); /* return given value */ +} + +void luaI_getglobal (void) +{ + lua_Object name = lua_getparam(1); + if (!lua_isstring(name)) + lua_error("incorrect argument to function `getglobal'"); + lua_pushobject(lua_getglobal(lua_getstring(name))); +} |