Exemplo n.º 1
0
        private static void io_write()
        {
            lua_Object o1 = lua_getparam(1);
            lua_Object o2 = lua_getparam(2);

            if (o1 == null)                             /* new line */
            {
                fprintf(@out, "\n");
                lua_pushnumber(1);
            }
            else if (o2 == null)                        /* free format */
            {
                int status = 0;
                if (lua_isnumber(o1) != 0)
                {
                    status = fprintf(@out, "%g", lua_getnumber(o1));
                }
                else if (lua_isstring(o1) != 0)
                {
                    status = fprintf(@out, "%s", lua_getstring(o1));
                }
                lua_pushnumber(status);
            }
            else                                                /* formated */
            {
                if (lua_isstring(o2) == 0)
                {
                    lua_error("incorrect format to function `write'");
                    lua_pushnumber(0);
                    return;
                }
                lua_pushnumber(fprintf(@out, "%s", buildformat(lua_getstring(o2), o1)));
            }
        }
Exemplo n.º 2
0
        /*
        ** Internal function: convert an object to a number
        */
        public static void lua_obj2number()
        {
            lua_Object o = lua_getparam(1);

            if (0 != lua_isnumber(o))
            {
                lua_pushobject(o);
            }
            else if (0 != lua_isstring(o))
            {
                char  c = (char)0;
                float f = 0;
                if (sscanf(lua_getstring(o), "%f %c", f, c) == 1)
                {
                    lua_pushnumber(f);
                }
                else
                {
                    lua_pushnil();
                }
            }
            else
            {
                lua_pushnil();
            }
        }
Exemplo n.º 3
0
        public static void lua_next()
        {
            Hash       t;
            lua_Object o = lua_getparam(1);
            lua_Object r = lua_getparam(2);

            if (o == LUA_NOOBJECT || r == LUA_NOOBJECT)
            {
                lua_error("too few arguments to function `next'");
            }
            if (lua_getparam(3) != LUA_NOOBJECT)
            {
                lua_error("too many arguments to function `next'");
            }
            if (0 == lua_istable(o))
            {
                lua_error("first argument of function `next' is not a table");
            }
            t = avalue(luaI_Address(o));
            if (0 != lua_isnil(r))
            {
                hashnext(t, 0);
            }
            else
            {
                Word h = present(t, luaI_Address(r));
                hashnext(t, (Word)(h + 1));
            }
        }
Exemplo n.º 4
0
        /*
        ** Return the substring of a string, from start to end
        ** LUA interface:
        **			substring = strsub (string, start, end)
        */
        private static void str_sub()
        {
            int        start, end;
            CharPtr    s;
            lua_Object o1 = lua_getparam(1);
            lua_Object o2 = lua_getparam(2);
            lua_Object o3 = lua_getparam(3);

            if (lua_isstring(o1) == 0 || lua_isnumber(o2) == 0 || lua_isnumber(o3) == 0)
            {
                lua_error("incorrect arguments to function `strsub'"); return;
            }
            s     = strdup(lua_getstring(o1));
            start = (int)lua_getnumber(o2);
            end   = (int)lua_getnumber(o3);
            if (end < start || start < 1 || end > strlen(s))
            {
                lua_pushstring("");
            }
            else
            {
                s[end] = '\0';                             //FIXME:s = s.Substring(0, end);
                lua_pushstring(new CharPtr(s, start - 1)); //s.Substring(start - 1));
            }
            free(s);
        }
Exemplo n.º 5
0
        /*
        ** Return the substring of a string, from start to end
        ** LUA interface:
        **			substring = strsub (string, start, end)
        */
        private static void str_sub()
        {
            int        start, end;
            CharPtr    s;
            lua_Object o1 = lua_getparam(1);
            lua_Object o2 = lua_getparam(2);
            lua_Object o3 = lua_getparam(3);

            if (0 == lua_isstring(o1) || 0 == lua_isnumber(o2))
            {
                lua_error("incorrect arguments to function `strsub'");
            }
            if (o3 != LUA_NOOBJECT && 0 == lua_isnumber(o3))
            {
                lua_error("incorrect third argument to function `strsub'");
            }
            s     = newstring(o1);
            start = (int)lua_getnumber(o2);
            end   = o3 == LUA_NOOBJECT ? (int)strlen(s) : (int)lua_getnumber(o3);
            if (end < start || start < 1 || end > strlen(s))
            {
                lua_pushliteral("");
            }
            else
            {
                s[end] = '\0';
                lua_pushstring(s.add(start - 1));
            }
            free(s);
        }
Exemplo n.º 6
0
        /*
        ** Return the substring of a string, from start to end
        ** LUA interface:
        **			substring = strsub (string, start, end)
        */
        private static void str_sub()
        {
            int        start, end;
            CharPtr    s;
            lua_Object o1 = lua_getparam(1);
            lua_Object o2 = lua_getparam(2);
            lua_Object o3 = lua_getparam(3);

            if (lua_isstring(o1) == 0 || lua_isnumber(o2) == 0)
            {
                lua_error("incorrect arguments to function `strsub'"); return;
            }
            if (o3 != null && lua_isnumber(o3) == 0)
            {
                lua_error("incorrect third argument to function `strsub'"); return;
            }
            s     = lua_copystring(o1);
            start = (int)lua_getnumber(o2);
            end   = o3 == null ? (int)strlen(s) : (int)lua_getnumber(o3);
            if (end < start || start < 1 || end > strlen(s))
            {
                lua_pushstring("");
            }
            else
            {
                s[end] = '\0';
                lua_pushstring(s.add(start - 1));
            }
            free(s);
        }
Exemplo n.º 7
0
        private static void execstr()
        {
            lua_Object obj = lua_getparam(1);

            if (lua_isstring(obj) != 0)
            {
                lua_dostring(lua_getstring(obj));
            }
        }
Exemplo n.º 8
0
        private static void callfunc()
        {
            lua_Object obj = lua_getparam(1);

            if (lua_isstring(obj) != 0)
            {
                lua_call(lua_getstring(obj), 0);
            }
        }
Exemplo n.º 9
0
        /*
        ** Return the string length
        ** LUA interface:
        **			n = strlen (string)
        */
        private static void str_len()
        {
            lua_Object o = lua_getparam(1);

            if (lua_isstring(o) == 0)
            {
                lua_error("incorrect arguments to function `strlen'"); return;
            }
            lua_pushnumber(strlen(lua_getstring(o)));
        }
Exemplo n.º 10
0
        /*
        ** Return the position of the first caracter of a substring into a string
        ** LUA interface:
        **			n = strfind (string, substring, init, end)
        */
        private static void str_find()
        {
            CharPtr    s1, s2, f;
            int        init;
            lua_Object o1 = lua_getparam(1);
            lua_Object o2 = lua_getparam(2);
            lua_Object o3 = lua_getparam(3);
            lua_Object o4 = lua_getparam(4);

            if (0 == lua_isstring(o1) || 0 == lua_isstring(o2))
            {
                lua_error("incorrect arguments to function `strfind'");
            }
            if (o3 == LUA_NOOBJECT)
            {
                init = 0;
            }
            else if (0 != lua_isnumber(o3))
            {
                init = (int)(lua_getnumber(o3) - 1);
            }
            else
            {
                lua_error("incorrect arguments to function `strfind'");
                return;                  /* to avoid warnings */
            }
            s1 = lua_getstring(o1);
            s2 = lua_getstring(o2);
            f  = strstr(s1 + init, s2);
            if (f != null)
            {
                int pos = f - s1 + 1;
                if (o4 == LUA_NOOBJECT)
                {
                    lua_pushnumber(pos);
                }
                else if (0 == lua_isnumber(o4))
                {
                    lua_error("incorrect arguments to function `strfind'");
                }
                else if ((int)lua_getnumber(o4) >= pos + strlen(s2) - 1)
                {
                    lua_pushnumber(pos);
                }
                else
                {
                    lua_pushnil();
                }
            }
            else
            {
                lua_pushnil();
            }
        }
Exemplo n.º 11
0
        //char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $";

        //#include <string.h>
        //#include <stdlib.h>
        //#include <ctype.h>

        //#include "lua.h"
        //#include "lualib.h"


        private static CharPtr newstring(lua_Object o)
        {
            CharPtr s  = lua_getstring(o);
            CharPtr ns = malloc(strlen(s) + 1);

            if (ns == null)
            {
                lua_error("not enough memory for new string");
            }
            strcpy(ns, s);
            return(ns);
        }
