首页 > 解决方案 > 用 lua 本身剖析 lua 代码

问题描述

我有一个用 lua 编写具有给定结构的配置文件,稍后应该动态创建一个 GUI:

-- Input config file
package.path = '?.fsl;'
require "init"

global_unit(mm)                    --   Global unit (m, cm, mm)
pickdist(0.001)                    --   Snap distance
cosys(polar)                       --   Set system of coordinates

-- Define model name, for filenames etc.
new_model_force(model_name,"from main.fsl")

-- Define FE Control Data
m.hc_min          =         95.000 --   Limit demagnetisa > 0:[%]Hc;<0:[kA/m]
m.con_hdcopy      =          1.000 --   Hc-copy:Name:auto:0;intact:1; none:-1
m.b_max           =          2.200 --   Max Induction [T] in colorgradation
m.b_min           =          0.000 --   Move inside: 0 ; Move outside: > 0

pre_models("FE-contr-data");

pre_models("connect_models");

-- Define the Basic Model Parameter
m.tot_num_slot    =             QS --   Number of slots               (>= 1)
m.num_poles       =          2 * p --   Number of poles 2p            (>= 2)
m.npols_gen       =         2*p*mf --   Number of poles simulated     (>= 1) --=m.num_poles * m.num_sl_gen / m.tot_num_slot
m.num_slots       =          QS*mf --   Number of slots in model
m.arm_length      =            lFe --   Effect. armature length          [mm]
m.fc_radius       =   (Di-delta)/2 --   Radius air-gap center            [mm] --=(m.inside_diam-ag)/2

pre_models("basic_modpar");

...

所有 m.whatever 变量都属于下面的相应 pre_models(...) 语句。每个“pre_models(...) - 块”的变量的数量和名称有时是未知的,同样适用于“pre_models(...) - 块”本身。

GUI应该显示某事。喜欢:

Tab1 = General settings: global_unit -> mm, pickdist -> 0.001, etc.
Tab2 = FE-contr-data: m.hc_min -> 95.000, m.con_hdcopy -> 1.000, etc.
Tab3 = connect_models:
Tab4 = basic_modpar: m.tot_num_slot -> 36, m.num_poles -> 30, etc.

..其中 QS = 36 和 p = 15 在文件 init 中定义。

希望:代码不必知道“FE-contr-data”、global_unit(...) 等术语。

知道如何接近吗?

标签: lua

解决方案


我只回答 b/c 这个答案没有答案,因此可以归类为已回答。

您需要拦截(使用metatable(_G))访问pre_models()并运行脚本。最后,m在每次调用截获的pre_models().


推荐阅读