C API example for stack operations in Lua

Source: Internet
Author: User


This is an example of LUA programming, a simple record.
#include <stdio.h> #include <lua5.2/lua.h> #include <lua5.2/lauxlib.h>static void Stackdump (Lua_    State *l) {int i;    int top = lua_gettop (L);        for (i = 1; I <= top; i++) {int t = Lua_type (L, i);                Switch (t) {case lua_tstring:printf ("'%s '", lua_tostring (L, i));            Break Case lua_tboolean:printf (Lua_toboolean (L, i)?                "True": "false");            Break                Case lua_tnumber:printf ("%g", Lua_tonumber (L, i));            Break                default:printf ("%s", Lua_typename (L, t));        Break    } printf (""); } printf ("\ n");}    int main (void) {Lua_state *l = Lual_newstate ();    Lua_pushboolean (L, 1);    Lua_pushnumber (L, 10);    Lua_pushnil (L);    Lua_pushstring (L, "Vonzhou");    Stackdump (L);    Dump the Stack lua_pushvalue (L,-4);//Push the value of the index to the stack stackdump (l); Lua_replace (L, 3);  Pop a value and replace the index ' s stackdump (L);   Lua_settop (L, 6);    Set the top index, fill ' nil ' stackdump (L);   Lua_remove (L,-3);    Stackdump (L);    Lua_settop (L,-5);    Stackdump (L);    Lua_close (L); return 0;}
Operation Result:

simply draw a:



C API example for stack operations in Lua

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.