Commit 44492d38 authored by jhammen's avatar jhammen

compiler add import statement functionality

parent 40820532
......@@ -180,6 +180,8 @@ typedef SQInteger (*SQREADFUNC)(SQUserPointer,SQUserPointer,SQInteger);
typedef SQInteger (*SQLEXREADFUNC)(SQUserPointer);
typedef SQInteger (*BIPIMPORTFUNC)(HSQUIRRELVM, const SQChar *, SQBool local);
typedef struct tagSQRegFunction{
const SQChar *name;
SQFUNCTION f;
......@@ -208,6 +210,7 @@ SQUIRREL_API SQRELEASEHOOK sq_getvmreleasehook(HSQUIRRELVM v);
SQUIRREL_API void sq_setsharedreleasehook(HSQUIRRELVM v,SQRELEASEHOOK hook);
SQUIRREL_API SQRELEASEHOOK sq_getsharedreleasehook(HSQUIRRELVM v);
SQUIRREL_API void sq_setprintfunc(HSQUIRRELVM v, SQPRINTFUNCTION printfunc,SQPRINTFUNCTION errfunc);
SQUIRREL_API void bip_setimportfunc(HSQUIRRELVM v, BIPIMPORTFUNC importfunc);
SQUIRREL_API SQPRINTFUNCTION sq_getprintfunc(HSQUIRRELVM v);
SQUIRREL_API SQPRINTFUNCTION sq_geterrorfunc(HSQUIRRELVM v);
SQUIRREL_API SQRESULT sq_suspendvm(HSQUIRRELVM v);
......
......@@ -1576,6 +1576,11 @@ void sq_setprintfunc(HSQUIRRELVM v, SQPRINTFUNCTION printfunc,SQPRINTFUNCTION er
_ss(v)->_errorfunc = errfunc;
}
void bip_setimportfunc(HSQUIRRELVM v, BIPIMPORTFUNC importfunc)
{
_ss(v)->_importfunc = importfunc;
}
SQPRINTFUNCTION sq_getprintfunc(HSQUIRRELVM v)
{
return _ss(v)->_printfunc;
......
......@@ -211,6 +211,19 @@ static SQInteger base_error(HSQUIRRELVM v)
return SQ_ERROR;
}
static SQInteger base_bip_import(HSQUIRRELVM v)
{
const SQChar *filename;
SQInteger local;
sq_getstring(v,2,&filename);
sq_getinteger(v,3,&local);
sq_push(v, 1); //repush the this
if(_ss(v)->_importfunc) {
return _ss(v)->_importfunc(v, filename, local);
}
return 0;
}
static SQInteger base_compilestring(HSQUIRRELVM v)
{
SQInteger nargs=sq_gettop(v);
......@@ -293,6 +306,7 @@ static const SQRegFunction base_funcs[]={
{_SC("type"),base_type,2, NULL},
{_SC("callee"),base_callee,0,NULL},
{_SC("dummy"),base_dummy,0,NULL},
{_SC("import"),base_bip_import,3,NULL},
#ifndef NO_GARBAGE_COLLECTOR
{_SC("collectgarbage"),base_collectgarbage,0, NULL},
{_SC("resurrectunreachable"),base_resurectureachable,0, NULL},
......
......@@ -307,6 +307,9 @@ public:
strongid.Null();
}
break;
case TK_IMPORT:
ImportStatement();
break;
default:
CommaExpr();
_fs->DiscardTarget();
......@@ -1374,6 +1377,46 @@ public:
strongid.Null();
Lex();
}
void ImportStatement()
{
Lex();
SQObject id = _fs->CreateString(_SC("import"));
_fs->PushTarget(0);
_fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
SQInteger key = _fs->PopTarget();
SQInteger table = _fs->PopTarget();
SQInteger closure = _fs->PushTarget();
SQInteger ttarget = _fs->PushTarget();
_fs->AddInstruction(_OP_PREPCALL, closure, key, table, ttarget);
SQObjectPtr path;
SQInteger local = 0;
switch(_token)
{
case TK_IDENTIFIER:
path = _fs->CreateString(_lex._svalue);
break;
case TK_STRING_LITERAL:
path = _fs->CreateString(_lex._svalue,_lex._longstr.size()-1);
local = 1;
break;
default:
Error(_SC("expected string or identifier"), _token);
break;
}
_fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(path));
_fs->AddInstruction(_OP_LOADINT, _fs->PushTarget(), local);
Lex();
_fs->PopTarget();
_fs->PopTarget();
SQInteger stackbase = _fs->PopTarget();
SQInteger closure2 = _fs->PopTarget();
SQInteger numargs = 3;
_fs->AddInstruction(_OP_CALL, _fs->PushTarget(), closure2, stackbase, numargs);
// _fs->AddInstruction(_OP_IMPORT, _fs->PushTarget(), local, _fs->GetConstant(path));
}
void TryCatchStatement()
{
SQObject exid;
......
......@@ -70,6 +70,7 @@ struct SQVM;
#define TK_STATIC 322
#define TK_ENUM 323
#define TK_CONST 324
#define TK_IMPORT 325
......
......@@ -65,6 +65,7 @@ void SQLexer::Init(SQSharedState *ss, SQLEXREADFUNC rg, SQUserPointer up,Compile
ADD_KEYWORD(static,TK_STATIC);
ADD_KEYWORD(enum,TK_ENUM);
ADD_KEYWORD(const,TK_CONST);
ADD_KEYWORD(import, TK_IMPORT);
ADD_KEYWORD(__LINE__,TK___LINE__);
ADD_KEYWORD(__FILE__,TK___FILE__);
......
......@@ -107,6 +107,7 @@ public:
SQCOMPILERERROR _compilererrorhandler;
SQPRINTFUNCTION _printfunc;
SQPRINTFUNCTION _errorfunc;
BIPIMPORTFUNC _importfunc;
bool _debuginfo;
bool _notifyallexceptions;
SQUserPointer _foreignptr;
......
......@@ -40,6 +40,19 @@ static void squirrel_print_function(HSQUIRRELVM sqvm, const SQChar *format, ...)
va_end(args);
}
SQInteger bip_import_function(HSQUIRRELVM vm, const SQChar *filename, SQBool local) {
if (local) {
SQBool printerror = SQFalse;
if(SQ_SUCCEEDED(sqstd_dofile(vm, filename, SQTrue, printerror))) {
return 1;
}
return SQ_ERROR;
} else {
// TODO: native package import
}
return 1;
}
void ScriptHost::resetProcessorThreads() {
ProcessorThread *thread = processorThreads.first();
while (thread) {
......@@ -143,6 +156,7 @@ uint8_t ScriptHost::run() {
// set print function
sqstd_seterrorhandlers(vm);
sq_setprintfunc(vm, squirrel_print_function, squirrel_print_function);
bip_setimportfunc(vm, bip_import_function);
// standard library modules
HSQOBJECT apiTable;
sq_pushroottable(vm);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment