Lua编程form

local form = {}
-- form to produce the target with super functional table and integrating multi-interface implement feature
function form.build(tag, super)
    super = super or form
    local target = {_tag = tag, _super = super}
    return setmetatable(target, form._meta())
end

-- specification on the target
function form.on(target, spec)
    local handler = {}
    function handler.function(target, spec)
        return function ( ... )
            return spec(target, ...)
        end
    end
    function handler.table(target, spec)
        local tar = {}
        for k,v in pairs(spec) do
            if type(v) == ‘function‘ then
                tar[k] = function ( ... )
                    spec[k](target, ...)
                end
            else
                tar[k] = v
            end
        end
        return tar
    end
    local handler_spec = handler[type(spec)]
    if handler_spec then
        print(target._tag..‘[‘..prop..‘] target binding form produced‘)
    end
    return handler_spec and handler_spec(target, spec)
end

--[[
function form:spec(prop, target) end
local p1 -- protocol specification
local p2 -- protocol specification
local px -- any interface spec
-- Future init style
local Future =
    form.build(‘Future‘):spec(‘proto_xxx‘, p1)
                        :spec(‘proto_xx2‘, p2)
                        :spec(px)
-- protocol usecase, assume implemented on target ‘Future‘
-- assume there p1:funcall defined
Future:spec(‘proto_xxx‘).funcall()
-- assume there px:funcall defined
Future:funcall()
--]]
function form:spec(prop, target)
    local handler = {}
    function handler.string(self, prop, target)
        if target ~= nil then
            print(self._tag..‘[‘..prop..‘] setter/getter produced‘)
            local property = ‘_‘..prop
            local function prop_(self, target)
                if target ~= nil then
                    self[property] = target
                    return self
                end
                return self[property]
            end
            prop_(self, target)[prop] = prop_
        end
        return form.on(self, self[prop](self))
    end
    function handler.table(self, prop)
        local target = prop
        local interface = self._interface or {}
        if  self._interface == nil then
            self._interface = interface
        end
        table.insert(interface, target)
        if type(target._tag)==‘string‘ then
            print(self._tag..‘ interface[‘..target._tag..‘] specified‘)
        end
        return self
    end
    local handler_prop = handler[type(prop)]
    return handler_prop and handler_prop(self, prop, target)
end

function form._meta()
    local meta = {}
    function meta.__index(target, key)
        local v = target._super[key]
        if v == nil then
            local interface = target._interface or {}
            for i=1, #interface do
                v = interface[i][key]
                if v ~= nil then return v end
            end
        end
        return v
    end
    return meta
end

return form

form.test

local p1 -- protocol specification
local p2 -- protocol specification
local px -- any interface spec
-- Future init style
local Future =
    form.build(‘Future‘):spec(‘proto_xxx‘, p1)
                        :spec(‘proto_xx2‘, p2)
                        :spec(px)
-- protocol usecase, assume implemented on target ‘Future‘
-- assume there p1:funcall defined
Future:spec(‘proto_xxx‘).funcall()
-- assume there px:funcall defined
Future:funcall()-- using property getterFuture:proto_xx2()==p2
时间: 2024-08-09 23:54:14

Lua编程form的相关文章

Lua编程基础

--建议用Notepad++打开语言调到Lua 在Lua中,一切都是变量,除了关键字.请记住这句话. I. 首先是注释 写一个程序,总是少不了注释的. 在Lua中,你可以使用单行注释和多行注释. 单行注释中,连续两个减号"--"表示注释的开始,一直延续到行末为止.相当于C++语言中的"//". 多行注释中,由"--[["表示注释开始,并且一直延续到"]]"为止.这种注释相当于C语言中的"/*-*/".在注释

Python编程和 Lua编程的比较

Python编程和 Lua编程的比较 2016.4.21 定义函数: python: def functionname( parameters ): "函数_文档字符串" function_suite return [expression] lua: --[[ function returning the max between two numbers --]] function max(num1, num2) if (num1 > num2) then result = num

