How to create a program that runs my LUA code? - lua

So, hi!
I am very new to Coding outside of a Game-Creation Program that automatically creates executable files for me, and from Create Your Frisk and other projects, I know a decent chunk of the basics in LUA. but how do I make a program out of it?
Like, How do I make an executable?(I'm using visual studio to code)

You need a Lua interpreter that runs your Lua code.
Either you use a standalone Lua interpreter, a little executable that can only run Lua scripts, or you embedd a Lua interpreter into your own application.
See http://www.lua.org/start.html

Related

Is there a command I can run from **inside** a Lua script that will tell me where the currently running Lua executable is located?

I'm using lua inside another (Windows) application that provides a lua scripting interface. Is there a way for me to know which lua executable is being used? I know the version, but I would like to know where is the lua.exe that is running.
Lua is an embedded scripting language; that is the design of the thing. Lua is designed and intended to be incorporated into other programs. Lua.exe is essentially just a tiny shim over the Lua runtime, Lua being incorporated into a small console application. This is useful for using Lua as a console scripting language, but Lua.exe is in no way required to use Lua.
Lua scripts are not expected to know or care about the environment in which they run, except for in exactly the ways that this environment provides for them to detect. Lua as a language therefore has no mechanism to detect anything about the nature of the environment. If an embedded Lua environment wants you to be able to query such things, it will provide a mechanism for you to do so.

How to install Lua GUI in ZBS IDE

I am completely new to programming in Lua outside a sandbox.
I have found a library called Lua GUI that I'd like to use (https://github.com/williamwilling/luagui/). I have still not understood how to quite install the library, and how I even go about using it.
That's part 1 of my question; How can I install Lua GUI(or an library for that matter), and then how do I go about actually using it? By that I mean, is it as simple as writing a program that starts with "require "gui"" and then running it, or is there more to it?
Part 2 of my question is then how do I go about installing it as a package for ZeroBrane Studio IDE, I have no idea at all what the readme file is instructing me to do so I would be grateful if someone could clarify.
If you copy the contents of the src folder in the luagui project to your project directory you should be able to call:
local gui = require("gui")
in your main Lua file and use it that way.

Scripting language for code blocks

I got code::blocks as my C/C++ compiler along with C++ for dummies, but my only problem is with a obscure scripting language that I have never heard of before; "Squirrel". Is it possible to change the scripting language of code::blocks to something more familiar to me, like lua?
It seems doable in theory. Whether it is doable in practice, hard to say. Here is what you would need to do:
create a folder src/sdk/scripting/lua in which you put the Lua interpreter (+ Lua libraries like io, math etc) source code and create project file for it
create a folder in src/sdk/scripting/lua_bindings where you put your Lua bindings: the C++ files that allow Lua scripts access to the host application. I recommend you use a tool like SWIG to generate them (codeblocks uses SqPlus). This involves determining what code-blocks functions/classes you want to export, creating one or more .i files, running SWIG on them, put the generated files going into "lua_bindings"; create a DLL project for the bindings
Create a src/lua_scripts in which you put the Lua equivalent of scripts found in src/scripts; or rather, a subset of those scripts, because it is unlikely you will want to export to Lua everything that is available via Squirrel if you're just following examples from a book
Find where Squirrel interpreter is instantiated in codeblocks and where RegisterBindings is called; replace it with instantiation of a Lua interpreter and call your luaopen_codeblocks which you will have created via SWIG (no need for a RegisterLuaBindings if you use SWIG, it does that for you)
Find where the various scripts are called by codeblocks (see http://wiki.codeblocks.org/index.php?title=Scripting_Code::Blocks). Call the equivalent Lua scripts (which are in lua_scripts -- you'll surely have to copy this to the installation folder for code-blocks). For example the startup.script, which is the Squirrel script that codeblocks automatically looks for at startup, is run by the following code in src/src/app.cpp:
// run startup script
try
{
wxString startup = ConfigManager::LocateDataFile(_T("startup.script"), sdScriptsUser | sdScriptsGlobal);
if (!startup.IsEmpty())
Manager::Get()->GetScriptingManager()->LoadScript(startup);
}
catch (SquirrelError& exception)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&exception);
}
I think that's about it.
Naturally based on how extensive your scripting is, you may cut some corners, but as you can see, this is not for the faint of heart!

Running a lua program from a text file

I've just started learning Lua, and I'm trying to get the native Lua interpreter to run a program that has been saved to a .txt file. I'm running Windows Vista with Lua 5.1.4.
Perhaps I'm missing something, but the only thing my book (Programming in Lua) says is that all you have to do to run your program is to call the interpreter with the name of the text file that contains your program. Then it gives this supposedly handy piece of code:
% lua filename.lua
Which I can't get to work in cmd or in the Lua interpreter. Further research I've done indicates that I might need to use a
dofile("filename.lua")
command, but I'm not sure how to do this. Specifically, what information do I need to put in the argument? Any help you can give is greatly appreciated.
You need to download a Win32 binary (see lua-5.2.1_Win32_bin.zip or lua-5.2.1_Win64_bin.zip here). Unzip this somewhere. How to run your script, in order of easiness and inverse order or common procedure:
Drag your script onto the Lua.exe using the Windows file Explorer.
a. Move your script to the same folder as Lua.exe
b. Shift right-click on that folder and select Open Command Window Here.
c. Type lua filename.lua and press Enter.
Add the directory containing Lua.exe to your system PATH, then do steps 2a and 2b on the folder containing your script.
Integrate Lua.exe with your editor in some way (how you do this depends on your editor).

Can bytecode produced by luac be used on computers with no Lua library?

If I compile a regular .lua file with luac, can the result be ran without the Lua library or interpreter installed?
No. You can run it on a version of Lua that was built without the compiler, but you still need the Lua interpreter to execute the code.
Incidentally, the compiled Lua bytecode is also machine-specific; i.e. you can't compile on one architecture and then run that output on another architecture unless you understand the subtleties (endianness, sizes of types, etc.).
If your code doesn't use any dynamic load-based facility (that's loadstring, loadfile, require, etc.) you can strip Lua library to just a VM, because what compiler emits is code to be run on this virtual machine. This can easily cut Lua already small footprint to 1/3 fraction of original.
However, since this is NOT a native binary code for any currently existing architecture, you still CAN'T run it directly without assistance of VM.

Resources