Exemplo n.º 12
0
        /*
        ** Internal function: do a string
        */
        public static void lua_internaldostring()
        {
            lua_Object obj = lua_getparam(1);

            if (0 != lua_isstring(obj) && 0 == lua_dostring(lua_getstring(obj)))
            {
                lua_pushnumber(1);
            }
            else
            {
                lua_pushnil();
            }
        }
Exemplo n.º 13
0
        private static void math_mod()
        {
            int        d1, d2;
            lua_Object o1 = lua_getparam(1);
            lua_Object o2 = lua_getparam(2);

            if (0 == lua_isnumber(o1) || 0 == lua_isnumber(o2))
            {
                lua_error("incorrect arguments to function `mod'"); return;
            }
            d1 = (int)lua_getnumber(o1);
            d2 = (int)lua_getnumber(o2);
            lua_pushnumber(d1 % d2);
        }
Exemplo n.º 14
0
        /*
        ** Open a file to write appended.
        ** LUA interface:
        **			status = appendto (filename)
        ** where:
        **			status = 2 -> success (already exist)
        **			status = 1 -> success (new file)
        **			status = 0 -> error
        */
        private static void io_appendto()
        {
            lua_Object o = lua_getparam(1);

            if (o == null)                      /* restore standart output */
            {
                if (@out != stdout)
                {
                    fclose(@out);
                    @out = stdout;
                }
                lua_pushnumber(1);
            }
            else
            {
                if (0 == lua_isstring(o))
                {
                    lua_error("incorrect argument to function 'appendto`");
                    lua_pushnumber(0);
                }
                else
                {
                    int         r;
                    FILE        fp;
                    stat_struct st = new stat_struct();
                    if (stat(lua_getstring(o), st) == -1)
                    {
                        r = 1;
                    }
                    else
                    {
                        r = 2;
                    }
                    fp = fopen(lua_getstring(o), "a");
                    if (fp == null)
                    {
                        lua_pushnumber(0);
                    }
                    else
                    {
                        if (@out != stdout)
                        {
                            fclose(@out);
                        }
                        @out = fp;
                        lua_pushnumber(r);
                    }
                }
            }
        }
Exemplo n.º 15
0
        private static void math_pow()
        {
            double     d1, d2;
            lua_Object o1 = lua_getparam(1);
            lua_Object o2 = lua_getparam(2);

            if (lua_isnumber(o1) == 0 || lua_isnumber(o2) == 0)
            {
                lua_error("incorrect arguments to function `pow'"); return;
            }
            d1 = lua_getnumber(o1);
            d2 = lua_getnumber(o2);
            lua_pushnumber((real)pow(d1, d2));
        }
Exemplo n.º 16
0
        private static void math_tan()
        {
            double     d;
            lua_Object o = lua_getparam(1);

            if (o == null)
            {
                lua_error("too few arguments to function `tan'"); return;
            }
            if (0 == lua_isnumber(o))
            {
                lua_error("incorrect arguments to function `tan'"); return;
            }
            d = lua_getnumber(o);
            lua_pushnumber((real)tan(TORAD(d)));
        }
Exemplo n.º 17
0
        private static void math_cos()
        {
            double     d;
            lua_Object o = lua_getparam(1);

            if (o == LUA_NOOBJECT)
            {
                lua_error("too few arguments to function `cos'");
            }
            if (0 == lua_isnumber(o))
            {
                lua_error("incorrect arguments to function `cos'");
            }
            d = lua_getnumber(o);
            lua_pushnumber((real)cos(TORAD(d)));
        }
Exemplo n.º 18
0
        internal static void math_sqrt()
        {
            double     d;
            lua_Object o = lua_getparam(1);

            if (o == null)
            {
                lua_error("too few arguments to function `sqrt'"); return;
            }
            if (0 == lua_isnumber(o))
            {
                lua_error("incorrect arguments to function `sqrt'"); return;
            }
            d = lua_getnumber(o);
            lua_pushnumber((real)sqrt(d));
        }
Exemplo n.º 19
0
        private static void math_rad()
        {
            float      d;
            lua_Object o = lua_getparam(1);

            if (o == LUA_NOOBJECT)
            {
                lua_error("too few arguments to function `rad'");
            }
            if (0 == lua_isnumber(o))
            {
                lua_error("incorrect arguments to function `rad'");
            }
            d = lua_getnumber(o);
            lua_pushnumber((real)(d / 180.0 * PI));
        }
