   [C++/Lua] Integrating Lua compiler in my C++ VM class
  Hi all!I've successfully integrated the interpreter to my engine through my VM class. It has the Execute and ExecuteFile methods. However, I wanted to implement the Compile method. I've compiled the lua library, interpreter and compiler as static libraries which my engine links to.I thought instead of using the compiler as a separate application (.exe) and have compile the lua files through that, I thought it would be better to have the compiler be built with my engine as well. What I did is to make the luac.c have a header luac.h file and all of the definitions, includes, and function prototypes in there. I recompiled the library and the static library's still intact.Now, I'm including the luac.h to my VM (lua) class in C++. My VM::Compile method goes like:bool VM::Compile( std::string p_strFile ){    // command line: Luac –o startup.lub startup.lua    if( !m_pVM )    {   return false;    }   int argc = p_strFile.size();    char * argv[1] = { const_cast< char * >( p_strFile.c_str() ) };    struct Smain s;    int i = doargs( argc , argv );    argc -= i;    argv += i;    if ( argc <= 0 ) usage("no input files given");    s.argc = argc;    s.argv = argv;    if( lua_cpcall( m_pVM, pmain, &s ) !=0 ) fatal( lua_tostring( m_pVM, -1 ) );    return true;}compiling it would give me the error message:1>Compiling...1>VM.cpp1>.\VM.cpp(129) : error C2106: '+=' : left operand must be l-valueLooks simple but I'm stumped. How do I fix that?Trying to find out what else will happen past that line, I remarked the line and compile again. I get a new bunch of errors...1>Compiling...1>VM.cpp1>D:\Program Files\Microsoft Visual Studio 9.0\VC\include\xlocnum(135) : error C2129: static function 'int doargs(int,char *[])' declared but not defined1>   d:\work\c++\pgex\pgex\lua/src/luac.h(42) : see declaration of 'doargs'The weird thing about the error is that doargs is actually defined in luac.cAnyone can help me on the above errors?   
    char* argv[1];    argv += i;argv is an array of pointers with 1 element. You're trying to increment the base-pointer of this array, which is giving you an error.Even if this did compile, it would be invalid, as argv would now point past the end of the array. What is this supposed to do?;  Thanks for the attention.argv+= i is part of the algorithm that came with luac.c so I guess it's authentic.char * argv[1] = ....is mine. I am converting the passed string as if it was passed through command prompt via main(int argc, char * argv[]). I just realized I've got some logical error in that as I haven't passed the complete argv for the command "Luac -o outFile inputLuaFile" but I'm sure pointer arithmetic shouldn't flag an error like that as I've seen and used it in some other sections which produce correct code.   ;  You are trying to do pointer arithmetic on an array. Arrays are not pointers. More info. From these two links you should gather that a declarations like these:int main(int argc, char *argv[]){}int main(int argc, char **argv){}Are identical, and that "argv" is actually a pointer in both cases. I usually use the latter so there is no confusion. Hence, pointer arithmetic on a parameter that looks like an array is valid, because it is actually performed on a pointer.What does "p_ptrFile" contain - just "startup.lua" or the full command line?   ;Quote:Original post by AndreiVictorThanks for the attention.argv+= i is part of the algorithm that came with luac.c so I guess it's authentic.char * argv[1] = ....is mine. I am converting the passed string as if it was passed through command prompt via main(int argc, char * argv[]). I just realized I've got some logical error in that as I haven't passed the complete argv for the command "Luac -o outFile inputLuaFile" but I'm sure pointer arithmetic shouldn't flag an error like that as I've seen and used it in some other sections which produce correct code.Ok, I've fixed the code to form a proper argv list which is:bool VM::Compile( std::string p_strFile ){    // command line: Luac –o startup.lub startup.lua    if( !m_pVM )    {   return false;    }    // This finds the last period to signify file extension.    std::string::size_type pos = p_strFile.find_last_of( "." );    if( pos == std::string::npos )    {   return false;    }    // This makes a new file name for the compiled file with extension: psf    std::string compiledName = p_strFile.replace( pos + 1, 3, "psf");    // Manually make an argument list.    int argc = 4;    char * argv[4] = {   "Luac", "-o",   const_cast< char * >( compiledName.c_str() ),   const_cast< char * >( p_strFile.c_str() )    };    // The rest of the algorithm found in luac.c to compile.    struct Smain s;    int i = doargs( argc , argv );    argc -= i;    argv += i;    if ( argc <= 0 ) usage("no input files given");    s.argc = argc;    s.argv = argv;    if( lua_cpcall( m_pVM, pmain, &s ) !=0 ) fatal( lua_tostring( m_pVM, -1 ) );    return true;}Still I got the argv += i; error however the one in luac.c in lua libraries don't have a problem with that. Why is this so?;Quote:Still I got the argv += i; error however the one in luac.c in lua libraries don't have a problem with that. Why is this so?I edited my post above to explain more. Make sure you read those links though.Also std::string::replace modifies the string it is called on. You need to make a copy of p_strFile before calling replace on it.To get around your problem, we can move the pointer arithmetic a bit and get it to work:    // ...    struct Smain s;    int i = doargs( argc , argv );    argc -= i;    // remove this line    // argv += i;    if ( argc <= 0 ) usage("no input files given");    s.argc = argc;    // add "i" here    // argv decays to a pointer, and then add "i" to it.    s.argv = argv + i;    if( lua_cpcall( m_pVM, pmain, &s ) !=0 ) fatal( lua_tostring( m_pVM, -1 ) );Finally, it would probably be quite trivial to add "const" to some of the pointers here to remove the need for "const_cast<>", which is best avoided. Also note that if you have copied this code from the Lua project that you comply with the license on that code.   ;Quote:Original post by rip-offYou are trying to do pointer arithmetic on an array. Arrays are not pointers. More info. From these two links you should gather that a declarations like these:int main(int argc, char *argv[]){}int main(int argc, char **argv){}Are identical, and that "argv" is actually a pointer in both cases. I usually use the latter so there is no confusion. Hence, pointer arithmetic on a parameter that looks like an array is valid, because it is actually performed on a pointer.What does "p_ptrFile" contain - just "startup.lua" or the full command line?Hi rip-off.I just corrected my code regarding what argv should contain. Check the new code above. p_strFile is just a filename for the lua file (e.g. "main.lua" ), now with the code above,  I'm forming the argv array with "Luac", "-o", "main.psf", "main.lua".;  Hi rip-off.Thanks for the help and some enlightenment on certain gotchas there. I've got it fixed thanks to your guidance. :-) And oh, of course I've included the Lua MIT licenses on all affected areas of the modified source code.   