The Squirrel interpreter. See http://www.squirrel-lang.org/
etc/minimal.c@0:97a4f8cc534c, 2014-12-16 (annotated)
- Committer:
- jhnwkmn
- Date:
- Tue Dec 16 10:20:34 2014 +0000
- Revision:
- 0:97a4f8cc534c
Initial import of Squirrel.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhnwkmn | 0:97a4f8cc534c | 1 | #include <stdarg.h> |
jhnwkmn | 0:97a4f8cc534c | 2 | #include <stdio.h> |
jhnwkmn | 0:97a4f8cc534c | 3 | |
jhnwkmn | 0:97a4f8cc534c | 4 | #include <squirrel.h> |
jhnwkmn | 0:97a4f8cc534c | 5 | #include <sqstdio.h> |
jhnwkmn | 0:97a4f8cc534c | 6 | #include <sqstdaux.h> |
jhnwkmn | 0:97a4f8cc534c | 7 | |
jhnwkmn | 0:97a4f8cc534c | 8 | #ifdef _MSC_VER |
jhnwkmn | 0:97a4f8cc534c | 9 | #pragma comment (lib ,"squirrel.lib") |
jhnwkmn | 0:97a4f8cc534c | 10 | #pragma comment (lib ,"sqstdlib.lib") |
jhnwkmn | 0:97a4f8cc534c | 11 | #endif |
jhnwkmn | 0:97a4f8cc534c | 12 | |
jhnwkmn | 0:97a4f8cc534c | 13 | #ifdef SQUNICODE |
jhnwkmn | 0:97a4f8cc534c | 14 | |
jhnwkmn | 0:97a4f8cc534c | 15 | #define scvprintf vfwprintf |
jhnwkmn | 0:97a4f8cc534c | 16 | #else |
jhnwkmn | 0:97a4f8cc534c | 17 | |
jhnwkmn | 0:97a4f8cc534c | 18 | #define scvprintf vfprintf |
jhnwkmn | 0:97a4f8cc534c | 19 | #endif |
jhnwkmn | 0:97a4f8cc534c | 20 | |
jhnwkmn | 0:97a4f8cc534c | 21 | void printfunc(HSQUIRRELVM v,const SQChar *s,...) |
jhnwkmn | 0:97a4f8cc534c | 22 | { |
jhnwkmn | 0:97a4f8cc534c | 23 | va_list vl; |
jhnwkmn | 0:97a4f8cc534c | 24 | va_start(vl, s); |
jhnwkmn | 0:97a4f8cc534c | 25 | scvprintf(stdout, s, vl); |
jhnwkmn | 0:97a4f8cc534c | 26 | va_end(vl); |
jhnwkmn | 0:97a4f8cc534c | 27 | } |
jhnwkmn | 0:97a4f8cc534c | 28 | |
jhnwkmn | 0:97a4f8cc534c | 29 | void errorfunc(HSQUIRRELVM v,const SQChar *s,...) |
jhnwkmn | 0:97a4f8cc534c | 30 | { |
jhnwkmn | 0:97a4f8cc534c | 31 | va_list vl; |
jhnwkmn | 0:97a4f8cc534c | 32 | va_start(vl, s); |
jhnwkmn | 0:97a4f8cc534c | 33 | scvprintf(stderr, s, vl); |
jhnwkmn | 0:97a4f8cc534c | 34 | va_end(vl); |
jhnwkmn | 0:97a4f8cc534c | 35 | } |
jhnwkmn | 0:97a4f8cc534c | 36 | |
jhnwkmn | 0:97a4f8cc534c | 37 | void call_foo(HSQUIRRELVM v, int n,float f,const SQChar *s) |
jhnwkmn | 0:97a4f8cc534c | 38 | { |
jhnwkmn | 0:97a4f8cc534c | 39 | SQInteger top = sq_gettop(v); //saves the stack size before the call |
jhnwkmn | 0:97a4f8cc534c | 40 | sq_pushroottable(v); //pushes the global table |
jhnwkmn | 0:97a4f8cc534c | 41 | sq_pushstring(v,_SC("foo"),-1); |
jhnwkmn | 0:97a4f8cc534c | 42 | if(SQ_SUCCEEDED(sq_get(v,-2))) { //gets the field 'foo' from the global table |
jhnwkmn | 0:97a4f8cc534c | 43 | sq_pushroottable(v); //push the 'this' (in this case is the global table) |
jhnwkmn | 0:97a4f8cc534c | 44 | sq_pushinteger(v,n); |
jhnwkmn | 0:97a4f8cc534c | 45 | sq_pushfloat(v,f); |
jhnwkmn | 0:97a4f8cc534c | 46 | sq_pushstring(v,s,-1); |
jhnwkmn | 0:97a4f8cc534c | 47 | sq_call(v,4,SQFalse,SQTrue); //calls the function |
jhnwkmn | 0:97a4f8cc534c | 48 | } |
jhnwkmn | 0:97a4f8cc534c | 49 | sq_settop(v,top); //restores the original stack size |
jhnwkmn | 0:97a4f8cc534c | 50 | } |
jhnwkmn | 0:97a4f8cc534c | 51 | |
jhnwkmn | 0:97a4f8cc534c | 52 | int main(int argc, char* argv[]) |
jhnwkmn | 0:97a4f8cc534c | 53 | { |
jhnwkmn | 0:97a4f8cc534c | 54 | HSQUIRRELVM v; |
jhnwkmn | 0:97a4f8cc534c | 55 | v = sq_open(1024); // creates a VM with initial stack size 1024 |
jhnwkmn | 0:97a4f8cc534c | 56 | |
jhnwkmn | 0:97a4f8cc534c | 57 | //REGISTRATION OF STDLIB |
jhnwkmn | 0:97a4f8cc534c | 58 | //sq_pushroottable(v); //push the root table where the std function will be registered |
jhnwkmn | 0:97a4f8cc534c | 59 | //sqstd_register_iolib(v); //registers a library |
jhnwkmn | 0:97a4f8cc534c | 60 | // ... call here other stdlibs string,math etc... |
jhnwkmn | 0:97a4f8cc534c | 61 | //sq_pop(v,1); //pops the root table |
jhnwkmn | 0:97a4f8cc534c | 62 | //END REGISTRATION OF STDLIB |
jhnwkmn | 0:97a4f8cc534c | 63 | |
jhnwkmn | 0:97a4f8cc534c | 64 | sqstd_seterrorhandlers(v); //registers the default error handlers |
jhnwkmn | 0:97a4f8cc534c | 65 | |
jhnwkmn | 0:97a4f8cc534c | 66 | sq_setprintfunc(v, printfunc,errorfunc); //sets the print function |
jhnwkmn | 0:97a4f8cc534c | 67 | |
jhnwkmn | 0:97a4f8cc534c | 68 | sq_pushroottable(v); //push the root table(were the globals of the script will be stored) |
jhnwkmn | 0:97a4f8cc534c | 69 | if(SQ_SUCCEEDED(sqstd_dofile(v, _SC("test.nut"), SQFalse, SQTrue))) // also prints syntax errors if any |
jhnwkmn | 0:97a4f8cc534c | 70 | { |
jhnwkmn | 0:97a4f8cc534c | 71 | call_foo(v,1,2.5,_SC("teststring")); |
jhnwkmn | 0:97a4f8cc534c | 72 | } |
jhnwkmn | 0:97a4f8cc534c | 73 | |
jhnwkmn | 0:97a4f8cc534c | 74 | sq_pop(v,1); //pops the root table |
jhnwkmn | 0:97a4f8cc534c | 75 | sq_close(v); |
jhnwkmn | 0:97a4f8cc534c | 76 | |
jhnwkmn | 0:97a4f8cc534c | 77 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 78 | } |