📜 ⬆️ ⬇️

Lua: how to stop embedding and start living

Lua: how to stop embedding and start living



For Lua, the glory of the semi-language is firmly established - a tool that can be embedded if necessary in order to script an application written in a compiled language like C ++. Nevertheless, Lua is a completely independent language with its own interpreter, the ability to create modules, a large number of libraries, and at the same time, this PL has the minimum size among its counterparts. Simply put, we have everything to create the same applications as perl, python, and in general any other common programming language.


')
I can offer you the following arguments for Lua:

To demonstrate the capabilities of Lua, I will show how to use it to create a small program for building graphs by points with the ability to save the graph as an image file.

As a graphic toolkit, we will use iup , a cross-platform library that was originally created with a usage calculation from Lua.

Installing Lua SDK

As part of the idea of ​​using Lua as a standalone PL, the Lua for Windows assembly was created, which contains libraries necessary for everyday tasks arising when programming for the specified OS: work with databases, GUI, XML parsing, etc. Do not be confused by the fact that the version of Lua in build 5.1 and not 5.2 is not much different in our case.

Download and install the build.

Iup concept brief

I thought for a long time how to paint the process of creating a program, without going into detail in the device iup. And I decided to briefly describe its basic principles:
If you previously wrote to the GUI using Tk, WxWidgets, or WinAPI, then all this will seem familiar. If not, the program is covered in some detail by the comments.

Program code


--   iup require("iuplua" ) require("iupluacontrols") require("iuplua_pplot") --     Canvas,      require("cdlua") require("iupluacd") require("string") --        --    plots_number = 5 --  ,          tabs = {} --    vboxes = {} --    ,    checkboxes = {} --         coords = {} --      legends = {} --     global_legend = {} --   ,  Lua    split function string:split(sep) local sep, fields = sep or ":", {} local pattern = string.format("([^%s]+)", sep) self:gsub(pattern, function(c) fields[#fields+1] = c end) return fields end --         function draw_plot(pwidget, pnum, data) x = data[1].value:split(",") y = data[2].value:split(",") if checkboxes[pnum].value == "OFF" then return end if not (#x == #y) or #x == 0 then iup.Message("", "      " .. pnum) return end iup.PPlotBegin(pwidget, 0) iup.PPlotAdd(pwidget, 0, 0) for i = 1,#x do iup.PPlotAdd(pwidget, x[i], y[i]) end iup.PPlotEnd(pwidget) end --       plot_btn = iup.button{ title = ""} --    " " function plot_btn:action() --    plot = iup.pplot { expand="YES", TITLE = "Simple Line", MARGINBOTTOM="65", MARGINLEFT="65", AXS_XLABEL = global_legend[1].value, AXS_YLABEL = global_legend[2].value, LEGENDSHOW="YES", LEGENDPOS="TOPLEFT", size = "400x300" } --      -          iup.PPlotBegin(plot, 0) iup.PPlotAdd(plot,0,0) plot.DS_LEGEND = "" iup.PPlotEnd(plot) --     for i = 1, plots_number do --        print(legends[i].value) plot.DS_LEGEND = legends[i].value --   draw_plot(plot, i, coords[i]) end --        save_btn = iup.button{ title = "" } --    ,     plot_dg = iup.dialog { iup.vbox --   ,       { plot, save_btn }, } --     function save_btn:action() --        --          EMF fs_dlg = iup.filedlg{DIALOGTYPE = "SAVE", FILTER = "*.emf" } iup.Popup(fs_dlg) --    if tonumber(fs_dlg.STATUS) >= 0 then --      pic = fs_dlg.value if not (string.sub(pic, string.len(pic)-3) == ".emf") then pic = pic .. ".emf" end --  -,    tmp_cv = cd.CreateCanvas(cd.EMF, pic .. " 400x300") --     iup.PPlotPaintTo(plot, tmp_cv) --     cd.KillCanvas(tmp_cv) end end --     plot_dg:showxy(iup.CENTER, iup.CENTER) --       if (iup.MainLoopLevel()==0) then iup.MainLoop() end end --    ,       --    for i=1,plots_number do --   ,      coords[i] = {} for j = 1,2 do coords[i][j] = iup.text { expand="HORIZONTAL", multiline = "YES", VISIBLELINES = 5 } end --       legends[i] = iup.text{ expand = "HORIZONTAL" } --        vboxes[i] = iup.vbox { iup.hbox { iup.label { title = " :" }, legends[i] }, iup.hbox { iup.label { title="X : ", }, coords[i][1] }, iup.hbox { iup.label { title="Y : ", }, coords[i][2] }; expand="YES", } --    vboxes[i].tabtitle = " " .. i --  ,     ,    --       checkboxes[i] = iup.toggle{ title= "" .. i, value = "ON" } end --        tabs = iup.tabs{unpack(vboxes)} --        global_legend[1] = iup.text{} global_legend[2] = iup.text{} --       frame = iup.frame { iup.vbox { iup.label{ title=" :", expand="HORIZONTAL" }, iup.vbox { unpack(checkboxes) }, iup.label{}, --       iup.label{title = ""}, iup.hbox { iup.label{ title = " X "}, global_legend[1] }, iup.hbox { iup.label{ title = " Y "}, global_legend[2] }, iup.label{}, plot_btn }; expand = "VERTICAL", } --            dg = iup.dialog { iup.hbox { frame, tabs }, title=" ", size = "HALF" } --        dg:showxy(iup.CENTER, iup.CENTER) if (iup.MainLoopLevel()==0) then iup.MainLoop() end 


A couple of words about deployment

The script can be run using the command:

 lua plotter.exe 


In this case, the libraries will be connected from the clibs / subdirectory, which is located in the directory where Lua for Windows was installed. To pack the script and libraries as compactly as possible to transfer to another machine, simply copy the following files into one folder (indicated with relative paths from the Lua installation directory):

 lua.exe lib/lua5.1.dll clibs/cd.dll clibs/cdlua51.dll clibs/iup.dll clibs/iup_pplot.dll clibs/iupcd.dll clibs/iupcontrols.dll clibs/iupgl.dll clibs/iuplua51.dll clibs/iuplua_pplot51.dll clibs/iupluacd51.dll clibs/iupluacontrols51.dll clibs/freetype6.dll 


Do not forget to put the script with the program in this folder. Now you can transfer this folder to another machine and start your program using the command specified above. At the same time no other actions for installing libraries and runtime are needed.

Unfortunately, the cd.dll, cdluad51.dll and iupcd.dll files in this version of Lua for Windows may not work correctly, therefore I recommend taking them from the archive using the link below.

Results

Archive with a working version is here , for convenience, a launcher app.bat is added.

Screenshots:





As a result, we received, albeit an odd one, a utility that has the same functionality as if it were written in a “serious” programming language. At the same time easy to deploy and the total weight of less than 2 MB. Memory consumption - about 7 MB. The source code is editable, Lua itself is interactively understandable, which simplifies the refinement of such software in the field.

In my opinion, this is an excellent choice for writing educational software for schools and institutes, as well as for internal use in enterprises. Since weak machines are still abundantly present in similar places throughout the CIS, the use of Lua in this way is advisable, especially in light of the gradual arrival of Linux on desktops. In addition, the tendency of losing the source code of samopisny software with its terrible zagupannosti can be equated to a national disaster.

Source: https://habr.com/ru/post/149857/


All Articles