From 27d6efb5f5a076f825fe2da1875e0cabaf02b4e7 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 17 Nov 2021 23:03:07 +0800 Subject: + LuaMacro --- Tools/LuaMacro/tests/proto.lua | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Tools/LuaMacro/tests/proto.lua (limited to 'Tools/LuaMacro/tests/proto.lua') diff --git a/Tools/LuaMacro/tests/proto.lua b/Tools/LuaMacro/tests/proto.lua new file mode 100644 index 0000000..dff532c --- /dev/null +++ b/Tools/LuaMacro/tests/proto.lua @@ -0,0 +1,31 @@ +local M = require 'macro' + +function _assert_arg(val,idx,t) + if type(val) ~= t then + error(("type mismatch argument %d: got %s, expecting %s"):format(idx,type(val),t),2) + end +end + +M.define('Function',function(get,put) + local name = get:upto '(' + local args,endt = get:list() + args = args:strip_spaces() + local argnames,names = {},{} + for i,a in ipairs(args) do + local name = a:pick(1) + M.assert(a:pick(2) == ':') + table.remove(a,1) + table.remove(a,1) + argnames[i] = {{'iden',name}} + names[i] = name + end + get:expecting ':' + local rtype, endt = get:upto '\n' + put :keyword 'function' :space() :tokens(name) '(' :list(argnames) ')' :space '\n' + put :space() + for i,a in ipairs(args) do + local tp = a:pick(1) + put :iden('_assert_arg') '(' :iden(names[i]) ',' :number(i) ',' :string(tp) ')' ';' + end + return put +end) -- cgit v1.1-26-g67d0