lua - 用 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(...) 等术语。
知道如何接近吗?
解决方案
我只回答 b/c 这个答案没有答案,因此可以归类为已回答。
您需要拦截(使用metatable(_G)
)访问pre_models()
并运行脚本。最后,m
在每次调用截获的pre_models()
.
推荐阅读
- r - Fontawesome 5.13.0 在 visNetwork 中不起作用
- c - 为什么make会执行我从未在makefile中写过的命令以及如何调试它?
- c# - [ApiController] 属性有什么作用?
- javascript - 如何在javascript中将对象转换为有效的css样式?
- vnc - Tiger VNC在环回IP地址上创建会话
- http - 监控通过 wininet.dll 发生的传入和传出 http 流量
- java - 实体转 Excel?
- angular - 尝试将数字格式化为数据库中的时间格式
- jquery - 把计数器变成罗马字母
- ios - 如何在 swift 中禁用 UITableCell 插图上的选择?