TOP > Programming > Lua > A Simple Light Weight App to Run Lua Scripts

[Japanese]

#### A Simple Light Weight App to Run Lua Scripts ####
When I just wanted to try to run Lua casually, the way to run it that came to my mind without any particular reasons was below:
  1. To write Lua code with my beloved text editor.
  2. To run it as 'a Lua script file' somehow.
Somehow? But how? Ah...you know, to drag and drop and...then it's misteriously done or something. Yes, that's sort of how it goes, you know.

I wish I conveniently had something like that but where can I get it?
Here you have. Oh, really?! Huraay!
#### The Whole Source Code ####
The Lua I used was version 5.1.4.
I built the following source code with VC++2010Express.

				#include <stdio.h>
				#include <lua.hpp>
				#include <string>
				#include <windows.h>

				using namespace std;

				//=========================================================
				// Main Entry Point
				//=========================================================
				int main (int argListSize, char *argList[]) {
					
					///// To get the file name from run-time arguments.
					///// That's supposed to be a Lua script file.
					string fileAllPath = ( argListSize > 1 ) ? argList[1] : "";

					///// Exit if any file was not given.
					if ( fileAllPath == "" ) {
						return 0;
					}

					///// To analyze the data as a file path.
					unsigned found = fileAllPath.find_last_of("/\\");
					string path = fileAllPath.substr ( 0, found );
					string file = fileAllPath.substr ( found + 1 );

					///// cd to the directory where the file you'd like to run is.
					SetCurrentDirectory ( path.c_str() );

					///// Start Lua
				    lua_State *lua = luaL_newstate();
					luaL_openlibs(lua);

					///// Load and run the file as a Lua script
					if (luaL_dofile (lua, file.c_str() ) ) {

						// Show error messages if given
						string error_message = lua_tostring(lua, -1);
					
						printf ( "luaL_dofile: %s\n", error_message.c_str());
					}

					///// Wanna stop here? Not really? Okay, forget it. 
					///// You can comment out this line.
					getchar();

					///// Exit
				    lua_close(lua);
					return EXIT_SUCCESS;
				}
				 
				
#### Discription ####
What is this short application going to do?
It has mainly two jobs.

1. [Line 13-25]
To get given run-time arguments and assume that it's a file name.

2. [Line 28]
To change the current working directory into the directory in which the file exists.

3. [Line 31-41]
To load and run the given file as a Lua script file.


I just noticed it's actually three despite what I said. Please forgive me.
The second step is not necessary but it would be a little useful when you describe 'require' statment in your Lua script to load an external file.

Finally, the third one is related to Lua.
I'm sure that you've already seen similar code in several Lua samples.
Honestly, I don't grasp exactly what it means yet.
So there is a possibility that I made misstakes.

Although it looks like working properly, it doesn't mean I'm on the right way.
The life is not so easy. I got tired. But I know nobody can help me. Okay, okay.
#### Download ####
I upload one that I built on my own environment.
SimpleLuaExecuter.zip

It didn't work from the beginning?
You can build the source code above on your own environment.
Wanna change its behavior?
You can modify the source code as much as you like.
Wanna be happier?
That's out of my hands. Please forgive me.

Allow me to show some advice for you.
To build, you need the Lua library itself along with the source code above.
And it also requires you to keep your own will.
That's how you keep going, right?
Well……what were we talking about?
#### How to use ####
  1. Just write a Lua script file with your beloved text editor as you usually do.
  2. Drag the file you made onto 'SimpleLuaExecuter.exe' and just drop it.
That's right. That's the one you and I always wanted.
Aftre all, the only job it does is just to run any given scripts simply.
It goes steadily and calmly. How cute!