How to write if statement in Lua with not equal symbol for boolean variable.
//In Java,
boolean a;
a = false;
if(!a){
//do something
}
-- In Lua I am trying to replicate the same code
local a
a = false
if(~a) then
-- do something
end
But I am getting error. How to write this in Lua ?
Lua uses mostly keywords. Use not a instead of ~a.
Related
I am trying to use a C++ library named MP4v2 in Swift. It is mostly working in that I can can call some functions, use some classes, etc.
I am having trouble with a particular function that returns a void pointer. It is NULL on failure, or some other value on success. There is a constant defined to check with, but neither that nor checking for nil works.
if file != MP4_INVALID_FILE_HANDLE {
throws /<path_to_project>/main.swift:19:12: Use of unresolved identifier 'MP4_INVALID_FILE_HANDLE', but it is DOES exist (other constants work).
if file != NULL just causes the same problem, and if file != nil never is true, even if the function failed. What am I doing wrong?
Looking at MP4v2 documentation, here is the definition of the macro to check for invalid handle:
#define MP4_INVALID_FILE_HANDLE ((MP4FileHandle)NULL)
The reason it cannot be used in Swift is because it involves a NULL. In fact, if you define something like
#define MY_NULL NULL
in your Objective-C(++) code and try to use it in Swift, Swift will suggest that you use nil instead.
The handle type MP4FileHandle is
typedef void * MP4FileHandle
So, if you are calling a function like
MP4FileHandle aCPPFunction()
You should be able to check the return value as follows in Swift:
let h : MP4FileHandle = aCPPFunction()
if h != nil
{
// The handle is valid and can be given as an argument to
// other library functions.
}
else
{
// The handle is NULL
}
I understand you tried this. It should work, please double-check. If for whatever strange reason this doesn't work for you, there are some other options:
Write a simple helper function in C, C++, Objective-C or
Objective-C++ to check if the handle is valid and return a integer
flag, which should be easily understood by Swift.
Check h.hashValue. If it is 0, then the handle is invalid,
otherwise it is valid. This is a bad undocumented hack, but it has
worked for me. I would stay away from this one.
I need to evaluate simple Boolean functions at run time in C++. The functions are read from a file as strings. Is there any library available that I could use for this?
I looked into BuDDy but sounds like it doesn't parse strings.
Use boost library (lexical_cast.hpp)
bool a = boost::lexical_cast<bool>("true"); //true
bool b = boost::lexical_cast<bool>("0"); //false
Or go here num_get facet and stringstream conversion to boolean - fails with initialised boolean?
The title probably sounds a bit recursive - but this is what I am trying to do:
I have C++ classes Foo and Foobar;
I am using tolua++ to export them to Lua
In Lua:
function wanna_be_starting_something()
foo = Foo:new()
fb = Foobar:new()
-- do something
foo.setResult(42) -- <- I want to store something back at the C++ end
end
In C++
int main(int argc, char argv[])
{
MyResult res;
LuaEngine * engine = new LuaEngine();
engine->run('wbs-something.lua');
// I now want to be able to access the stored result, in variable res
};
So my question is this: how do I pass data from a C++ object that is being manipulated by Lua, back into a C++ program?
To understand how to exchange data back and forth, you should learn about the Lua stack that is the structure Lua uses to communicate with the host program. I guess tolua++ takes care of this for the classes/methods you exported.
Here there is a good start: http://www.lua.org/pil/24.html is for Lua 5.0 but there are indications on how to make it work with 5.1 (which I assume is the Lua version you're using).
If you don't want to dig into all the details, you can always resort to create an ad-hoc C++ method that sets values into a global object. Not the cleanest way, IMHO, but could work.
I don't know tolua++, but both luabind and luabridge support what you need:
* option 1 is just to have the lua code do return whatever and you'll get the that in C++. This require that you'll have a template based version of run(), which returns a value.
* option 2 is to use the lua engine to define a function and then use the engine's call method with the function name and parameters. There are several implementations of LuaEngine which support such a call:
LuaEngine * engine = new LuaEngine();
engine->run("function a(v) return v . 'a'; end ");
valua = engine->call("a", argument);
for example i have this:
static int callFunction(lua_State* L)
{
int p = lua_gettop(L);
if (p == 1 && lua_isfunction(L, -1)) {
/*
* now i need something like "get the function thats in the first parametre
*/
}
return 0;
}
now i need to get the function thats in the first parametre of the function in this C++ code, sry for not being clear, i suck at explaining.
If you need to call the function, you can use lua_call. Lua however won't allow you to take any sort of useful pointer to Lua functions. If you want to store a function in Lua, then you will have to use the Lua registry to store it.
If you want to store a "pointer" to a Lua function in C++, you could just store the /name/ of the Lua function and then do as DeadMG says and call it with lua_call, as here: http://pgl.yoyo.org/luai/i/lua_call .
If you are stuck with the code you have already, it's kind of a sticky problem; I'm not sure you can get the name of the Lua function from the stack you have. In other words, you may need to modify the code one level up from what you posted.
I'm guessing that you might want to look into the lua_tocfunction() function.
I'm talking about C and/or C++ here as this are the only languages I know used for interpreters where the following could be a problem:
If we have an interpreted language X how can a library written for it add functions to the language which can then be called from within programs written in the language?
PHP example:
substr( $str, 5, 10 );
How is the function substr added to the "function pool" of PHP so it can be called from within scripts?
It is easy for PHP storing all registered function names in an array and searching through it as a function is called in a script. However, as there obviously is no eval in C(++), how can the function then be called? I assume PHP doesn't have 100MB of code like:
if( identifier == "substr" )
{
return PHP_SUBSTR(...);
} else if( ... ) {
...
}
Ha ha, that would be pretty funny. I hope you have understood my question so far.
How do interpreters written in C/C++ solve this problem?
How can I solve this for my own experimental toy interpreter written in C++?
Actually scripting languages do something like what you mentioned.
They wrap functions and they register that functions to the interpreter engine.
Lua sample:
static int io_read (lua_State *L) {
return g_read(L, getiofile(L, IO_INPUT), 1);
}
static int f_read (lua_State *L) {
return g_read(L, tofile(L), 2);
}
...
static const luaL_Reg flib[] = {
{"close", io_close},
{"flush", f_flush},
{"lines", f_lines},
{"read", f_read},
{"seek", f_seek},
{"setvbuf", f_setvbuf},
{"write", f_write},
{"__gc", io_gc},
{"__tostring", io_tostring},
{NULL, NULL}
};
...
luaL_register(L, NULL, flib); /* file methods */
Interpreters probably just keep a hashmap of function names to the function definition (which will include parameter information, return type, function location/definition etc.) That way, you can just do a search on the hashmap for a function name (when your interpreter encounters one). If it exists, use the function info in the hashtable to evaluate it.
You obviously need to add provisions for different levels of scope, etc. but that's the gist of it.
Pretty much all compilers have a "symbol table" that they use to look up what an identifier represents. The symbol table will hold function name, variable names, type names, etc... Anything that has a name goes in a symbol table, which is basically a map of names to everything the compiler knows about that name (I'm simplifying here). Then when the compiler encounters an identifier, it look it up in the symbol table, and finds out that it's a function. If you're using an interpreter, then the symbol table will have information on where to find the function and continue interpretation. If this is a compiler, the symbol table will have an address of where that function will be in the compiled code (or a placeholder to fill in the address later). Assembly can then be produced that essentially says: put the arguments on the stack, and resume execution at some address.
So, for you're example an interpreter would look at
substr( $str, 5, 10 );
and find "substr" in it's symbol table:
symbolTableEntry entry = symbolTable["substr"];
from there, it will gather up $str, 5 and 10 as arguments, and look at entry to see that the arguments are valid for the function. Then it will look in entry to find out where to jump to with the marshalled arguments.
In C++ you'd probably use a similar mechanism as Nick D did, but taking advantage of its OO capabilities:
typedef luaFunction boost::function<void(*)(lua_State&)>
std::map<std::string, luaFunction > symbolTable;
symbolTable["read"] = f_read;
symbolTable["close"] = f_close; // etc.
// ...
luaFunction& f = symbolTable[*symbolIterator++];
f(currentLuaState);