2.7. Compiling a script

You can compile a Quirrel script with the function sq_compile.:

SQRESULT sq_compile(HSQUIRRELVM v, const SQChar* s, SQInteger size,
            const SQChar *sourcename, SQBool raiseerror,
            const HSQOBJECT *bindings = nullptr);

The function compiles a script from a memory buffer. The parameters are:

  • v: the target VM

  • s: pointer to the buffer containing the script source code

  • size: size in characters of the buffer

  • sourcename: symbolic name of the program (used for runtime errors)

  • raiseerror: if true, the compiler error handler will be called on errors

  • bindings: optional compile-time bindings object (can be nullptr)

If sq_compile succeeds, the compiled script will be pushed as Quirrel function in the stack.

When the compiler fails for a syntax error it will try to call the ‘compiler error handler’; this function must be declared as follow:

typedef void (*SQCOMPILERERROR)(HSQUIRRELVM v, SQMessageSeverity severity,
                        const SQChar *desc, const SQChar *source,
                        SQInteger line, SQInteger column, const SQChar *extra_info);

where severity indicates the message severity (error, warning, etc.), desc is the error description, source is the source filename, line and column are the location, and extra_info provides additional context.

The handler can be set with the following API call:

void sq_setcompilererrorhandler(HSQUIRRELVM v, SQCOMPILERERROR f);