The Squirrel interpreter. See http://www.squirrel-lang.org/

Dependents:   Squirrel

Committer:
jhnwkmn
Date:
Tue Dec 16 11:39:42 2014 +0000
Revision:
3:7268a3ceaffc
Parent:
0:97a4f8cc534c
Accepts \r as line terminator as well.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhnwkmn 0:97a4f8cc534c 1 /* see copyright notice in squirrel.h */
jhnwkmn 0:97a4f8cc534c 2 #include <squirrel.h>
jhnwkmn 0:97a4f8cc534c 3 #include <math.h>
jhnwkmn 0:97a4f8cc534c 4 #include <stdlib.h>
jhnwkmn 0:97a4f8cc534c 5 #include <sqstdmath.h>
jhnwkmn 0:97a4f8cc534c 6
jhnwkmn 0:97a4f8cc534c 7 #define SINGLE_ARG_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
jhnwkmn 0:97a4f8cc534c 8 SQFloat f; \
jhnwkmn 0:97a4f8cc534c 9 sq_getfloat(v,2,&f); \
jhnwkmn 0:97a4f8cc534c 10 sq_pushfloat(v,(SQFloat)_funcname(f)); \
jhnwkmn 0:97a4f8cc534c 11 return 1; \
jhnwkmn 0:97a4f8cc534c 12 }
jhnwkmn 0:97a4f8cc534c 13
jhnwkmn 0:97a4f8cc534c 14 #define TWO_ARGS_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
jhnwkmn 0:97a4f8cc534c 15 SQFloat p1,p2; \
jhnwkmn 0:97a4f8cc534c 16 sq_getfloat(v,2,&p1); \
jhnwkmn 0:97a4f8cc534c 17 sq_getfloat(v,3,&p2); \
jhnwkmn 0:97a4f8cc534c 18 sq_pushfloat(v,(SQFloat)_funcname(p1,p2)); \
jhnwkmn 0:97a4f8cc534c 19 return 1; \
jhnwkmn 0:97a4f8cc534c 20 }
jhnwkmn 0:97a4f8cc534c 21
jhnwkmn 0:97a4f8cc534c 22 static SQInteger math_srand(HSQUIRRELVM v)
jhnwkmn 0:97a4f8cc534c 23 {
jhnwkmn 0:97a4f8cc534c 24 SQInteger i;
jhnwkmn 0:97a4f8cc534c 25 if(SQ_FAILED(sq_getinteger(v,2,&i)))
jhnwkmn 0:97a4f8cc534c 26 return sq_throwerror(v,_SC("invalid param"));
jhnwkmn 0:97a4f8cc534c 27 srand((unsigned int)i);
jhnwkmn 0:97a4f8cc534c 28 return 0;
jhnwkmn 0:97a4f8cc534c 29 }
jhnwkmn 0:97a4f8cc534c 30
jhnwkmn 0:97a4f8cc534c 31 static SQInteger math_rand(HSQUIRRELVM v)
jhnwkmn 0:97a4f8cc534c 32 {
jhnwkmn 0:97a4f8cc534c 33 sq_pushinteger(v,rand());
jhnwkmn 0:97a4f8cc534c 34 return 1;
jhnwkmn 0:97a4f8cc534c 35 }
jhnwkmn 0:97a4f8cc534c 36
jhnwkmn 0:97a4f8cc534c 37 static SQInteger math_abs(HSQUIRRELVM v)
jhnwkmn 0:97a4f8cc534c 38 {
jhnwkmn 0:97a4f8cc534c 39 SQInteger n;
jhnwkmn 0:97a4f8cc534c 40 sq_getinteger(v,2,&n);
jhnwkmn 0:97a4f8cc534c 41 sq_pushinteger(v,(SQInteger)abs((int)n));
jhnwkmn 0:97a4f8cc534c 42 return 1;
jhnwkmn 0:97a4f8cc534c 43 }
jhnwkmn 0:97a4f8cc534c 44
jhnwkmn 0:97a4f8cc534c 45 SINGLE_ARG_FUNC(sqrt)
jhnwkmn 0:97a4f8cc534c 46 SINGLE_ARG_FUNC(fabs)
jhnwkmn 0:97a4f8cc534c 47 SINGLE_ARG_FUNC(sin)
jhnwkmn 0:97a4f8cc534c 48 SINGLE_ARG_FUNC(cos)
jhnwkmn 0:97a4f8cc534c 49 SINGLE_ARG_FUNC(asin)
jhnwkmn 0:97a4f8cc534c 50 SINGLE_ARG_FUNC(acos)
jhnwkmn 0:97a4f8cc534c 51 SINGLE_ARG_FUNC(log)
jhnwkmn 0:97a4f8cc534c 52 SINGLE_ARG_FUNC(log10)
jhnwkmn 0:97a4f8cc534c 53 SINGLE_ARG_FUNC(tan)
jhnwkmn 0:97a4f8cc534c 54 SINGLE_ARG_FUNC(atan)
jhnwkmn 0:97a4f8cc534c 55 TWO_ARGS_FUNC(atan2)
jhnwkmn 0:97a4f8cc534c 56 TWO_ARGS_FUNC(pow)
jhnwkmn 0:97a4f8cc534c 57 SINGLE_ARG_FUNC(floor)
jhnwkmn 0:97a4f8cc534c 58 SINGLE_ARG_FUNC(ceil)
jhnwkmn 0:97a4f8cc534c 59 SINGLE_ARG_FUNC(exp)
jhnwkmn 0:97a4f8cc534c 60
jhnwkmn 0:97a4f8cc534c 61 #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),math_##name,nparams,tycheck}
jhnwkmn 0:97a4f8cc534c 62 static SQRegFunction mathlib_funcs[] = {
jhnwkmn 0:97a4f8cc534c 63 _DECL_FUNC(sqrt,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 64 _DECL_FUNC(sin,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 65 _DECL_FUNC(cos,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 66 _DECL_FUNC(asin,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 67 _DECL_FUNC(acos,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 68 _DECL_FUNC(log,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 69 _DECL_FUNC(log10,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 70 _DECL_FUNC(tan,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 71 _DECL_FUNC(atan,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 72 _DECL_FUNC(atan2,3,_SC(".nn")),
jhnwkmn 0:97a4f8cc534c 73 _DECL_FUNC(pow,3,_SC(".nn")),
jhnwkmn 0:97a4f8cc534c 74 _DECL_FUNC(floor,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 75 _DECL_FUNC(ceil,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 76 _DECL_FUNC(exp,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 77 _DECL_FUNC(srand,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 78 _DECL_FUNC(rand,1,NULL),
jhnwkmn 0:97a4f8cc534c 79 _DECL_FUNC(fabs,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 80 _DECL_FUNC(abs,2,_SC(".n")),
jhnwkmn 0:97a4f8cc534c 81 {0,0},
jhnwkmn 0:97a4f8cc534c 82 };
jhnwkmn 0:97a4f8cc534c 83 #undef _DECL_FUNC
jhnwkmn 0:97a4f8cc534c 84
jhnwkmn 0:97a4f8cc534c 85 #ifndef M_PI
jhnwkmn 0:97a4f8cc534c 86 #define M_PI (3.14159265358979323846)
jhnwkmn 0:97a4f8cc534c 87 #endif
jhnwkmn 0:97a4f8cc534c 88
jhnwkmn 0:97a4f8cc534c 89 SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
jhnwkmn 0:97a4f8cc534c 90 {
jhnwkmn 0:97a4f8cc534c 91 SQInteger i=0;
jhnwkmn 0:97a4f8cc534c 92 while(mathlib_funcs[i].name!=0) {
jhnwkmn 0:97a4f8cc534c 93 sq_pushstring(v,mathlib_funcs[i].name,-1);
jhnwkmn 0:97a4f8cc534c 94 sq_newclosure(v,mathlib_funcs[i].f,0);
jhnwkmn 0:97a4f8cc534c 95 sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
jhnwkmn 0:97a4f8cc534c 96 sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
jhnwkmn 0:97a4f8cc534c 97 sq_newslot(v,-3,SQFalse);
jhnwkmn 0:97a4f8cc534c 98 i++;
jhnwkmn 0:97a4f8cc534c 99 }
jhnwkmn 0:97a4f8cc534c 100 sq_pushstring(v,_SC("RAND_MAX"),-1);
jhnwkmn 0:97a4f8cc534c 101 sq_pushinteger(v,RAND_MAX);
jhnwkmn 0:97a4f8cc534c 102 sq_newslot(v,-3,SQFalse);
jhnwkmn 0:97a4f8cc534c 103 sq_pushstring(v,_SC("PI"),-1);
jhnwkmn 0:97a4f8cc534c 104 sq_pushfloat(v,(SQFloat)M_PI);
jhnwkmn 0:97a4f8cc534c 105 sq_newslot(v,-3,SQFalse);
jhnwkmn 0:97a4f8cc534c 106 return SQ_OK;
jhnwkmn 0:97a4f8cc534c 107 }