Exemplo n.º 20
0
        /*
        ** Return the position of the first caracter of a substring into a string
        ** LUA interface:
        **			n = strfind (string, substring)
        */
        private static void str_find()
        {
            int        n;
            CharPtr    s1, s2;
            lua_Object o1 = lua_getparam(1);
            lua_Object o2 = lua_getparam(2);

            if (lua_isstring(o1) == 0 || lua_isstring(o2) == 0)
            {
                lua_error("incorrect arguments to function `strfind'"); return;
            }
            s1 = lua_getstring(o1);
            s2 = lua_getstring(o2);
            n  = strstr(s1, s2) - s1 + 1;
            lua_pushnumber(n);
        }
Exemplo n.º 21
0
        /*
        ** Execute a executable program using "sustem".
        ** On error put 0 on stack, otherwise put 1.
        */
        public static void io_execute()
        {
            lua_Object o = lua_getparam(1);

            if (o == null || lua_isstring(o) == 0)
            {
                lua_error("incorrect argument to function 'execute`");
                lua_pushnumber(0);
            }
            else
            {
                system(lua_getstring(o));
                lua_pushnumber(1);
            }
            return;
        }
Exemplo n.º 22
0
        /*
        ** Convert a string to lower case.
        ** LUA interface:
        **			lowercase = strlower (string)
        */
        private static void str_lower()
        {
            CharPtr    s, c;
            lua_Object o = lua_getparam(1);

            if (lua_isstring(o) == 0)
            {
                lua_error("incorrect arguments to function `strlower'"); return;
            }
            s = lua_getstring(o); c = new CharPtr(s);
            while (c[0] != '\0')
            {
                c[0] = tolower(c[0]);
                c.inc();
            }
            lua_pushstring(s);
            free(s);
        }
Exemplo n.º 23
0
        private static void math_abs()
        {
            double     d;
            lua_Object o = lua_getparam(1);

            if (o == null)
            {
                lua_error("too few arguments to function `abs'"); return;
            }
            if (0 == lua_isnumber(o))
            {
                lua_error("incorrect arguments to function `abs'"); return;
            }
            d = lua_getnumber(o);
            if (d < 0)
            {
                d = -d;
            }
            lua_pushnumber((real)d);
        }
Exemplo n.º 24
0
        //char *rcs_strlib="$Id: strlib.c,v 1.2 1994/03/28 15:14:02 celes Exp $";

        //#include <stdlib.h>
        //#include <string.h>
        //#include <ctype.h>

        //#include "mm.h"


        //#include "lua.h"

        /*
        ** Return the position of the first caracter of a substring into a string
        ** LUA interface:
        **			n = strfind (string, substring)
        */
        private static void str_find()
        {
            CharPtr    s1, s2, f;
            lua_Object o1 = lua_getparam(1);
            lua_Object o2 = lua_getparam(2);

            if (lua_isstring(o1) == 0 || lua_isstring(o2) == 0)
            {
                lua_error("incorrect arguments to function `strfind'"); return;
            }
            s1 = lua_getstring(o1);
            s2 = lua_getstring(o2);
            f  = strstr(s1, s2);
            if (f != null)
            {
                lua_pushnumber(f - s1 + 1);
            }
            else
            {
                lua_pushnil();
            }
        }
Exemplo n.º 25
0
        /*
        ** Open a file to read.
        ** LUA interface:
        **			status = readfrom (filename)
        ** where:
        **			status = 1 -> success
        **			status = 0 -> error
        */
        private static void io_readfrom()
        {
            lua_Object o = lua_getparam(1);

            if (o == null)                              /* restore standart input */
            {
                if (@in != stdin)
                {
                    fclose(@in);
                    @in = stdin;
                }
                lua_pushnumber(1);
            }
            else
            {
                if (lua_isstring(o) == 0)
                {
                    lua_error("incorrect argument to function 'readfrom`");
                    lua_pushnumber(0);
                }
                else
                {
                    FILE fp = fopen(lua_getstring(o), "r");
                    if (fp == null)
                    {
                        lua_pushnumber(0);
                    }
                    else
                    {
                        if (@in != stdin)
                        {
                            fclose(@in);
                        }
                        @in = fp;
                        lua_pushnumber(1);
                    }
                }
            }
        }