VS2008 Lua 编程环境搭建(初学备忘)

在VS 2008 中,导入 lua.h 之类的头文件后,编译含有Lua函数的时候,可能会出现如下错误: 1>main.obj : error LNK2019: 无法解析的外部符号_luaL_checkinteger,该符号在函数"int __cdecl add(struct lua_State *)" ([email protected]@[email protected]@@Z) 中被引用 1>main.obj : error LNK2019: 无法解析的外部符号_lua

lua编程之协程介绍

一,lua协程简介 协程(coroutine),意思就是协作的例程,最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程属于侵入式组件,线程实现的系统称之为抢占式多任务系统,而协程实现的多任务系统成为协作式多任务系统.线程由于缺乏yield语义,所以运行过程中不可避免需要调度,休眠挂起,上下文切换等系统开销,还需要小心使用同步机制保证多线程正常运行.而协程的运行指令系列是固定的,不需要同步机制,协程之间切换也只涉及到控制权的交换,相比较线程来说是非常轻便的.

饥荒MOD lua编程0基础入门

前言 原贴写于饥荒游戏贴吧,为了使文章针对性更强,将原文切割并精简.此贴主要为编程0基础的modder讲解一些编程的基础知识.至于说有关饥荒框架的介绍,则会放在另一篇文章里讲解. 编程0基础的人,要想学习制作MOD,难度是比较大的,因为缺乏一些基本的编程概念,只懂得复制别人的代码或者在它们的基础上稍加改变,遇到稍微复杂一点的代码,就束手无策了.对于MOD崩溃或错误,也几乎没办法自行处理.但我也不推荐先去学一门编程语言之后再来学习MOD代码,这是没有必要的.事实上饥荒MOD里用到的基本编程知识都比

Lua编程入门-学习笔记1

第1章:起点 Chunks: 语句块 每个语句结尾的分号是可选的,如果同一行有多个语句最好使用分号分隔: dofile("lib1.lua")  -- 执行lua文件 全局变量:局部变量用local修饰,否则就是全局变量 保留字: if then else elseif end and or not fuction return end true false nil while ... do .. end    break repeat ... until ... for ... in

lua编程之元表与元方法

一. 前言 lua是一种非常轻量的动态类型语言,在1993年由由Roberto Ierusalimschy.Waldemar Celes 和 Luiz Henrique de Figueiredo等人发明,lua的设计目标是轻便地嵌入宿主语言,增强系统的可扩展性和可定制性.lua的源码只有两万余行,非常精简小巧,在目前的脚本引擎中,lua的速度是最快的,这也是lua进入程序设计语言前20名,如今已经广泛应用于游戏行业,这几篇文章将会讨论下lua的几个比较重要的特性. 一门语言的类型系统是其最根本

学习cocos code ide 的lua编程

装上了cocos code ide,配了路径之后,建立一个默认lua工程,发现无法调试,报错. 问了别人,在cocos2dconstants.lua的613行插入cc.AsyncTaskPool  =  cc.AsyncTaskPool or {} 然后发现可以直接运行了,但还是不能调试.重启,一切正常. 接下来看默认lua工程的代码.这是一个杀虫游戏.目录结构如下: 其中: res是图片和声音的文件夹 app是游戏逻辑代码 cocos是cocos lua版代码 packages作用不明 con

玩转cocos2d-x lua-binding, 实现c++与lua混合编程

引言 城市精灵GO(http://csjl.teamtop3.com/)是一款基于cocos2d-x开发的LBS社交游戏, 通过真实地图的探索, 发现和抓捕隐匿于身边的野生精灵, 利用游戏中丰富的玩法提升和进化自己的精灵团队, 一步一步成为精灵训练大师. 本游戏的开发混合使用了c++和lua编程, 既发挥了c++高性能, 跨平台系统兼容的优势, 又享受了lua敏捷方便的开发效率. cocos2d-x提供了一套完备的lua-binding工具来帮助开发者实现c++和lua的代码联合, 可以方便实现