Exemplo n.º 26
0
        /*
        ** Internal function: return an object type.
        */
        public static void luaI_type()
        {
            lua_Object o = lua_getparam(1);

            if (o == LUA_NOOBJECT)
            {
                lua_error("no parameter to function 'type'");
            }
            switch (lua_type(o))
            {
            case (int)lua_Type.LUA_T_NIL:
                lua_pushliteral("nil");
                break;

            case (int)lua_Type.LUA_T_NUMBER:
                lua_pushliteral("number");
                break;

            case (int)lua_Type.LUA_T_STRING:
                lua_pushliteral("string");
                break;

            case (int)lua_Type.LUA_T_ARRAY:
                lua_pushliteral("table");
                break;

            case (int)lua_Type.LUA_T_FUNCTION:
                lua_pushliteral("function");
                break;

            case (int)lua_Type.LUA_T_CFUNCTION:
                lua_pushliteral("cfunction");
                break;

            default:
                lua_pushliteral("userdata");
                break;
            }
        }
Exemplo n.º 27
0
        /*
        ** Open a file to write.
        ** LUA interface:
        **			status = writeto (filename)
        ** where:
        **			status = 1 -> success
        **			status = 0 -> error
        */
        private static void io_writeto()
        {
            lua_Object o = lua_getparam(1);

            if (o == null)                              /* restore standart output */
            {
                if (@out != stdout)
                {
                    fclose(@out);
                    @out = stdout;
                }
                lua_pushnumber(1);
            }
            else
            {
                if (lua_isstring(o) == 0)
                {
                    lua_error("incorrect argument to function 'writeto`");
                    lua_pushnumber(0);
                }
                else
                {
                    FILE fp = fopen(lua_getstring(o), "w");
                    if (fp == null)
                    {
                        lua_pushnumber(0);
                    }
                    else
                    {
                        if (@out != stdout)
                        {
                            fclose(@out);
                        }
                        @out = fp;
                        lua_pushnumber(1);
                    }
                }
            }
        }
Exemplo n.º 28
0
        private static void math_pow()
        {
            lua_Object o1 = lua_getparam(1);
            lua_Object o2 = lua_getparam(2);
            lua_Object op = lua_getparam(3);

            if (0 == lua_isnumber(o1) || 0 == lua_isnumber(o2) || lua_getstring(op)[0] != 'p')
            {
                lua_Object old = lua_getlocked(old_pow);
                lua_pushobject(o1);
                lua_pushobject(o2);
                lua_pushobject(op);
                if (lua_callfunction(old) != 0)
                {
                    lua_error(null);
                }
            }
            else
            {
                double d1 = lua_getnumber(o1);
                double d2 = lua_getnumber(o2);
                lua_pushnumber((real)pow(d1, d2));
            }
        }
Exemplo n.º 29
0
 /*
 ** Store top of the stack at an array index. Return 1 on error, 0 on success.
 */
 public static int lua_storeindexed(lua_Object @object, float index)
 {
     if (tag(@object) != Type.T_ARRAY)
     {
         return(1);
     }
     else
     {
         Object_ @ref = new Object_(), h;
         tag(@ref, Type.T_NUMBER);
         nvalue(@ref, index);                 //FIXME:real->float
         h = lua_hashdefine(avalue(@object), @ref);
         if (h == null)
         {
             return(1);
         }
         if (tag(top.get(-1)) == Type.T_MARK)
         {
             return(1);
         }
         top.dec(); h.set(top.get());
     }
     return(0);
 }
Exemplo n.º 30
0
 /*
 ** Store top of the stack at an array field. Return 1 on error, 0 on success.
 */
 public static int lua_storefield(lua_Object @object, CharPtr field)
 {
     if (tag(@object) != Type.T_ARRAY)
     {
         return(1);
     }
     else
     {
         Object_ @ref = new Object_(), h;
         tag(@ref, Type.T_STRING);
         svalue(@ref, lua_createstring(lua_strdup(field)));
         h = lua_hashdefine(avalue(@object), @ref);
         if (h == null)
         {
             return(1);
         }
         if (tag(top.get(-1)) == Type.T_MARK)
         {
             return(1);
         }
         top.dec(); h.set(top.get());
     }
     return(0);
 }