mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2024-11-08 00:37:15 +01:00
Update squirrel to the latest version from the official repository.
Include the standard library that was filtered out by a mistake in the gitignore file.
This commit is contained in:
parent
93dcf84263
commit
a35643ae6e
@ -60,10 +60,10 @@ typedef wchar_t SQChar;
|
||||
|
||||
|
||||
#define scstrcmp wcscmp
|
||||
#ifdef _MSC_VER
|
||||
#ifdef _WIN32
|
||||
#define scsprintf _snwprintf
|
||||
#else
|
||||
#define scsprintf snwprintf
|
||||
#define scsprintf swprintf
|
||||
#endif
|
||||
#define scstrlen wcslen
|
||||
#define scstrtod wcstod
|
||||
|
130
external/Squirrel/Lib/sqstdaux.cpp
vendored
Normal file
130
external/Squirrel/Lib/sqstdaux.cpp
vendored
Normal file
@ -0,0 +1,130 @@
|
||||
/* see copyright notice in squirrel.h */
|
||||
#include <squirrel.h>
|
||||
#include <sqstdaux.h>
|
||||
#include <assert.h>
|
||||
|
||||
void sqstd_printcallstack(HSQUIRRELVM v)
|
||||
{
|
||||
SQPRINTFUNCTION pf = sq_geterrorfunc(v);
|
||||
if(pf) {
|
||||
SQStackInfos si;
|
||||
SQInteger i;
|
||||
SQFloat f;
|
||||
const SQChar *s;
|
||||
SQInteger level=1; //1 is to skip this function that is level 0
|
||||
const SQChar *name=0;
|
||||
SQInteger seq=0;
|
||||
pf(v,_SC("\nCALLSTACK\n"));
|
||||
while(SQ_SUCCEEDED(sq_stackinfos(v,level,&si)))
|
||||
{
|
||||
const SQChar *fn=_SC("unknown");
|
||||
const SQChar *src=_SC("unknown");
|
||||
if(si.funcname)fn=si.funcname;
|
||||
if(si.source)src=si.source;
|
||||
pf(v,_SC("*FUNCTION [%s()] %s line [%d]\n"),fn,src,si.line);
|
||||
level++;
|
||||
}
|
||||
level=0;
|
||||
pf(v,_SC("\nLOCALS\n"));
|
||||
|
||||
for(level=0;level<10;level++){
|
||||
seq=0;
|
||||
while((name = sq_getlocal(v,level,seq)))
|
||||
{
|
||||
seq++;
|
||||
switch(sq_gettype(v,-1))
|
||||
{
|
||||
case OT_NULL:
|
||||
pf(v,_SC("[%s] NULL\n"),name);
|
||||
break;
|
||||
case OT_INTEGER:
|
||||
sq_getinteger(v,-1,&i);
|
||||
pf(v,_SC("[%s] %d\n"),name,i);
|
||||
break;
|
||||
case OT_FLOAT:
|
||||
sq_getfloat(v,-1,&f);
|
||||
pf(v,_SC("[%s] %.14g\n"),name,f);
|
||||
break;
|
||||
case OT_USERPOINTER:
|
||||
pf(v,_SC("[%s] USERPOINTER\n"),name);
|
||||
break;
|
||||
case OT_STRING:
|
||||
sq_getstring(v,-1,&s);
|
||||
pf(v,_SC("[%s] \"%s\"\n"),name,s);
|
||||
break;
|
||||
case OT_TABLE:
|
||||
pf(v,_SC("[%s] TABLE\n"),name);
|
||||
break;
|
||||
case OT_ARRAY:
|
||||
pf(v,_SC("[%s] ARRAY\n"),name);
|
||||
break;
|
||||
case OT_CLOSURE:
|
||||
pf(v,_SC("[%s] CLOSURE\n"),name);
|
||||
break;
|
||||
case OT_NATIVECLOSURE:
|
||||
pf(v,_SC("[%s] NATIVECLOSURE\n"),name);
|
||||
break;
|
||||
case OT_GENERATOR:
|
||||
pf(v,_SC("[%s] GENERATOR\n"),name);
|
||||
break;
|
||||
case OT_USERDATA:
|
||||
pf(v,_SC("[%s] USERDATA\n"),name);
|
||||
break;
|
||||
case OT_THREAD:
|
||||
pf(v,_SC("[%s] THREAD\n"),name);
|
||||
break;
|
||||
case OT_CLASS:
|
||||
pf(v,_SC("[%s] CLASS\n"),name);
|
||||
break;
|
||||
case OT_INSTANCE:
|
||||
pf(v,_SC("[%s] INSTANCE\n"),name);
|
||||
break;
|
||||
case OT_WEAKREF:
|
||||
pf(v,_SC("[%s] WEAKREF\n"),name);
|
||||
break;
|
||||
case OT_BOOL:{
|
||||
SQBool bval;
|
||||
sq_getbool(v,-1,&bval);
|
||||
pf(v,_SC("[%s] %s\n"),name,bval == SQTrue ? _SC("true"):_SC("false"));
|
||||
}
|
||||
break;
|
||||
default: assert(0); break;
|
||||
}
|
||||
sq_pop(v,1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static SQInteger _sqstd_aux_printerror(HSQUIRRELVM v)
|
||||
{
|
||||
SQPRINTFUNCTION pf = sq_geterrorfunc(v);
|
||||
if(pf) {
|
||||
const SQChar *sErr = 0;
|
||||
if(sq_gettop(v)>=1) {
|
||||
if(SQ_SUCCEEDED(sq_getstring(v,2,&sErr))) {
|
||||
pf(v,_SC("\nAN ERROR HAS OCCURED [%s]\n"),sErr);
|
||||
}
|
||||
else{
|
||||
pf(v,_SC("\nAN ERROR HAS OCCURED [unknown]\n"));
|
||||
}
|
||||
sqstd_printcallstack(v);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void _sqstd_compiler_error(HSQUIRRELVM v,const SQChar *sErr,const SQChar *sSource,SQInteger line,SQInteger column)
|
||||
{
|
||||
SQPRINTFUNCTION pf = sq_geterrorfunc(v);
|
||||
if(pf) {
|
||||
pf(v,_SC("%s line = (%d) column = (%d) : error %s\n"),sSource,line,column,sErr);
|
||||
}
|
||||
}
|
||||
|
||||
void sqstd_seterrorhandlers(HSQUIRRELVM v)
|
||||
{
|
||||
sq_setcompilererrorhandler(v,_sqstd_compiler_error);
|
||||
sq_newclosure(v,_sqstd_aux_printerror,0);
|
||||
sq_seterrorhandler(v);
|
||||
}
|
277
external/Squirrel/Lib/sqstdblob.cpp
vendored
Normal file
277
external/Squirrel/Lib/sqstdblob.cpp
vendored
Normal file
@ -0,0 +1,277 @@
|
||||
/* see copyright notice in squirrel.h */
|
||||
#include <new>
|
||||
#include <squirrel.h>
|
||||
#include <sqstdio.h>
|
||||
#include <string.h>
|
||||
#include <sqstdblob.h>
|
||||
#include "sqstdstream.h"
|
||||
#include "sqstdblobimpl.h"
|
||||
|
||||
#define SQSTD_BLOB_TYPE_TAG (SQSTD_STREAM_TYPE_TAG | 0x00000002)
|
||||
|
||||
//Blob
|
||||
|
||||
|
||||
#define SETUP_BLOB(v) \
|
||||
SQBlob *self = NULL; \
|
||||
{ if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) \
|
||||
return sq_throwerror(v,_SC("invalid type tag")); } \
|
||||
if(!self || !self->IsValid()) \
|
||||
return sq_throwerror(v,_SC("the blob is invalid"));
|
||||
|
||||
|
||||
static SQInteger _blob_resize(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_BLOB(v);
|
||||
SQInteger size;
|
||||
sq_getinteger(v,2,&size);
|
||||
if(!self->Resize(size))
|
||||
return sq_throwerror(v,_SC("resize failed"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __swap_dword(unsigned int *n)
|
||||
{
|
||||
*n=(unsigned int)(((*n&0xFF000000)>>24) |
|
||||
((*n&0x00FF0000)>>8) |
|
||||
((*n&0x0000FF00)<<8) |
|
||||
((*n&0x000000FF)<<24));
|
||||
}
|
||||
|
||||
static void __swap_word(unsigned short *n)
|
||||
{
|
||||
*n=(unsigned short)((*n>>8)&0x00FF)| ((*n<<8)&0xFF00);
|
||||
}
|
||||
|
||||
static SQInteger _blob_swap4(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_BLOB(v);
|
||||
SQInteger num=(self->Len()-(self->Len()%4))>>2;
|
||||
unsigned int *t=(unsigned int *)self->GetBuf();
|
||||
for(SQInteger i = 0; i < num; i++) {
|
||||
__swap_dword(&t[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SQInteger _blob_swap2(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_BLOB(v);
|
||||
SQInteger num=(self->Len()-(self->Len()%2))>>1;
|
||||
unsigned short *t = (unsigned short *)self->GetBuf();
|
||||
for(SQInteger i = 0; i < num; i++) {
|
||||
__swap_word(&t[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SQInteger _blob__set(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_BLOB(v);
|
||||
SQInteger idx,val;
|
||||
sq_getinteger(v,2,&idx);
|
||||
sq_getinteger(v,3,&val);
|
||||
if(idx < 0 || idx >= self->Len())
|
||||
return sq_throwerror(v,_SC("index out of range"));
|
||||
((unsigned char *)self->GetBuf())[idx] = (unsigned char) val;
|
||||
sq_push(v,3);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _blob__get(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_BLOB(v);
|
||||
SQInteger idx;
|
||||
sq_getinteger(v,2,&idx);
|
||||
if(idx < 0 || idx >= self->Len())
|
||||
return sq_throwerror(v,_SC("index out of range"));
|
||||
sq_pushinteger(v,((unsigned char *)self->GetBuf())[idx]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _blob__nexti(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_BLOB(v);
|
||||
if(sq_gettype(v,2) == OT_NULL) {
|
||||
sq_pushinteger(v, 0);
|
||||
return 1;
|
||||
}
|
||||
SQInteger idx;
|
||||
if(SQ_SUCCEEDED(sq_getinteger(v, 2, &idx))) {
|
||||
if(idx+1 < self->Len()) {
|
||||
sq_pushinteger(v, idx+1);
|
||||
return 1;
|
||||
}
|
||||
sq_pushnull(v);
|
||||
return 1;
|
||||
}
|
||||
return sq_throwerror(v,_SC("internal error (_nexti) wrong argument type"));
|
||||
}
|
||||
|
||||
static SQInteger _blob__typeof(HSQUIRRELVM v)
|
||||
{
|
||||
sq_pushstring(v,_SC("blob"),-1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _blob_releasehook(SQUserPointer p, SQInteger SQ_UNUSED_ARG(size))
|
||||
{
|
||||
SQBlob *self = (SQBlob*)p;
|
||||
self->~SQBlob();
|
||||
sq_free(self,sizeof(SQBlob));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _blob_constructor(HSQUIRRELVM v)
|
||||
{
|
||||
SQInteger nparam = sq_gettop(v);
|
||||
SQInteger size = 0;
|
||||
if(nparam == 2) {
|
||||
sq_getinteger(v, 2, &size);
|
||||
}
|
||||
if(size < 0) return sq_throwerror(v, _SC("cannot create blob with negative size"));
|
||||
//SQBlob *b = new SQBlob(size);
|
||||
|
||||
SQBlob *b = new (sq_malloc(sizeof(SQBlob)))SQBlob(size);
|
||||
if(SQ_FAILED(sq_setinstanceup(v,1,b))) {
|
||||
b->~SQBlob();
|
||||
sq_free(b,sizeof(SQBlob));
|
||||
return sq_throwerror(v, _SC("cannot create blob"));
|
||||
}
|
||||
sq_setreleasehook(v,1,_blob_releasehook);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SQInteger _blob__cloned(HSQUIRRELVM v)
|
||||
{
|
||||
SQBlob *other = NULL;
|
||||
{
|
||||
if(SQ_FAILED(sq_getinstanceup(v,2,(SQUserPointer*)&other,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
|
||||
return SQ_ERROR;
|
||||
}
|
||||
//SQBlob *thisone = new SQBlob(other->Len());
|
||||
SQBlob *thisone = new (sq_malloc(sizeof(SQBlob)))SQBlob(other->Len());
|
||||
memcpy(thisone->GetBuf(),other->GetBuf(),thisone->Len());
|
||||
if(SQ_FAILED(sq_setinstanceup(v,1,thisone))) {
|
||||
thisone->~SQBlob();
|
||||
sq_free(thisone,sizeof(SQBlob));
|
||||
return sq_throwerror(v, _SC("cannot clone blob"));
|
||||
}
|
||||
sq_setreleasehook(v,1,_blob_releasehook);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define _DECL_BLOB_FUNC(name,nparams,typecheck) {_SC(#name),_blob_##name,nparams,typecheck}
|
||||
static const SQRegFunction _blob_methods[] = {
|
||||
_DECL_BLOB_FUNC(constructor,-1,_SC("xn")),
|
||||
_DECL_BLOB_FUNC(resize,2,_SC("xn")),
|
||||
_DECL_BLOB_FUNC(swap2,1,_SC("x")),
|
||||
_DECL_BLOB_FUNC(swap4,1,_SC("x")),
|
||||
_DECL_BLOB_FUNC(_set,3,_SC("xnn")),
|
||||
_DECL_BLOB_FUNC(_get,2,_SC("xn")),
|
||||
_DECL_BLOB_FUNC(_typeof,1,_SC("x")),
|
||||
_DECL_BLOB_FUNC(_nexti,2,_SC("x")),
|
||||
_DECL_BLOB_FUNC(_cloned,2,_SC("xx")),
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
|
||||
|
||||
//GLOBAL FUNCTIONS
|
||||
|
||||
static SQInteger _g_blob_casti2f(HSQUIRRELVM v)
|
||||
{
|
||||
SQInteger i;
|
||||
sq_getinteger(v,2,&i);
|
||||
sq_pushfloat(v,*((const SQFloat *)&i));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _g_blob_castf2i(HSQUIRRELVM v)
|
||||
{
|
||||
SQFloat f;
|
||||
sq_getfloat(v,2,&f);
|
||||
sq_pushinteger(v,*((const SQInteger *)&f));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _g_blob_swap2(HSQUIRRELVM v)
|
||||
{
|
||||
SQInteger i;
|
||||
sq_getinteger(v,2,&i);
|
||||
short s=(short)i;
|
||||
sq_pushinteger(v,(s<<8)|((s>>8)&0x00FF));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _g_blob_swap4(HSQUIRRELVM v)
|
||||
{
|
||||
SQInteger i;
|
||||
sq_getinteger(v,2,&i);
|
||||
unsigned int t4 = (unsigned int)i;
|
||||
__swap_dword(&t4);
|
||||
sq_pushinteger(v,(SQInteger)t4);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _g_blob_swapfloat(HSQUIRRELVM v)
|
||||
{
|
||||
SQFloat f;
|
||||
sq_getfloat(v,2,&f);
|
||||
__swap_dword((unsigned int *)&f);
|
||||
sq_pushfloat(v,f);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define _DECL_GLOBALBLOB_FUNC(name,nparams,typecheck) {_SC(#name),_g_blob_##name,nparams,typecheck}
|
||||
static const SQRegFunction bloblib_funcs[]={
|
||||
_DECL_GLOBALBLOB_FUNC(casti2f,2,_SC(".n")),
|
||||
_DECL_GLOBALBLOB_FUNC(castf2i,2,_SC(".n")),
|
||||
_DECL_GLOBALBLOB_FUNC(swap2,2,_SC(".n")),
|
||||
_DECL_GLOBALBLOB_FUNC(swap4,2,_SC(".n")),
|
||||
_DECL_GLOBALBLOB_FUNC(swapfloat,2,_SC(".n")),
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
SQRESULT sqstd_getblob(HSQUIRRELVM v,SQInteger idx,SQUserPointer *ptr)
|
||||
{
|
||||
SQBlob *blob;
|
||||
if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
|
||||
return -1;
|
||||
*ptr = blob->GetBuf();
|
||||
return SQ_OK;
|
||||
}
|
||||
|
||||
SQInteger sqstd_getblobsize(HSQUIRRELVM v,SQInteger idx)
|
||||
{
|
||||
SQBlob *blob;
|
||||
if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
|
||||
return -1;
|
||||
return blob->Len();
|
||||
}
|
||||
|
||||
SQUserPointer sqstd_createblob(HSQUIRRELVM v, SQInteger size)
|
||||
{
|
||||
SQInteger top = sq_gettop(v);
|
||||
sq_pushregistrytable(v);
|
||||
sq_pushstring(v,_SC("std_blob"),-1);
|
||||
if(SQ_SUCCEEDED(sq_get(v,-2))) {
|
||||
sq_remove(v,-2); //removes the registry
|
||||
sq_push(v,1); // push the this
|
||||
sq_pushinteger(v,size); //size
|
||||
SQBlob *blob = NULL;
|
||||
if(SQ_SUCCEEDED(sq_call(v,2,SQTrue,SQFalse))
|
||||
&& SQ_SUCCEEDED(sq_getinstanceup(v,-1,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) {
|
||||
sq_remove(v,-2);
|
||||
return blob->GetBuf();
|
||||
}
|
||||
}
|
||||
sq_settop(v,top);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SQRESULT sqstd_register_bloblib(HSQUIRRELVM v)
|
||||
{
|
||||
return declare_stream(v,_SC("blob"),(SQUserPointer)SQSTD_BLOB_TYPE_TAG,_SC("std_blob"),_blob_methods,bloblib_funcs);
|
||||
}
|
||||
|
108
external/Squirrel/Lib/sqstdblobimpl.h
vendored
Normal file
108
external/Squirrel/Lib/sqstdblobimpl.h
vendored
Normal file
@ -0,0 +1,108 @@
|
||||
/* see copyright notice in squirrel.h */
|
||||
#ifndef _SQSTD_BLOBIMPL_H_
|
||||
#define _SQSTD_BLOBIMPL_H_
|
||||
|
||||
struct SQBlob : public SQStream
|
||||
{
|
||||
SQBlob(SQInteger size) {
|
||||
_size = size;
|
||||
_allocated = size;
|
||||
_buf = (unsigned char *)sq_malloc(size);
|
||||
memset(_buf, 0, _size);
|
||||
_ptr = 0;
|
||||
_owns = true;
|
||||
}
|
||||
virtual ~SQBlob() {
|
||||
sq_free(_buf, _allocated);
|
||||
}
|
||||
SQInteger Write(void *buffer, SQInteger size) {
|
||||
if(!CanAdvance(size)) {
|
||||
GrowBufOf(_ptr + size - _size);
|
||||
}
|
||||
memcpy(&_buf[_ptr], buffer, size);
|
||||
_ptr += size;
|
||||
return size;
|
||||
}
|
||||
SQInteger Read(void *buffer,SQInteger size) {
|
||||
SQInteger n = size;
|
||||
if(!CanAdvance(size)) {
|
||||
if((_size - _ptr) > 0)
|
||||
n = _size - _ptr;
|
||||
else return 0;
|
||||
}
|
||||
memcpy(buffer, &_buf[_ptr], n);
|
||||
_ptr += n;
|
||||
return n;
|
||||
}
|
||||
bool Resize(SQInteger n) {
|
||||
if(!_owns) return false;
|
||||
if(n != _allocated) {
|
||||
unsigned char *newbuf = (unsigned char *)sq_malloc(n);
|
||||
memset(newbuf,0,n);
|
||||
if(_size > n)
|
||||
memcpy(newbuf,_buf,n);
|
||||
else
|
||||
memcpy(newbuf,_buf,_size);
|
||||
sq_free(_buf,_allocated);
|
||||
_buf=newbuf;
|
||||
_allocated = n;
|
||||
if(_size > _allocated)
|
||||
_size = _allocated;
|
||||
if(_ptr > _allocated)
|
||||
_ptr = _allocated;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool GrowBufOf(SQInteger n)
|
||||
{
|
||||
bool ret = true;
|
||||
if(_size + n > _allocated) {
|
||||
if(_size + n > _size * 2)
|
||||
ret = Resize(_size + n);
|
||||
else
|
||||
ret = Resize(_size * 2);
|
||||
}
|
||||
_size = _size + n;
|
||||
return ret;
|
||||
}
|
||||
bool CanAdvance(SQInteger n) {
|
||||
if(_ptr+n>_size)return false;
|
||||
return true;
|
||||
}
|
||||
SQInteger Seek(SQInteger offset, SQInteger origin) {
|
||||
switch(origin) {
|
||||
case SQ_SEEK_SET:
|
||||
if(offset > _size || offset < 0) return -1;
|
||||
_ptr = offset;
|
||||
break;
|
||||
case SQ_SEEK_CUR:
|
||||
if(_ptr + offset > _size || _ptr + offset < 0) return -1;
|
||||
_ptr += offset;
|
||||
break;
|
||||
case SQ_SEEK_END:
|
||||
if(_size + offset > _size || _size + offset < 0) return -1;
|
||||
_ptr = _size + offset;
|
||||
break;
|
||||
default: return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
bool IsValid() {
|
||||
return _buf?true:false;
|
||||
}
|
||||
bool EOS() {
|
||||
return _ptr == _size;
|
||||
}
|
||||
SQInteger Flush() { return 0; }
|
||||
SQInteger Tell() { return _ptr; }
|
||||
SQInteger Len() { return _size; }
|
||||
SQUserPointer GetBuf(){ return _buf; }
|
||||
private:
|
||||
SQInteger _size;
|
||||
SQInteger _allocated;
|
||||
SQInteger _ptr;
|
||||
unsigned char *_buf;
|
||||
bool _owns;
|
||||
};
|
||||
|
||||
#endif //_SQSTD_BLOBIMPL_H_
|
485
external/Squirrel/Lib/sqstdio.cpp
vendored
Normal file
485
external/Squirrel/Lib/sqstdio.cpp
vendored
Normal file
@ -0,0 +1,485 @@
|
||||
/* see copyright notice in squirrel.h */
|
||||
#include <new>
|
||||
#include <stdio.h>
|
||||
#include <squirrel.h>
|
||||
#include <sqstdio.h>
|
||||
#include "sqstdstream.h"
|
||||
|
||||
#define SQSTD_FILE_TYPE_TAG (SQSTD_STREAM_TYPE_TAG | 0x00000001)
|
||||
//basic API
|
||||
SQFILE sqstd_fopen(const SQChar *filename ,const SQChar *mode)
|
||||
{
|
||||
#ifndef SQUNICODE
|
||||
return (SQFILE)fopen(filename,mode);
|
||||
#else
|
||||
return (SQFILE)_wfopen(filename,mode);
|
||||
#endif
|
||||
}
|
||||
|
||||
SQInteger sqstd_fread(void* buffer, SQInteger size, SQInteger count, SQFILE file)
|
||||
{
|
||||
SQInteger ret = (SQInteger)fread(buffer,size,count,(FILE *)file);
|
||||
return ret;
|
||||
}
|
||||
|
||||
SQInteger sqstd_fwrite(const SQUserPointer buffer, SQInteger size, SQInteger count, SQFILE file)
|
||||
{
|
||||
return (SQInteger)fwrite(buffer,size,count,(FILE *)file);
|
||||
}
|
||||
|
||||
SQInteger sqstd_fseek(SQFILE file, SQInteger offset, SQInteger origin)
|
||||
{
|
||||
SQInteger realorigin;
|
||||
switch(origin) {
|
||||
case SQ_SEEK_CUR: realorigin = SEEK_CUR; break;
|
||||
case SQ_SEEK_END: realorigin = SEEK_END; break;
|
||||
case SQ_SEEK_SET: realorigin = SEEK_SET; break;
|
||||
default: return -1; //failed
|
||||
}
|
||||
return fseek((FILE *)file,(long)offset,(int)realorigin);
|
||||
}
|
||||
|
||||
SQInteger sqstd_ftell(SQFILE file)
|
||||
{
|
||||
return ftell((FILE *)file);
|
||||
}
|
||||
|
||||
SQInteger sqstd_fflush(SQFILE file)
|
||||
{
|
||||
return fflush((FILE *)file);
|
||||
}
|
||||
|
||||
SQInteger sqstd_fclose(SQFILE file)
|
||||
{
|
||||
return fclose((FILE *)file);
|
||||
}
|
||||
|
||||
SQInteger sqstd_feof(SQFILE file)
|
||||
{
|
||||
return feof((FILE *)file);
|
||||
}
|
||||
|
||||
//File
|
||||
struct SQFile : public SQStream {
|
||||
SQFile() { _handle = NULL; _owns = false;}
|
||||
SQFile(SQFILE file, bool owns) { _handle = file; _owns = owns;}
|
||||
virtual ~SQFile() { Close(); }
|
||||
bool Open(const SQChar *filename ,const SQChar *mode) {
|
||||
Close();
|
||||
if( (_handle = sqstd_fopen(filename,mode)) ) {
|
||||
_owns = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void Close() {
|
||||
if(_handle && _owns) {
|
||||
sqstd_fclose(_handle);
|
||||
_handle = NULL;
|
||||
_owns = false;
|
||||
}
|
||||
}
|
||||
SQInteger Read(void *buffer,SQInteger size) {
|
||||
return sqstd_fread(buffer,1,size,_handle);
|
||||
}
|
||||
SQInteger Write(void *buffer,SQInteger size) {
|
||||
return sqstd_fwrite(buffer,1,size,_handle);
|
||||
}
|
||||
SQInteger Flush() {
|
||||
return sqstd_fflush(_handle);
|
||||
}
|
||||
SQInteger Tell() {
|
||||
return sqstd_ftell(_handle);
|
||||
}
|
||||
SQInteger Len() {
|
||||
SQInteger prevpos=Tell();
|
||||
Seek(0,SQ_SEEK_END);
|
||||
SQInteger size=Tell();
|
||||
Seek(prevpos,SQ_SEEK_SET);
|
||||
return size;
|
||||
}
|
||||
SQInteger Seek(SQInteger offset, SQInteger origin) {
|
||||
return sqstd_fseek(_handle,offset,origin);
|
||||
}
|
||||
bool IsValid() { return _handle?true:false; }
|
||||
bool EOS() { return Tell()==Len()?true:false;}
|
||||
SQFILE GetHandle() {return _handle;}
|
||||
private:
|
||||
SQFILE _handle;
|
||||
bool _owns;
|
||||
};
|
||||
|
||||
static SQInteger _file__typeof(HSQUIRRELVM v)
|
||||
{
|
||||
sq_pushstring(v,_SC("file"),-1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _file_releasehook(SQUserPointer p, SQInteger SQ_UNUSED_ARG(size))
|
||||
{
|
||||
SQFile *self = (SQFile*)p;
|
||||
self->~SQFile();
|
||||
sq_free(self,sizeof(SQFile));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _file_constructor(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *filename,*mode;
|
||||
bool owns = true;
|
||||
SQFile *f;
|
||||
SQFILE newf;
|
||||
if(sq_gettype(v,2) == OT_STRING && sq_gettype(v,3) == OT_STRING) {
|
||||
sq_getstring(v, 2, &filename);
|
||||
sq_getstring(v, 3, &mode);
|
||||
newf = sqstd_fopen(filename, mode);
|
||||
if(!newf) return sq_throwerror(v, _SC("cannot open file"));
|
||||
} else if(sq_gettype(v,2) == OT_USERPOINTER) {
|
||||
owns = !(sq_gettype(v,3) == OT_NULL);
|
||||
sq_getuserpointer(v,2,&newf);
|
||||
} else {
|
||||
return sq_throwerror(v,_SC("wrong parameter"));
|
||||
}
|
||||
|
||||
f = new (sq_malloc(sizeof(SQFile)))SQFile(newf,owns);
|
||||
if(SQ_FAILED(sq_setinstanceup(v,1,f))) {
|
||||
f->~SQFile();
|
||||
sq_free(f,sizeof(SQFile));
|
||||
return sq_throwerror(v, _SC("cannot create blob with negative size"));
|
||||
}
|
||||
sq_setreleasehook(v,1,_file_releasehook);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SQInteger _file_close(HSQUIRRELVM v)
|
||||
{
|
||||
SQFile *self = NULL;
|
||||
if(SQ_SUCCEEDED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_FILE_TYPE_TAG))
|
||||
&& self != NULL)
|
||||
{
|
||||
self->Close();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//bindings
|
||||
#define _DECL_FILE_FUNC(name,nparams,typecheck) {_SC(#name),_file_##name,nparams,typecheck}
|
||||
static const SQRegFunction _file_methods[] = {
|
||||
_DECL_FILE_FUNC(constructor,3,_SC("x")),
|
||||
_DECL_FILE_FUNC(_typeof,1,_SC("x")),
|
||||
_DECL_FILE_FUNC(close,1,_SC("x")),
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
|
||||
|
||||
SQRESULT sqstd_createfile(HSQUIRRELVM v, SQFILE file,SQBool own)
|
||||
{
|
||||
SQInteger top = sq_gettop(v);
|
||||
sq_pushregistrytable(v);
|
||||
sq_pushstring(v,_SC("std_file"),-1);
|
||||
if(SQ_SUCCEEDED(sq_get(v,-2))) {
|
||||
sq_remove(v,-2); //removes the registry
|
||||
sq_pushroottable(v); // push the this
|
||||
sq_pushuserpointer(v,file); //file
|
||||
if(own){
|
||||
sq_pushinteger(v,1); //true
|
||||
}
|
||||
else{
|
||||
sq_pushnull(v); //false
|
||||
}
|
||||
if(SQ_SUCCEEDED( sq_call(v,3,SQTrue,SQFalse) )) {
|
||||
sq_remove(v,-2);
|
||||
return SQ_OK;
|
||||
}
|
||||
}
|
||||
sq_settop(v,top);
|
||||
return SQ_ERROR;
|
||||
}
|
||||
|
||||
SQRESULT sqstd_getfile(HSQUIRRELVM v, SQInteger idx, SQFILE *file)
|
||||
{
|
||||
SQFile *fileobj = NULL;
|
||||
if(SQ_SUCCEEDED(sq_getinstanceup(v,idx,(SQUserPointer*)&fileobj,(SQUserPointer)SQSTD_FILE_TYPE_TAG))) {
|
||||
*file = fileobj->GetHandle();
|
||||
return SQ_OK;
|
||||
}
|
||||
return sq_throwerror(v,_SC("not a file"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define IO_BUFFER_SIZE 2048
|
||||
struct IOBuffer {
|
||||
unsigned char buffer[IO_BUFFER_SIZE];
|
||||
SQInteger size;
|
||||
SQInteger ptr;
|
||||
SQFILE file;
|
||||
};
|
||||
|
||||
SQInteger _read_byte(IOBuffer *iobuffer)
|
||||
{
|
||||
if(iobuffer->ptr < iobuffer->size) {
|
||||
|
||||
SQInteger ret = iobuffer->buffer[iobuffer->ptr];
|
||||
iobuffer->ptr++;
|
||||
return ret;
|
||||
}
|
||||
else {
|
||||
if( (iobuffer->size = sqstd_fread(iobuffer->buffer,1,IO_BUFFER_SIZE,iobuffer->file )) > 0 )
|
||||
{
|
||||
SQInteger ret = iobuffer->buffer[0];
|
||||
iobuffer->ptr = 1;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SQInteger _read_two_bytes(IOBuffer *iobuffer)
|
||||
{
|
||||
if(iobuffer->ptr < iobuffer->size) {
|
||||
if(iobuffer->size < 2) return 0;
|
||||
SQInteger ret = *((const wchar_t*)&iobuffer->buffer[iobuffer->ptr]);
|
||||
iobuffer->ptr += 2;
|
||||
return ret;
|
||||
}
|
||||
else {
|
||||
if( (iobuffer->size = sqstd_fread(iobuffer->buffer,1,IO_BUFFER_SIZE,iobuffer->file )) > 0 )
|
||||
{
|
||||
if(iobuffer->size < 2) return 0;
|
||||
SQInteger ret = *((const wchar_t*)&iobuffer->buffer[0]);
|
||||
iobuffer->ptr = 2;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SQInteger _io_file_lexfeed_PLAIN(SQUserPointer iobuf)
|
||||
{
|
||||
IOBuffer *iobuffer = (IOBuffer *)iobuf;
|
||||
return _read_byte(iobuffer);
|
||||
|
||||
}
|
||||
|
||||
#ifdef SQUNICODE
|
||||
static SQInteger _io_file_lexfeed_UTF8(SQUserPointer iobuf)
|
||||
{
|
||||
IOBuffer *iobuffer = (IOBuffer *)iobuf;
|
||||
#define READ(iobuf) \
|
||||
if((inchar = (unsigned char)_read_byte(iobuf)) == 0) \
|
||||
return 0;
|
||||
|
||||
static const SQInteger utf8_lengths[16] =
|
||||
{
|
||||
1,1,1,1,1,1,1,1, /* 0000 to 0111 : 1 byte (plain ASCII) */
|
||||
0,0,0,0, /* 1000 to 1011 : not valid */
|
||||
2,2, /* 1100, 1101 : 2 bytes */
|
||||
3, /* 1110 : 3 bytes */
|
||||
4 /* 1111 :4 bytes */
|
||||
};
|
||||
static const unsigned char byte_masks[5] = {0,0,0x1f,0x0f,0x07};
|
||||
unsigned char inchar;
|
||||
SQInteger c = 0;
|
||||
READ(iobuffer);
|
||||
c = inchar;
|
||||
//
|
||||
if(c >= 0x80) {
|
||||
SQInteger tmp;
|
||||
SQInteger codelen = utf8_lengths[c>>4];
|
||||
if(codelen == 0)
|
||||
return 0;
|
||||
//"invalid UTF-8 stream";
|
||||
tmp = c&byte_masks[codelen];
|
||||
for(SQInteger n = 0; n < codelen-1; n++) {
|
||||
tmp<<=6;
|
||||
READ(iobuffer);
|
||||
tmp |= inchar & 0x3F;
|
||||
}
|
||||
c = tmp;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
#endif
|
||||
|
||||
static SQInteger _io_file_lexfeed_UCS2_LE(SQUserPointer iobuf)
|
||||
{
|
||||
SQInteger ret;
|
||||
IOBuffer *iobuffer = (IOBuffer *)iobuf;
|
||||
if( (ret = _read_two_bytes(iobuffer)) > 0 )
|
||||
return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SQInteger _io_file_lexfeed_UCS2_BE(SQUserPointer iobuf)
|
||||
{
|
||||
SQInteger c;
|
||||
IOBuffer *iobuffer = (IOBuffer *)iobuf;
|
||||
if( (c = _read_two_bytes(iobuffer)) > 0 ) {
|
||||
c = ((c>>8)&0x00FF)| ((c<<8)&0xFF00);
|
||||
return c;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
SQInteger file_read(SQUserPointer file,SQUserPointer buf,SQInteger size)
|
||||
{
|
||||
SQInteger ret;
|
||||
if( ( ret = sqstd_fread(buf,1,size,(SQFILE)file ))!=0 )return ret;
|
||||
return -1;
|
||||
}
|
||||
|
||||
SQInteger file_write(SQUserPointer file,SQUserPointer p,SQInteger size)
|
||||
{
|
||||
return sqstd_fwrite(p,1,size,(SQFILE)file);
|
||||
}
|
||||
|
||||
SQRESULT sqstd_loadfile(HSQUIRRELVM v,const SQChar *filename,SQBool printerror)
|
||||
{
|
||||
SQFILE file = sqstd_fopen(filename,_SC("rb"));
|
||||
|
||||
SQInteger ret;
|
||||
unsigned short us;
|
||||
unsigned char uc;
|
||||
SQLEXREADFUNC func = _io_file_lexfeed_PLAIN;
|
||||
if(file){
|
||||
ret = sqstd_fread(&us,1,2,file);
|
||||
if(ret != 2) {
|
||||
//probably an empty file
|
||||
us = 0;
|
||||
}
|
||||
if(us == SQ_BYTECODE_STREAM_TAG) { //BYTECODE
|
||||
sqstd_fseek(file,0,SQ_SEEK_SET);
|
||||
if(SQ_SUCCEEDED(sq_readclosure(v,file_read,file))) {
|
||||
sqstd_fclose(file);
|
||||
return SQ_OK;
|
||||
}
|
||||
}
|
||||
else { //SCRIPT
|
||||
|
||||
switch(us)
|
||||
{
|
||||
//gotta swap the next 2 lines on BIG endian machines
|
||||
case 0xFFFE: func = _io_file_lexfeed_UCS2_BE; break;//UTF-16 little endian;
|
||||
case 0xFEFF: func = _io_file_lexfeed_UCS2_LE; break;//UTF-16 big endian;
|
||||
case 0xBBEF:
|
||||
if(sqstd_fread(&uc,1,sizeof(uc),file) == 0) {
|
||||
sqstd_fclose(file);
|
||||
return sq_throwerror(v,_SC("io error"));
|
||||
}
|
||||
if(uc != 0xBF) {
|
||||
sqstd_fclose(file);
|
||||
return sq_throwerror(v,_SC("Unrecognozed ecoding"));
|
||||
}
|
||||
#ifdef SQUNICODE
|
||||
func = _io_file_lexfeed_UTF8;
|
||||
#else
|
||||
func = _io_file_lexfeed_PLAIN;
|
||||
#endif
|
||||
break;//UTF-8 ;
|
||||
default: sqstd_fseek(file,0,SQ_SEEK_SET); break; // ascii
|
||||
}
|
||||
IOBuffer buffer;
|
||||
buffer.ptr = 0;
|
||||
buffer.size = 0;
|
||||
buffer.file = file;
|
||||
if(SQ_SUCCEEDED(sq_compile(v,func,&buffer,filename,printerror))){
|
||||
sqstd_fclose(file);
|
||||
return SQ_OK;
|
||||
}
|
||||
}
|
||||
sqstd_fclose(file);
|
||||
return SQ_ERROR;
|
||||
}
|
||||
return sq_throwerror(v,_SC("cannot open the file"));
|
||||
}
|
||||
|
||||
SQRESULT sqstd_dofile(HSQUIRRELVM v,const SQChar *filename,SQBool retval,SQBool printerror)
|
||||
{
|
||||
if(SQ_SUCCEEDED(sqstd_loadfile(v,filename,printerror))) {
|
||||
sq_push(v,-2);
|
||||
if(SQ_SUCCEEDED(sq_call(v,1,retval,SQTrue))) {
|
||||
sq_remove(v,retval?-2:-1); //removes the closure
|
||||
return 1;
|
||||
}
|
||||
sq_pop(v,1); //removes the closure
|
||||
}
|
||||
return SQ_ERROR;
|
||||
}
|
||||
|
||||
SQRESULT sqstd_writeclosuretofile(HSQUIRRELVM v,const SQChar *filename)
|
||||
{
|
||||
SQFILE file = sqstd_fopen(filename,_SC("wb+"));
|
||||
if(!file) return sq_throwerror(v,_SC("cannot open the file"));
|
||||
if(SQ_SUCCEEDED(sq_writeclosure(v,file_write,file))) {
|
||||
sqstd_fclose(file);
|
||||
return SQ_OK;
|
||||
}
|
||||
sqstd_fclose(file);
|
||||
return SQ_ERROR; //forward the error
|
||||
}
|
||||
|
||||
SQInteger _g_io_loadfile(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *filename;
|
||||
SQBool printerror = SQFalse;
|
||||
sq_getstring(v,2,&filename);
|
||||
if(sq_gettop(v) >= 3) {
|
||||
sq_getbool(v,3,&printerror);
|
||||
}
|
||||
if(SQ_SUCCEEDED(sqstd_loadfile(v,filename,printerror)))
|
||||
return 1;
|
||||
return SQ_ERROR; //propagates the error
|
||||
}
|
||||
|
||||
SQInteger _g_io_writeclosuretofile(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *filename;
|
||||
sq_getstring(v,2,&filename);
|
||||
if(SQ_SUCCEEDED(sqstd_writeclosuretofile(v,filename)))
|
||||
return 1;
|
||||
return SQ_ERROR; //propagates the error
|
||||
}
|
||||
|
||||
SQInteger _g_io_dofile(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *filename;
|
||||
SQBool printerror = SQFalse;
|
||||
sq_getstring(v,2,&filename);
|
||||
if(sq_gettop(v) >= 3) {
|
||||
sq_getbool(v,3,&printerror);
|
||||
}
|
||||
sq_push(v,1); //repush the this
|
||||
if(SQ_SUCCEEDED(sqstd_dofile(v,filename,SQTrue,printerror)))
|
||||
return 1;
|
||||
return SQ_ERROR; //propagates the error
|
||||
}
|
||||
|
||||
#define _DECL_GLOBALIO_FUNC(name,nparams,typecheck) {_SC(#name),_g_io_##name,nparams,typecheck}
|
||||
static const SQRegFunction iolib_funcs[]={
|
||||
_DECL_GLOBALIO_FUNC(loadfile,-2,_SC(".sb")),
|
||||
_DECL_GLOBALIO_FUNC(dofile,-2,_SC(".sb")),
|
||||
_DECL_GLOBALIO_FUNC(writeclosuretofile,3,_SC(".sc")),
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
SQRESULT sqstd_register_iolib(HSQUIRRELVM v)
|
||||
{
|
||||
SQInteger top = sq_gettop(v);
|
||||
//create delegate
|
||||
declare_stream(v,_SC("file"),(SQUserPointer)SQSTD_FILE_TYPE_TAG,_SC("std_file"),_file_methods,iolib_funcs);
|
||||
sq_pushstring(v,_SC("stdout"),-1);
|
||||
sqstd_createfile(v,stdout,SQFalse);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
sq_pushstring(v,_SC("stdin"),-1);
|
||||
sqstd_createfile(v,stdin,SQFalse);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
sq_pushstring(v,_SC("stderr"),-1);
|
||||
sqstd_createfile(v,stderr,SQFalse);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
sq_settop(v,top);
|
||||
return SQ_OK;
|
||||
}
|
107
external/Squirrel/Lib/sqstdmath.cpp
vendored
Normal file
107
external/Squirrel/Lib/sqstdmath.cpp
vendored
Normal file
@ -0,0 +1,107 @@
|
||||
/* see copyright notice in squirrel.h */
|
||||
#include <squirrel.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <sqstdmath.h>
|
||||
|
||||
#define SINGLE_ARG_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
|
||||
SQFloat f; \
|
||||
sq_getfloat(v,2,&f); \
|
||||
sq_pushfloat(v,(SQFloat)_funcname(f)); \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
#define TWO_ARGS_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
|
||||
SQFloat p1,p2; \
|
||||
sq_getfloat(v,2,&p1); \
|
||||
sq_getfloat(v,3,&p2); \
|
||||
sq_pushfloat(v,(SQFloat)_funcname(p1,p2)); \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
static SQInteger math_srand(HSQUIRRELVM v)
|
||||
{
|
||||
SQInteger i;
|
||||
if(SQ_FAILED(sq_getinteger(v,2,&i)))
|
||||
return sq_throwerror(v,_SC("invalid param"));
|
||||
srand((unsigned int)i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SQInteger math_rand(HSQUIRRELVM v)
|
||||
{
|
||||
sq_pushinteger(v,rand());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger math_abs(HSQUIRRELVM v)
|
||||
{
|
||||
SQInteger n;
|
||||
sq_getinteger(v,2,&n);
|
||||
sq_pushinteger(v,(SQInteger)abs((int)n));
|
||||
return 1;
|
||||
}
|
||||
|
||||
SINGLE_ARG_FUNC(sqrt)
|
||||
SINGLE_ARG_FUNC(fabs)
|
||||
SINGLE_ARG_FUNC(sin)
|
||||
SINGLE_ARG_FUNC(cos)
|
||||
SINGLE_ARG_FUNC(asin)
|
||||
SINGLE_ARG_FUNC(acos)
|
||||
SINGLE_ARG_FUNC(log)
|
||||
SINGLE_ARG_FUNC(log10)
|
||||
SINGLE_ARG_FUNC(tan)
|
||||
SINGLE_ARG_FUNC(atan)
|
||||
TWO_ARGS_FUNC(atan2)
|
||||
TWO_ARGS_FUNC(pow)
|
||||
SINGLE_ARG_FUNC(floor)
|
||||
SINGLE_ARG_FUNC(ceil)
|
||||
SINGLE_ARG_FUNC(exp)
|
||||
|
||||
#define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),math_##name,nparams,tycheck}
|
||||
static const SQRegFunction mathlib_funcs[] = {
|
||||
_DECL_FUNC(sqrt,2,_SC(".n")),
|
||||
_DECL_FUNC(sin,2,_SC(".n")),
|
||||
_DECL_FUNC(cos,2,_SC(".n")),
|
||||
_DECL_FUNC(asin,2,_SC(".n")),
|
||||
_DECL_FUNC(acos,2,_SC(".n")),
|
||||
_DECL_FUNC(log,2,_SC(".n")),
|
||||
_DECL_FUNC(log10,2,_SC(".n")),
|
||||
_DECL_FUNC(tan,2,_SC(".n")),
|
||||
_DECL_FUNC(atan,2,_SC(".n")),
|
||||
_DECL_FUNC(atan2,3,_SC(".nn")),
|
||||
_DECL_FUNC(pow,3,_SC(".nn")),
|
||||
_DECL_FUNC(floor,2,_SC(".n")),
|
||||
_DECL_FUNC(ceil,2,_SC(".n")),
|
||||
_DECL_FUNC(exp,2,_SC(".n")),
|
||||
_DECL_FUNC(srand,2,_SC(".n")),
|
||||
_DECL_FUNC(rand,1,NULL),
|
||||
_DECL_FUNC(fabs,2,_SC(".n")),
|
||||
_DECL_FUNC(abs,2,_SC(".n")),
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
#undef _DECL_FUNC
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI (3.14159265358979323846)
|
||||
#endif
|
||||
|
||||
SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
|
||||
{
|
||||
SQInteger i=0;
|
||||
while(mathlib_funcs[i].name!=0) {
|
||||
sq_pushstring(v,mathlib_funcs[i].name,-1);
|
||||
sq_newclosure(v,mathlib_funcs[i].f,0);
|
||||
sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
|
||||
sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
i++;
|
||||
}
|
||||
sq_pushstring(v,_SC("RAND_MAX"),-1);
|
||||
sq_pushinteger(v,RAND_MAX);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
sq_pushstring(v,_SC("PI"),-1);
|
||||
sq_pushfloat(v,(SQFloat)M_PI);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
return SQ_OK;
|
||||
}
|
663
external/Squirrel/Lib/sqstdrex.cpp
vendored
Normal file
663
external/Squirrel/Lib/sqstdrex.cpp
vendored
Normal file
@ -0,0 +1,663 @@
|
||||
/* see copyright notice in squirrel.h */
|
||||
#include <squirrel.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <setjmp.h>
|
||||
#include <sqstdstring.h>
|
||||
|
||||
#ifdef _DEBUG
|
||||
#include <stdio.h>
|
||||
|
||||
static const SQChar *g_nnames[] =
|
||||
{
|
||||
_SC("NONE"),_SC("OP_GREEDY"), _SC("OP_OR"),
|
||||
_SC("OP_EXPR"),_SC("OP_NOCAPEXPR"),_SC("OP_DOT"), _SC("OP_CLASS"),
|
||||
_SC("OP_CCLASS"),_SC("OP_NCLASS"),_SC("OP_RANGE"),_SC("OP_CHAR"),
|
||||
_SC("OP_EOL"),_SC("OP_BOL"),_SC("OP_WB"),_SC("OP_MB")
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#define OP_GREEDY (MAX_CHAR+1) // * + ? {n}
|
||||
#define OP_OR (MAX_CHAR+2)
|
||||
#define OP_EXPR (MAX_CHAR+3) //parentesis ()
|
||||
#define OP_NOCAPEXPR (MAX_CHAR+4) //parentesis (?:)
|
||||
#define OP_DOT (MAX_CHAR+5)
|
||||
#define OP_CLASS (MAX_CHAR+6)
|
||||
#define OP_CCLASS (MAX_CHAR+7)
|
||||
#define OP_NCLASS (MAX_CHAR+8) //negates class the [^
|
||||
#define OP_RANGE (MAX_CHAR+9)
|
||||
#define OP_CHAR (MAX_CHAR+10)
|
||||
#define OP_EOL (MAX_CHAR+11)
|
||||
#define OP_BOL (MAX_CHAR+12)
|
||||
#define OP_WB (MAX_CHAR+13)
|
||||
#define OP_MB (MAX_CHAR+14) //match balanced
|
||||
|
||||
#define SQREX_SYMBOL_ANY_CHAR ('.')
|
||||
#define SQREX_SYMBOL_GREEDY_ONE_OR_MORE ('+')
|
||||
#define SQREX_SYMBOL_GREEDY_ZERO_OR_MORE ('*')
|
||||
#define SQREX_SYMBOL_GREEDY_ZERO_OR_ONE ('?')
|
||||
#define SQREX_SYMBOL_BRANCH ('|')
|
||||
#define SQREX_SYMBOL_END_OF_STRING ('$')
|
||||
#define SQREX_SYMBOL_BEGINNING_OF_STRING ('^')
|
||||
#define SQREX_SYMBOL_ESCAPE_CHAR ('\\')
|
||||
|
||||
|
||||
typedef int SQRexNodeType;
|
||||
|
||||
typedef struct tagSQRexNode{
|
||||
SQRexNodeType type;
|
||||
SQInteger left;
|
||||
SQInteger right;
|
||||
SQInteger next;
|
||||
}SQRexNode;
|
||||
|
||||
struct SQRex{
|
||||
const SQChar *_eol;
|
||||
const SQChar *_bol;
|
||||
const SQChar *_p;
|
||||
SQInteger _first;
|
||||
SQInteger _op;
|
||||
SQRexNode *_nodes;
|
||||
SQInteger _nallocated;
|
||||
SQInteger _nsize;
|
||||
SQInteger _nsubexpr;
|
||||
SQRexMatch *_matches;
|
||||
SQInteger _currsubexp;
|
||||
void *_jmpbuf;
|
||||
const SQChar **_error;
|
||||
};
|
||||
|
||||
static SQInteger sqstd_rex_list(SQRex *exp);
|
||||
|
||||
static SQInteger sqstd_rex_newnode(SQRex *exp, SQRexNodeType type)
|
||||
{
|
||||
SQRexNode n;
|
||||
n.type = type;
|
||||
n.next = n.right = n.left = -1;
|
||||
if(type == OP_EXPR)
|
||||
n.right = exp->_nsubexpr++;
|
||||
if(exp->_nallocated < (exp->_nsize + 1)) {
|
||||
SQInteger oldsize = exp->_nallocated;
|
||||
exp->_nallocated *= 2;
|
||||
exp->_nodes = (SQRexNode *)sq_realloc(exp->_nodes, oldsize * sizeof(SQRexNode) ,exp->_nallocated * sizeof(SQRexNode));
|
||||
}
|
||||
exp->_nodes[exp->_nsize++] = n;
|
||||
SQInteger newid = exp->_nsize - 1;
|
||||
return (SQInteger)newid;
|
||||
}
|
||||
|
||||
static void sqstd_rex_error(SQRex *exp,const SQChar *error)
|
||||
{
|
||||
if(exp->_error) *exp->_error = error;
|
||||
longjmp(*((jmp_buf*)exp->_jmpbuf),-1);
|
||||
}
|
||||
|
||||
static void sqstd_rex_expect(SQRex *exp, SQInteger n){
|
||||
if((*exp->_p) != n)
|
||||
sqstd_rex_error(exp, _SC("expected paren"));
|
||||
exp->_p++;
|
||||
}
|
||||
|
||||
static SQChar sqstd_rex_escapechar(SQRex *exp)
|
||||
{
|
||||
if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR){
|
||||
exp->_p++;
|
||||
switch(*exp->_p) {
|
||||
case 'v': exp->_p++; return '\v';
|
||||
case 'n': exp->_p++; return '\n';
|
||||
case 't': exp->_p++; return '\t';
|
||||
case 'r': exp->_p++; return '\r';
|
||||
case 'f': exp->_p++; return '\f';
|
||||
default: return (*exp->_p++);
|
||||
}
|
||||
} else if(!scisprint(*exp->_p)) sqstd_rex_error(exp,_SC("letter expected"));
|
||||
return (*exp->_p++);
|
||||
}
|
||||
|
||||
static SQInteger sqstd_rex_charclass(SQRex *exp,SQInteger classid)
|
||||
{
|
||||
SQInteger n = sqstd_rex_newnode(exp,OP_CCLASS);
|
||||
exp->_nodes[n].left = classid;
|
||||
return n;
|
||||
}
|
||||
|
||||
static SQInteger sqstd_rex_charnode(SQRex *exp,SQBool isclass)
|
||||
{
|
||||
SQChar t;
|
||||
if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR) {
|
||||
exp->_p++;
|
||||
switch(*exp->_p) {
|
||||
case 'n': exp->_p++; return sqstd_rex_newnode(exp,'\n');
|
||||
case 't': exp->_p++; return sqstd_rex_newnode(exp,'\t');
|
||||
case 'r': exp->_p++; return sqstd_rex_newnode(exp,'\r');
|
||||
case 'f': exp->_p++; return sqstd_rex_newnode(exp,'\f');
|
||||
case 'v': exp->_p++; return sqstd_rex_newnode(exp,'\v');
|
||||
case 'a': case 'A': case 'w': case 'W': case 's': case 'S':
|
||||
case 'd': case 'D': case 'x': case 'X': case 'c': case 'C':
|
||||
case 'p': case 'P': case 'l': case 'u':
|
||||
{
|
||||
t = *exp->_p; exp->_p++;
|
||||
return sqstd_rex_charclass(exp,t);
|
||||
}
|
||||
case 'm':
|
||||
{
|
||||
SQChar cb, ce; //cb = character begin match ce = character end match
|
||||
cb = *++exp->_p; //skip 'm'
|
||||
ce = *++exp->_p;
|
||||
exp->_p++; //points to the next char to be parsed
|
||||
if ((!cb) || (!ce)) sqstd_rex_error(exp,_SC("balanced chars expected"));
|
||||
if ( cb == ce ) sqstd_rex_error(exp,_SC("open/close char can't be the same"));
|
||||
SQInteger node = sqstd_rex_newnode(exp,OP_MB);
|
||||
exp->_nodes[node].left = cb;
|
||||
exp->_nodes[node].right = ce;
|
||||
return node;
|
||||
}
|
||||
case 'b':
|
||||
case 'B':
|
||||
if(!isclass) {
|
||||
SQInteger node = sqstd_rex_newnode(exp,OP_WB);
|
||||
exp->_nodes[node].left = *exp->_p;
|
||||
exp->_p++;
|
||||
return node;
|
||||
} //else default
|
||||
default:
|
||||
t = *exp->_p; exp->_p++;
|
||||
return sqstd_rex_newnode(exp,t);
|
||||
}
|
||||
}
|
||||
else if(!scisprint(*exp->_p)) {
|
||||
|
||||
sqstd_rex_error(exp,_SC("letter expected"));
|
||||
}
|
||||
t = *exp->_p; exp->_p++;
|
||||
return sqstd_rex_newnode(exp,t);
|
||||
}
|
||||
static SQInteger sqstd_rex_class(SQRex *exp)
|
||||
{
|
||||
SQInteger ret = -1;
|
||||
SQInteger first = -1,chain;
|
||||
if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING){
|
||||
ret = sqstd_rex_newnode(exp,OP_NCLASS);
|
||||
exp->_p++;
|
||||
}else ret = sqstd_rex_newnode(exp,OP_CLASS);
|
||||
|
||||
if(*exp->_p == ']') sqstd_rex_error(exp,_SC("empty class"));
|
||||
chain = ret;
|
||||
while(*exp->_p != ']' && exp->_p != exp->_eol) {
|
||||
if(*exp->_p == '-' && first != -1){
|
||||
SQInteger r;
|
||||
if(*exp->_p++ == ']') sqstd_rex_error(exp,_SC("unfinished range"));
|
||||
r = sqstd_rex_newnode(exp,OP_RANGE);
|
||||
if(exp->_nodes[first].type>*exp->_p) sqstd_rex_error(exp,_SC("invalid range"));
|
||||
if(exp->_nodes[first].type == OP_CCLASS) sqstd_rex_error(exp,_SC("cannot use character classes in ranges"));
|
||||
exp->_nodes[r].left = exp->_nodes[first].type;
|
||||
SQInteger t = sqstd_rex_escapechar(exp);
|
||||
exp->_nodes[r].right = t;
|
||||
exp->_nodes[chain].next = r;
|
||||
chain = r;
|
||||
first = -1;
|
||||
}
|
||||
else{
|
||||
if(first!=-1){
|
||||
SQInteger c = first;
|
||||
exp->_nodes[chain].next = c;
|
||||
chain = c;
|
||||
first = sqstd_rex_charnode(exp,SQTrue);
|
||||
}
|
||||
else{
|
||||
first = sqstd_rex_charnode(exp,SQTrue);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(first!=-1){
|
||||
SQInteger c = first;
|
||||
exp->_nodes[chain].next = c;
|
||||
}
|
||||
/* hack? */
|
||||
exp->_nodes[ret].left = exp->_nodes[ret].next;
|
||||
exp->_nodes[ret].next = -1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static SQInteger sqstd_rex_parsenumber(SQRex *exp)
|
||||
{
|
||||
SQInteger ret = *exp->_p-'0';
|
||||
SQInteger positions = 10;
|
||||
exp->_p++;
|
||||
while(isdigit(*exp->_p)) {
|
||||
ret = ret*10+(*exp->_p++-'0');
|
||||
if(positions==1000000000) sqstd_rex_error(exp,_SC("overflow in numeric constant"));
|
||||
positions *= 10;
|
||||
};
|
||||
return ret;
|
||||
}
|
||||
|
||||
static SQInteger sqstd_rex_element(SQRex *exp)
|
||||
{
|
||||
SQInteger ret = -1;
|
||||
switch(*exp->_p)
|
||||
{
|
||||
case '(': {
|
||||
SQInteger expr;
|
||||
exp->_p++;
|
||||
|
||||
|
||||
if(*exp->_p =='?') {
|
||||
exp->_p++;
|
||||
sqstd_rex_expect(exp,':');
|
||||
expr = sqstd_rex_newnode(exp,OP_NOCAPEXPR);
|
||||
}
|
||||
else
|
||||
expr = sqstd_rex_newnode(exp,OP_EXPR);
|
||||
SQInteger newn = sqstd_rex_list(exp);
|
||||
exp->_nodes[expr].left = newn;
|
||||
ret = expr;
|
||||
sqstd_rex_expect(exp,')');
|
||||
}
|
||||
break;
|
||||
case '[':
|
||||
exp->_p++;
|
||||
ret = sqstd_rex_class(exp);
|
||||
sqstd_rex_expect(exp,']');
|
||||
break;
|
||||
case SQREX_SYMBOL_END_OF_STRING: exp->_p++; ret = sqstd_rex_newnode(exp,OP_EOL);break;
|
||||
case SQREX_SYMBOL_ANY_CHAR: exp->_p++; ret = sqstd_rex_newnode(exp,OP_DOT);break;
|
||||
default:
|
||||
ret = sqstd_rex_charnode(exp,SQFalse);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
SQBool isgreedy = SQFalse;
|
||||
unsigned short p0 = 0, p1 = 0;
|
||||
switch(*exp->_p){
|
||||
case SQREX_SYMBOL_GREEDY_ZERO_OR_MORE: p0 = 0; p1 = 0xFFFF; exp->_p++; isgreedy = SQTrue; break;
|
||||
case SQREX_SYMBOL_GREEDY_ONE_OR_MORE: p0 = 1; p1 = 0xFFFF; exp->_p++; isgreedy = SQTrue; break;
|
||||
case SQREX_SYMBOL_GREEDY_ZERO_OR_ONE: p0 = 0; p1 = 1; exp->_p++; isgreedy = SQTrue; break;
|
||||
case '{':
|
||||
exp->_p++;
|
||||
if(!isdigit(*exp->_p)) sqstd_rex_error(exp,_SC("number expected"));
|
||||
p0 = (unsigned short)sqstd_rex_parsenumber(exp);
|
||||
/*******************************/
|
||||
switch(*exp->_p) {
|
||||
case '}':
|
||||
p1 = p0; exp->_p++;
|
||||
break;
|
||||
case ',':
|
||||
exp->_p++;
|
||||
p1 = 0xFFFF;
|
||||
if(isdigit(*exp->_p)){
|
||||
p1 = (unsigned short)sqstd_rex_parsenumber(exp);
|
||||
}
|
||||
sqstd_rex_expect(exp,'}');
|
||||
break;
|
||||
default:
|
||||
sqstd_rex_error(exp,_SC(", or } expected"));
|
||||
}
|
||||
/*******************************/
|
||||
isgreedy = SQTrue;
|
||||
break;
|
||||
|
||||
}
|
||||
if(isgreedy) {
|
||||
SQInteger nnode = sqstd_rex_newnode(exp,OP_GREEDY);
|
||||
exp->_nodes[nnode].left = ret;
|
||||
exp->_nodes[nnode].right = ((p0)<<16)|p1;
|
||||
ret = nnode;
|
||||
}
|
||||
|
||||
if((*exp->_p != SQREX_SYMBOL_BRANCH) && (*exp->_p != ')') && (*exp->_p != SQREX_SYMBOL_GREEDY_ZERO_OR_MORE) && (*exp->_p != SQREX_SYMBOL_GREEDY_ONE_OR_MORE) && (*exp->_p != '\0')) {
|
||||
SQInteger nnode = sqstd_rex_element(exp);
|
||||
exp->_nodes[ret].next = nnode;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static SQInteger sqstd_rex_list(SQRex *exp)
|
||||
{
|
||||
SQInteger ret=-1,e;
|
||||
if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING) {
|
||||
exp->_p++;
|
||||
ret = sqstd_rex_newnode(exp,OP_BOL);
|
||||
}
|
||||
e = sqstd_rex_element(exp);
|
||||
if(ret != -1) {
|
||||
exp->_nodes[ret].next = e;
|
||||
}
|
||||
else ret = e;
|
||||
|
||||
if(*exp->_p == SQREX_SYMBOL_BRANCH) {
|
||||
SQInteger temp,tright;
|
||||
exp->_p++;
|
||||
temp = sqstd_rex_newnode(exp,OP_OR);
|
||||
exp->_nodes[temp].left = ret;
|
||||
tright = sqstd_rex_list(exp);
|
||||
exp->_nodes[temp].right = tright;
|
||||
ret = temp;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static SQBool sqstd_rex_matchcclass(SQInteger cclass,SQChar c)
|
||||
{
|
||||
switch(cclass) {
|
||||
case 'a': return isalpha(c)?SQTrue:SQFalse;
|
||||
case 'A': return !isalpha(c)?SQTrue:SQFalse;
|
||||
case 'w': return (isalnum(c) || c == '_')?SQTrue:SQFalse;
|
||||
case 'W': return (!isalnum(c) && c != '_')?SQTrue:SQFalse;
|
||||
case 's': return isspace(c)?SQTrue:SQFalse;
|
||||
case 'S': return !isspace(c)?SQTrue:SQFalse;
|
||||
case 'd': return isdigit(c)?SQTrue:SQFalse;
|
||||
case 'D': return !isdigit(c)?SQTrue:SQFalse;
|
||||
case 'x': return isxdigit(c)?SQTrue:SQFalse;
|
||||
case 'X': return !isxdigit(c)?SQTrue:SQFalse;
|
||||
case 'c': return iscntrl(c)?SQTrue:SQFalse;
|
||||
case 'C': return !iscntrl(c)?SQTrue:SQFalse;
|
||||
case 'p': return ispunct(c)?SQTrue:SQFalse;
|
||||
case 'P': return !ispunct(c)?SQTrue:SQFalse;
|
||||
case 'l': return islower(c)?SQTrue:SQFalse;
|
||||
case 'u': return isupper(c)?SQTrue:SQFalse;
|
||||
}
|
||||
return SQFalse; /*cannot happen*/
|
||||
}
|
||||
|
||||
static SQBool sqstd_rex_matchclass(SQRex* exp,SQRexNode *node,SQChar c)
|
||||
{
|
||||
do {
|
||||
switch(node->type) {
|
||||
case OP_RANGE:
|
||||
if(c >= node->left && c <= node->right) return SQTrue;
|
||||
break;
|
||||
case OP_CCLASS:
|
||||
if(sqstd_rex_matchcclass(node->left,c)) return SQTrue;
|
||||
break;
|
||||
default:
|
||||
if(c == node->type)return SQTrue;
|
||||
}
|
||||
} while((node->next != -1) && (node = &exp->_nodes[node->next]));
|
||||
return SQFalse;
|
||||
}
|
||||
|
||||
static const SQChar *sqstd_rex_matchnode(SQRex* exp,SQRexNode *node,const SQChar *str,SQRexNode *next)
|
||||
{
|
||||
|
||||
SQRexNodeType type = node->type;
|
||||
switch(type) {
|
||||
case OP_GREEDY: {
|
||||
//SQRexNode *greedystop = (node->next != -1) ? &exp->_nodes[node->next] : NULL;
|
||||
SQRexNode *greedystop = NULL;
|
||||
SQInteger p0 = (node->right >> 16)&0x0000FFFF, p1 = node->right&0x0000FFFF, nmaches = 0;
|
||||
const SQChar *s=str, *good = str;
|
||||
|
||||
if(node->next != -1) {
|
||||
greedystop = &exp->_nodes[node->next];
|
||||
}
|
||||
else {
|
||||
greedystop = next;
|
||||
}
|
||||
|
||||
while((nmaches == 0xFFFF || nmaches < p1)) {
|
||||
|
||||
const SQChar *stop;
|
||||
if(!(s = sqstd_rex_matchnode(exp,&exp->_nodes[node->left],s,greedystop)))
|
||||
break;
|
||||
nmaches++;
|
||||
good=s;
|
||||
if(greedystop) {
|
||||
//checks that 0 matches satisfy the expression(if so skips)
|
||||
//if not would always stop(for instance if is a '?')
|
||||
if(greedystop->type != OP_GREEDY ||
|
||||
(greedystop->type == OP_GREEDY && ((greedystop->right >> 16)&0x0000FFFF) != 0))
|
||||
{
|
||||
SQRexNode *gnext = NULL;
|
||||
if(greedystop->next != -1) {
|
||||
gnext = &exp->_nodes[greedystop->next];
|
||||
}else if(next && next->next != -1){
|
||||
gnext = &exp->_nodes[next->next];
|
||||
}
|
||||
stop = sqstd_rex_matchnode(exp,greedystop,s,gnext);
|
||||
if(stop) {
|
||||
//if satisfied stop it
|
||||
if(p0 == p1 && p0 == nmaches) break;
|
||||
else if(nmaches >= p0 && p1 == 0xFFFF) break;
|
||||
else if(nmaches >= p0 && nmaches <= p1) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(s >= exp->_eol)
|
||||
break;
|
||||
}
|
||||
if(p0 == p1 && p0 == nmaches) return good;
|
||||
else if(nmaches >= p0 && p1 == 0xFFFF) return good;
|
||||
else if(nmaches >= p0 && nmaches <= p1) return good;
|
||||
return NULL;
|
||||
}
|
||||
case OP_OR: {
|
||||
const SQChar *asd = str;
|
||||
SQRexNode *temp=&exp->_nodes[node->left];
|
||||
while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
|
||||
if(temp->next != -1)
|
||||
temp = &exp->_nodes[temp->next];
|
||||
else
|
||||
return asd;
|
||||
}
|
||||
asd = str;
|
||||
temp = &exp->_nodes[node->right];
|
||||
while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
|
||||
if(temp->next != -1)
|
||||
temp = &exp->_nodes[temp->next];
|
||||
else
|
||||
return asd;
|
||||
}
|
||||
return NULL;
|
||||
break;
|
||||
}
|
||||
case OP_EXPR:
|
||||
case OP_NOCAPEXPR:{
|
||||
SQRexNode *n = &exp->_nodes[node->left];
|
||||
const SQChar *cur = str;
|
||||
SQInteger capture = -1;
|
||||
if(node->type != OP_NOCAPEXPR && node->right == exp->_currsubexp) {
|
||||
capture = exp->_currsubexp;
|
||||
exp->_matches[capture].begin = cur;
|
||||
exp->_currsubexp++;
|
||||
}
|
||||
SQInteger tempcap = exp->_currsubexp;
|
||||
do {
|
||||
SQRexNode *subnext = NULL;
|
||||
if(n->next != -1) {
|
||||
subnext = &exp->_nodes[n->next];
|
||||
}else {
|
||||
subnext = next;
|
||||
}
|
||||
if(!(cur = sqstd_rex_matchnode(exp,n,cur,subnext))) {
|
||||
if(capture != -1){
|
||||
exp->_matches[capture].begin = 0;
|
||||
exp->_matches[capture].len = 0;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
} while((n->next != -1) && (n = &exp->_nodes[n->next]));
|
||||
|
||||
exp->_currsubexp = tempcap;
|
||||
if(capture != -1)
|
||||
exp->_matches[capture].len = cur - exp->_matches[capture].begin;
|
||||
return cur;
|
||||
}
|
||||
case OP_WB:
|
||||
if((str == exp->_bol && !isspace(*str))
|
||||
|| (str == exp->_eol && !isspace(*(str-1)))
|
||||
|| (!isspace(*str) && isspace(*(str+1)))
|
||||
|| (isspace(*str) && !isspace(*(str+1))) ) {
|
||||
return (node->left == 'b')?str:NULL;
|
||||
}
|
||||
return (node->left == 'b')?NULL:str;
|
||||
case OP_BOL:
|
||||
if(str == exp->_bol) return str;
|
||||
return NULL;
|
||||
case OP_EOL:
|
||||
if(str == exp->_eol) return str;
|
||||
return NULL;
|
||||
case OP_DOT:{
|
||||
if (str == exp->_eol) return NULL;
|
||||
str++;
|
||||
}
|
||||
return str;
|
||||
case OP_NCLASS:
|
||||
case OP_CLASS:
|
||||
if (str == exp->_eol) return NULL;
|
||||
if(sqstd_rex_matchclass(exp,&exp->_nodes[node->left],*str)?(type == OP_CLASS?SQTrue:SQFalse):(type == OP_NCLASS?SQTrue:SQFalse)) {
|
||||
str++;
|
||||
return str;
|
||||
}
|
||||
return NULL;
|
||||
case OP_CCLASS:
|
||||
if (str == exp->_eol) return NULL;
|
||||
if(sqstd_rex_matchcclass(node->left,*str)) {
|
||||
str++;
|
||||
return str;
|
||||
}
|
||||
return NULL;
|
||||
case OP_MB:
|
||||
{
|
||||
SQInteger cb = node->left; //char that opens a balanced expression
|
||||
if(*str != cb) return NULL; // string doesnt start with open char
|
||||
SQInteger ce = node->right; //char that closes a balanced expression
|
||||
SQInteger cont = 1;
|
||||
const SQChar *streol = exp->_eol;
|
||||
while (++str < streol) {
|
||||
if (*str == ce) {
|
||||
if (--cont == 0) {
|
||||
return ++str;
|
||||
}
|
||||
}
|
||||
else if (*str == cb) cont++;
|
||||
}
|
||||
}
|
||||
return NULL; // string ends out of balance
|
||||
default: /* char */
|
||||
if (str == exp->_eol) return NULL;
|
||||
if(*str != node->type) return NULL;
|
||||
str++;
|
||||
return str;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* public api */
|
||||
SQRex *sqstd_rex_compile(const SQChar *pattern,const SQChar **error)
|
||||
{
|
||||
SQRex * volatile exp = (SQRex *)sq_malloc(sizeof(SQRex)); // "volatile" is needed for setjmp()
|
||||
exp->_eol = exp->_bol = NULL;
|
||||
exp->_p = pattern;
|
||||
exp->_nallocated = (SQInteger)scstrlen(pattern) * sizeof(SQChar);
|
||||
exp->_nodes = (SQRexNode *)sq_malloc(exp->_nallocated * sizeof(SQRexNode));
|
||||
exp->_nsize = 0;
|
||||
exp->_matches = 0;
|
||||
exp->_nsubexpr = 0;
|
||||
exp->_first = sqstd_rex_newnode(exp,OP_EXPR);
|
||||
exp->_error = error;
|
||||
exp->_jmpbuf = sq_malloc(sizeof(jmp_buf));
|
||||
if(setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) {
|
||||
SQInteger res = sqstd_rex_list(exp);
|
||||
exp->_nodes[exp->_first].left = res;
|
||||
if(*exp->_p!='\0')
|
||||
sqstd_rex_error(exp,_SC("unexpected character"));
|
||||
#ifdef _DEBUG
|
||||
{
|
||||
SQInteger nsize,i;
|
||||
SQRexNode *t;
|
||||
nsize = exp->_nsize;
|
||||
t = &exp->_nodes[0];
|
||||
scprintf(_SC("\n"));
|
||||
for(i = 0;i < nsize; i++) {
|
||||
if(exp->_nodes[i].type>MAX_CHAR)
|
||||
scprintf(_SC("[%02d] %10s "),i,g_nnames[exp->_nodes[i].type-MAX_CHAR]);
|
||||
else
|
||||
scprintf(_SC("[%02d] %10c "),i,exp->_nodes[i].type);
|
||||
scprintf(_SC("left %02d right %02d next %02d\n"), (SQInt32)exp->_nodes[i].left, (SQInt32)exp->_nodes[i].right, (SQInt32)exp->_nodes[i].next);
|
||||
}
|
||||
scprintf(_SC("\n"));
|
||||
}
|
||||
#endif
|
||||
exp->_matches = (SQRexMatch *) sq_malloc(exp->_nsubexpr * sizeof(SQRexMatch));
|
||||
memset(exp->_matches,0,exp->_nsubexpr * sizeof(SQRexMatch));
|
||||
}
|
||||
else{
|
||||
sqstd_rex_free(exp);
|
||||
return NULL;
|
||||
}
|
||||
return exp;
|
||||
}
|
||||
|
||||
void sqstd_rex_free(SQRex *exp)
|
||||
{
|
||||
if(exp) {
|
||||
if(exp->_nodes) sq_free(exp->_nodes,exp->_nallocated * sizeof(SQRexNode));
|
||||
if(exp->_jmpbuf) sq_free(exp->_jmpbuf,sizeof(jmp_buf));
|
||||
if(exp->_matches) sq_free(exp->_matches,exp->_nsubexpr * sizeof(SQRexMatch));
|
||||
sq_free(exp,sizeof(SQRex));
|
||||
}
|
||||
}
|
||||
|
||||
SQBool sqstd_rex_match(SQRex* exp,const SQChar* text)
|
||||
{
|
||||
const SQChar* res = NULL;
|
||||
exp->_bol = text;
|
||||
exp->_eol = text + scstrlen(text);
|
||||
exp->_currsubexp = 0;
|
||||
res = sqstd_rex_matchnode(exp,exp->_nodes,text,NULL);
|
||||
if(res == NULL || res != exp->_eol)
|
||||
return SQFalse;
|
||||
return SQTrue;
|
||||
}
|
||||
|
||||
SQBool sqstd_rex_searchrange(SQRex* exp,const SQChar* text_begin,const SQChar* text_end,const SQChar** out_begin, const SQChar** out_end)
|
||||
{
|
||||
const SQChar *cur = NULL;
|
||||
SQInteger node = exp->_first;
|
||||
if(text_begin >= text_end) return SQFalse;
|
||||
exp->_bol = text_begin;
|
||||
exp->_eol = text_end;
|
||||
do {
|
||||
cur = text_begin;
|
||||
while(node != -1) {
|
||||
exp->_currsubexp = 0;
|
||||
cur = sqstd_rex_matchnode(exp,&exp->_nodes[node],cur,NULL);
|
||||
if(!cur)
|
||||
break;
|
||||
node = exp->_nodes[node].next;
|
||||
}
|
||||
text_begin++;
|
||||
} while(cur == NULL && text_begin != text_end);
|
||||
|
||||
if(cur == NULL)
|
||||
return SQFalse;
|
||||
|
||||
--text_begin;
|
||||
|
||||
if(out_begin) *out_begin = text_begin;
|
||||
if(out_end) *out_end = cur;
|
||||
return SQTrue;
|
||||
}
|
||||
|
||||
SQBool sqstd_rex_search(SQRex* exp,const SQChar* text, const SQChar** out_begin, const SQChar** out_end)
|
||||
{
|
||||
return sqstd_rex_searchrange(exp,text,text + scstrlen(text),out_begin,out_end);
|
||||
}
|
||||
|
||||
SQInteger sqstd_rex_getsubexpcount(SQRex* exp)
|
||||
{
|
||||
return exp->_nsubexpr;
|
||||
}
|
||||
|
||||
SQBool sqstd_rex_getsubexp(SQRex* exp, SQInteger n, SQRexMatch *subexp)
|
||||
{
|
||||
if( n<0 || n >= exp->_nsubexpr) return SQFalse;
|
||||
*subexp = exp->_matches[n];
|
||||
return SQTrue;
|
||||
}
|
||||
|
336
external/Squirrel/Lib/sqstdstream.cpp
vendored
Normal file
336
external/Squirrel/Lib/sqstdstream.cpp
vendored
Normal file
@ -0,0 +1,336 @@
|
||||
/* see copyright notice in squirrel.h */
|
||||
#include <new>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <squirrel.h>
|
||||
#include <sqstdio.h>
|
||||
#include <sqstdblob.h>
|
||||
#include "sqstdstream.h"
|
||||
#include "sqstdblobimpl.h"
|
||||
|
||||
#define SETUP_STREAM(v) \
|
||||
SQStream *self = NULL; \
|
||||
if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_STREAM_TYPE_TAG))) \
|
||||
return sq_throwerror(v,_SC("invalid type tag")); \
|
||||
if(!self || !self->IsValid()) \
|
||||
return sq_throwerror(v,_SC("the stream is invalid"));
|
||||
|
||||
SQInteger _stream_readblob(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_STREAM(v);
|
||||
SQUserPointer data,blobp;
|
||||
SQInteger size,res;
|
||||
sq_getinteger(v,2,&size);
|
||||
if(size > self->Len()) {
|
||||
size = self->Len();
|
||||
}
|
||||
data = sq_getscratchpad(v,size);
|
||||
res = self->Read(data,size);
|
||||
if(res <= 0)
|
||||
return sq_throwerror(v,_SC("no data left to read"));
|
||||
blobp = sqstd_createblob(v,res);
|
||||
memcpy(blobp,data,res);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define SAFE_READN(ptr,len) { \
|
||||
if(self->Read(ptr,len) != len) return sq_throwerror(v,_SC("io error")); \
|
||||
}
|
||||
SQInteger _stream_readn(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_STREAM(v);
|
||||
SQInteger format;
|
||||
sq_getinteger(v, 2, &format);
|
||||
switch(format) {
|
||||
case 'l': {
|
||||
SQInteger i;
|
||||
SAFE_READN(&i, sizeof(i));
|
||||
sq_pushinteger(v, i);
|
||||
}
|
||||
break;
|
||||
case 'i': {
|
||||
SQInt32 i;
|
||||
SAFE_READN(&i, sizeof(i));
|
||||
sq_pushinteger(v, i);
|
||||
}
|
||||
break;
|
||||
case 's': {
|
||||
short s;
|
||||
SAFE_READN(&s, sizeof(short));
|
||||
sq_pushinteger(v, s);
|
||||
}
|
||||
break;
|
||||
case 'w': {
|
||||
unsigned short w;
|
||||
SAFE_READN(&w, sizeof(unsigned short));
|
||||
sq_pushinteger(v, w);
|
||||
}
|
||||
break;
|
||||
case 'c': {
|
||||
char c;
|
||||
SAFE_READN(&c, sizeof(char));
|
||||
sq_pushinteger(v, c);
|
||||
}
|
||||
break;
|
||||
case 'b': {
|
||||
unsigned char c;
|
||||
SAFE_READN(&c, sizeof(unsigned char));
|
||||
sq_pushinteger(v, c);
|
||||
}
|
||||
break;
|
||||
case 'f': {
|
||||
float f;
|
||||
SAFE_READN(&f, sizeof(float));
|
||||
sq_pushfloat(v, f);
|
||||
}
|
||||
break;
|
||||
case 'd': {
|
||||
double d;
|
||||
SAFE_READN(&d, sizeof(double));
|
||||
sq_pushfloat(v, (SQFloat)d);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return sq_throwerror(v, _SC("invalid format"));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
SQInteger _stream_writeblob(HSQUIRRELVM v)
|
||||
{
|
||||
SQUserPointer data;
|
||||
SQInteger size;
|
||||
SETUP_STREAM(v);
|
||||
if(SQ_FAILED(sqstd_getblob(v,2,&data)))
|
||||
return sq_throwerror(v,_SC("invalid parameter"));
|
||||
size = sqstd_getblobsize(v,2);
|
||||
if(self->Write(data,size) != size)
|
||||
return sq_throwerror(v,_SC("io error"));
|
||||
sq_pushinteger(v,size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
SQInteger _stream_writen(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_STREAM(v);
|
||||
SQInteger format, ti;
|
||||
SQFloat tf;
|
||||
sq_getinteger(v, 3, &format);
|
||||
switch(format) {
|
||||
case 'l': {
|
||||
SQInteger i;
|
||||
sq_getinteger(v, 2, &ti);
|
||||
i = ti;
|
||||
self->Write(&i, sizeof(SQInteger));
|
||||
}
|
||||
break;
|
||||
case 'i': {
|
||||
SQInt32 i;
|
||||
sq_getinteger(v, 2, &ti);
|
||||
i = (SQInt32)ti;
|
||||
self->Write(&i, sizeof(SQInt32));
|
||||
}
|
||||
break;
|
||||
case 's': {
|
||||
short s;
|
||||
sq_getinteger(v, 2, &ti);
|
||||
s = (short)ti;
|
||||
self->Write(&s, sizeof(short));
|
||||
}
|
||||
break;
|
||||
case 'w': {
|
||||
unsigned short w;
|
||||
sq_getinteger(v, 2, &ti);
|
||||
w = (unsigned short)ti;
|
||||
self->Write(&w, sizeof(unsigned short));
|
||||
}
|
||||
break;
|
||||
case 'c': {
|
||||
char c;
|
||||
sq_getinteger(v, 2, &ti);
|
||||
c = (char)ti;
|
||||
self->Write(&c, sizeof(char));
|
||||
}
|
||||
break;
|
||||
case 'b': {
|
||||
unsigned char b;
|
||||
sq_getinteger(v, 2, &ti);
|
||||
b = (unsigned char)ti;
|
||||
self->Write(&b, sizeof(unsigned char));
|
||||
}
|
||||
break;
|
||||
case 'f': {
|
||||
float f;
|
||||
sq_getfloat(v, 2, &tf);
|
||||
f = (float)tf;
|
||||
self->Write(&f, sizeof(float));
|
||||
}
|
||||
break;
|
||||
case 'd': {
|
||||
double d;
|
||||
sq_getfloat(v, 2, &tf);
|
||||
d = tf;
|
||||
self->Write(&d, sizeof(double));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return sq_throwerror(v, _SC("invalid format"));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
SQInteger _stream_seek(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_STREAM(v);
|
||||
SQInteger offset, origin = SQ_SEEK_SET;
|
||||
sq_getinteger(v, 2, &offset);
|
||||
if(sq_gettop(v) > 2) {
|
||||
SQInteger t;
|
||||
sq_getinteger(v, 3, &t);
|
||||
switch(t) {
|
||||
case 'b': origin = SQ_SEEK_SET; break;
|
||||
case 'c': origin = SQ_SEEK_CUR; break;
|
||||
case 'e': origin = SQ_SEEK_END; break;
|
||||
default: return sq_throwerror(v,_SC("invalid origin"));
|
||||
}
|
||||
}
|
||||
sq_pushinteger(v, self->Seek(offset, origin));
|
||||
return 1;
|
||||
}
|
||||
|
||||
SQInteger _stream_tell(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_STREAM(v);
|
||||
sq_pushinteger(v, self->Tell());
|
||||
return 1;
|
||||
}
|
||||
|
||||
SQInteger _stream_len(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_STREAM(v);
|
||||
sq_pushinteger(v, self->Len());
|
||||
return 1;
|
||||
}
|
||||
|
||||
SQInteger _stream_flush(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_STREAM(v);
|
||||
if(!self->Flush())
|
||||
sq_pushinteger(v, 1);
|
||||
else
|
||||
sq_pushnull(v);
|
||||
return 1;
|
||||
}
|
||||
|
||||
SQInteger _stream_eos(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_STREAM(v);
|
||||
if(self->EOS())
|
||||
sq_pushinteger(v, 1);
|
||||
else
|
||||
sq_pushnull(v);
|
||||
return 1;
|
||||
}
|
||||
|
||||
SQInteger _stream__cloned(HSQUIRRELVM v)
|
||||
{
|
||||
return sq_throwerror(v,_SC("this object cannot be cloned"));
|
||||
}
|
||||
|
||||
static const SQRegFunction _stream_methods[] = {
|
||||
_DECL_STREAM_FUNC(readblob,2,_SC("xn")),
|
||||
_DECL_STREAM_FUNC(readn,2,_SC("xn")),
|
||||
_DECL_STREAM_FUNC(writeblob,-2,_SC("xx")),
|
||||
_DECL_STREAM_FUNC(writen,3,_SC("xnn")),
|
||||
_DECL_STREAM_FUNC(seek,-2,_SC("xnn")),
|
||||
_DECL_STREAM_FUNC(tell,1,_SC("x")),
|
||||
_DECL_STREAM_FUNC(len,1,_SC("x")),
|
||||
_DECL_STREAM_FUNC(eos,1,_SC("x")),
|
||||
_DECL_STREAM_FUNC(flush,1,_SC("x")),
|
||||
_DECL_STREAM_FUNC(_cloned,0,NULL),
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
void init_streamclass(HSQUIRRELVM v)
|
||||
{
|
||||
sq_pushregistrytable(v);
|
||||
sq_pushstring(v,_SC("std_stream"),-1);
|
||||
if(SQ_FAILED(sq_get(v,-2))) {
|
||||
sq_pushstring(v,_SC("std_stream"),-1);
|
||||
sq_newclass(v,SQFalse);
|
||||
sq_settypetag(v,-1,(SQUserPointer)SQSTD_STREAM_TYPE_TAG);
|
||||
SQInteger i = 0;
|
||||
while(_stream_methods[i].name != 0) {
|
||||
const SQRegFunction &f = _stream_methods[i];
|
||||
sq_pushstring(v,f.name,-1);
|
||||
sq_newclosure(v,f.f,0);
|
||||
sq_setparamscheck(v,f.nparamscheck,f.typemask);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
i++;
|
||||
}
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
sq_pushroottable(v);
|
||||
sq_pushstring(v,_SC("stream"),-1);
|
||||
sq_pushstring(v,_SC("std_stream"),-1);
|
||||
sq_get(v,-4);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
sq_pop(v,1);
|
||||
}
|
||||
else {
|
||||
sq_pop(v,1); //result
|
||||
}
|
||||
sq_pop(v,1);
|
||||
}
|
||||
|
||||
SQRESULT declare_stream(HSQUIRRELVM v,const SQChar* name,SQUserPointer typetag,const SQChar* reg_name,const SQRegFunction *methods,const SQRegFunction *globals)
|
||||
{
|
||||
if(sq_gettype(v,-1) != OT_TABLE)
|
||||
return sq_throwerror(v,_SC("table expected"));
|
||||
SQInteger top = sq_gettop(v);
|
||||
//create delegate
|
||||
init_streamclass(v);
|
||||
sq_pushregistrytable(v);
|
||||
sq_pushstring(v,reg_name,-1);
|
||||
sq_pushstring(v,_SC("std_stream"),-1);
|
||||
if(SQ_SUCCEEDED(sq_get(v,-3))) {
|
||||
sq_newclass(v,SQTrue);
|
||||
sq_settypetag(v,-1,typetag);
|
||||
SQInteger i = 0;
|
||||
while(methods[i].name != 0) {
|
||||
const SQRegFunction &f = methods[i];
|
||||
sq_pushstring(v,f.name,-1);
|
||||
sq_newclosure(v,f.f,0);
|
||||
sq_setparamscheck(v,f.nparamscheck,f.typemask);
|
||||
sq_setnativeclosurename(v,-1,f.name);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
i++;
|
||||
}
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
sq_pop(v,1);
|
||||
|
||||
i = 0;
|
||||
while(globals[i].name!=0)
|
||||
{
|
||||
const SQRegFunction &f = globals[i];
|
||||
sq_pushstring(v,f.name,-1);
|
||||
sq_newclosure(v,f.f,0);
|
||||
sq_setparamscheck(v,f.nparamscheck,f.typemask);
|
||||
sq_setnativeclosurename(v,-1,f.name);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
i++;
|
||||
}
|
||||
//register the class in the target table
|
||||
sq_pushstring(v,name,-1);
|
||||
sq_pushregistrytable(v);
|
||||
sq_pushstring(v,reg_name,-1);
|
||||
sq_get(v,-2);
|
||||
sq_remove(v,-2);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
|
||||
sq_settop(v,top);
|
||||
return SQ_OK;
|
||||
}
|
||||
sq_settop(v,top);
|
||||
return SQ_ERROR;
|
||||
}
|
18
external/Squirrel/Lib/sqstdstream.h
vendored
Normal file
18
external/Squirrel/Lib/sqstdstream.h
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
/* see copyright notice in squirrel.h */
|
||||
#ifndef _SQSTD_STREAM_H_
|
||||
#define _SQSTD_STREAM_H_
|
||||
|
||||
SQInteger _stream_readblob(HSQUIRRELVM v);
|
||||
SQInteger _stream_readline(HSQUIRRELVM v);
|
||||
SQInteger _stream_readn(HSQUIRRELVM v);
|
||||
SQInteger _stream_writeblob(HSQUIRRELVM v);
|
||||
SQInteger _stream_writen(HSQUIRRELVM v);
|
||||
SQInteger _stream_seek(HSQUIRRELVM v);
|
||||
SQInteger _stream_tell(HSQUIRRELVM v);
|
||||
SQInteger _stream_len(HSQUIRRELVM v);
|
||||
SQInteger _stream_eos(HSQUIRRELVM v);
|
||||
SQInteger _stream_flush(HSQUIRRELVM v);
|
||||
|
||||
#define _DECL_STREAM_FUNC(name,nparams,typecheck) {_SC(#name),_stream_##name,nparams,typecheck}
|
||||
SQRESULT declare_stream(HSQUIRRELVM v,const SQChar* name,SQUserPointer typetag,const SQChar* reg_name,const SQRegFunction *methods,const SQRegFunction *globals);
|
||||
#endif /*_SQSTD_STREAM_H_*/
|
501
external/Squirrel/Lib/sqstdstring.cpp
vendored
Normal file
501
external/Squirrel/Lib/sqstdstring.cpp
vendored
Normal file
@ -0,0 +1,501 @@
|
||||
/* see copyright notice in squirrel.h */
|
||||
#include <squirrel.h>
|
||||
#include <sqstdstring.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define MAX_FORMAT_LEN 20
|
||||
#define MAX_WFORMAT_LEN 3
|
||||
#define ADDITIONAL_FORMAT_SPACE (100*sizeof(SQChar))
|
||||
|
||||
static SQBool isfmtchr(SQChar ch)
|
||||
{
|
||||
switch(ch) {
|
||||
case '-': case '+': case ' ': case '#': case '0': return SQTrue;
|
||||
}
|
||||
return SQFalse;
|
||||
}
|
||||
|
||||
static SQInteger validate_format(HSQUIRRELVM v, SQChar *fmt, const SQChar *src, SQInteger n,SQInteger &width)
|
||||
{
|
||||
SQChar *dummy;
|
||||
SQChar swidth[MAX_WFORMAT_LEN];
|
||||
SQInteger wc = 0;
|
||||
SQInteger start = n;
|
||||
fmt[0] = '%';
|
||||
while (isfmtchr(src[n])) n++;
|
||||
while (scisdigit(src[n])) {
|
||||
swidth[wc] = src[n];
|
||||
n++;
|
||||
wc++;
|
||||
if(wc>=MAX_WFORMAT_LEN)
|
||||
return sq_throwerror(v,_SC("width format too long"));
|
||||
}
|
||||
swidth[wc] = '\0';
|
||||
if(wc > 0) {
|
||||
width = scstrtol(swidth,&dummy,10);
|
||||
}
|
||||
else
|
||||
width = 0;
|
||||
if (src[n] == '.') {
|
||||
n++;
|
||||
|
||||
wc = 0;
|
||||
while (scisdigit(src[n])) {
|
||||
swidth[wc] = src[n];
|
||||
n++;
|
||||
wc++;
|
||||
if(wc>=MAX_WFORMAT_LEN)
|
||||
return sq_throwerror(v,_SC("precision format too long"));
|
||||
}
|
||||
swidth[wc] = '\0';
|
||||
if(wc > 0) {
|
||||
width += scstrtol(swidth,&dummy,10);
|
||||
|
||||
}
|
||||
}
|
||||
if (n-start > MAX_FORMAT_LEN )
|
||||
return sq_throwerror(v,_SC("format too long"));
|
||||
memcpy(&fmt[1],&src[start],((n-start)+1)*sizeof(SQChar));
|
||||
fmt[(n-start)+2] = '\0';
|
||||
return n;
|
||||
}
|
||||
|
||||
SQRESULT sqstd_format(HSQUIRRELVM v,SQInteger nformatstringidx,SQInteger *outlen,SQChar **output)
|
||||
{
|
||||
const SQChar *format;
|
||||
SQChar *dest;
|
||||
SQChar fmt[MAX_FORMAT_LEN];
|
||||
sq_getstring(v,nformatstringidx,&format);
|
||||
SQInteger format_size = sq_getsize(v,nformatstringidx);
|
||||
SQInteger allocated = (format_size+2)*sizeof(SQChar);
|
||||
dest = sq_getscratchpad(v,allocated);
|
||||
SQInteger n = 0,i = 0, nparam = nformatstringidx+1, w = 0;
|
||||
//while(format[n] != '\0')
|
||||
while(n < format_size)
|
||||
{
|
||||
if(format[n] != '%') {
|
||||
assert(i < allocated);
|
||||
dest[i++] = format[n];
|
||||
n++;
|
||||
}
|
||||
else if(format[n+1] == '%') { //handles %%
|
||||
dest[i++] = '%';
|
||||
n += 2;
|
||||
}
|
||||
else {
|
||||
n++;
|
||||
if( nparam > sq_gettop(v) )
|
||||
return sq_throwerror(v,_SC("not enough paramters for the given format string"));
|
||||
n = validate_format(v,fmt,format,n,w);
|
||||
if(n < 0) return -1;
|
||||
SQInteger addlen = 0;
|
||||
SQInteger valtype = 0;
|
||||
const SQChar *ts = NULL;
|
||||
SQInteger ti = 0;
|
||||
SQFloat tf = 0;
|
||||
switch(format[n]) {
|
||||
case 's':
|
||||
if(SQ_FAILED(sq_getstring(v,nparam,&ts)))
|
||||
return sq_throwerror(v,_SC("string expected for the specified format"));
|
||||
addlen = (sq_getsize(v,nparam)*sizeof(SQChar))+((w+1)*sizeof(SQChar));
|
||||
valtype = 's';
|
||||
break;
|
||||
case 'i': case 'd': case 'o': case 'u': case 'x': case 'X':
|
||||
#ifdef _SQ64
|
||||
{
|
||||
size_t flen = scstrlen(fmt);
|
||||
SQInteger fpos = flen - 1;
|
||||
SQChar f = fmt[fpos];
|
||||
const SQChar *prec = (const SQChar *)_PRINT_INT_PREC;
|
||||
while(*prec != _SC('\0')) {
|
||||
fmt[fpos++] = *prec++;
|
||||
}
|
||||
fmt[fpos++] = f;
|
||||
fmt[fpos++] = _SC('\0');
|
||||
}
|
||||
#endif
|
||||
case 'c':
|
||||
if(SQ_FAILED(sq_getinteger(v,nparam,&ti)))
|
||||
return sq_throwerror(v,_SC("integer expected for the specified format"));
|
||||
addlen = (ADDITIONAL_FORMAT_SPACE)+((w+1)*sizeof(SQChar));
|
||||
valtype = 'i';
|
||||
break;
|
||||
case 'f': case 'g': case 'G': case 'e': case 'E':
|
||||
if(SQ_FAILED(sq_getfloat(v,nparam,&tf)))
|
||||
return sq_throwerror(v,_SC("float expected for the specified format"));
|
||||
addlen = (ADDITIONAL_FORMAT_SPACE)+((w+1)*sizeof(SQChar));
|
||||
valtype = 'f';
|
||||
break;
|
||||
default:
|
||||
return sq_throwerror(v,_SC("invalid format"));
|
||||
}
|
||||
n++;
|
||||
allocated += addlen + sizeof(SQChar);
|
||||
dest = sq_getscratchpad(v,allocated);
|
||||
switch(valtype) {
|
||||
case 's': i += scsprintf(&dest[i],allocated,fmt,ts); break;
|
||||
case 'i': i += scsprintf(&dest[i],allocated,fmt,ti); break;
|
||||
case 'f': i += scsprintf(&dest[i],allocated,fmt,tf); break;
|
||||
};
|
||||
nparam ++;
|
||||
}
|
||||
}
|
||||
*outlen = i;
|
||||
dest[i] = '\0';
|
||||
*output = dest;
|
||||
return SQ_OK;
|
||||
}
|
||||
|
||||
static SQInteger _string_format(HSQUIRRELVM v)
|
||||
{
|
||||
SQChar *dest = NULL;
|
||||
SQInteger length = 0;
|
||||
if(SQ_FAILED(sqstd_format(v,2,&length,&dest)))
|
||||
return -1;
|
||||
sq_pushstring(v,dest,length);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void __strip_l(const SQChar *str,const SQChar **start)
|
||||
{
|
||||
const SQChar *t = str;
|
||||
while(((*t) != '\0') && scisspace(*t)){ t++; }
|
||||
*start = t;
|
||||
}
|
||||
|
||||
static void __strip_r(const SQChar *str,SQInteger len,const SQChar **end)
|
||||
{
|
||||
if(len == 0) {
|
||||
*end = str;
|
||||
return;
|
||||
}
|
||||
const SQChar *t = &str[len-1];
|
||||
while(t >= str && scisspace(*t)) { t--; }
|
||||
*end = t + 1;
|
||||
}
|
||||
|
||||
static SQInteger _string_strip(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *str,*start,*end;
|
||||
sq_getstring(v,2,&str);
|
||||
SQInteger len = sq_getsize(v,2);
|
||||
__strip_l(str,&start);
|
||||
__strip_r(str,len,&end);
|
||||
sq_pushstring(v,start,end - start);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _string_lstrip(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *str,*start;
|
||||
sq_getstring(v,2,&str);
|
||||
__strip_l(str,&start);
|
||||
sq_pushstring(v,start,-1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _string_rstrip(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *str,*end;
|
||||
sq_getstring(v,2,&str);
|
||||
SQInteger len = sq_getsize(v,2);
|
||||
__strip_r(str,len,&end);
|
||||
sq_pushstring(v,str,end - str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _string_split(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *str,*seps;
|
||||
SQChar *stemp;
|
||||
sq_getstring(v,2,&str);
|
||||
sq_getstring(v,3,&seps);
|
||||
SQInteger sepsize = sq_getsize(v,3);
|
||||
if(sepsize == 0) return sq_throwerror(v,_SC("empty separators string"));
|
||||
SQInteger memsize = (sq_getsize(v,2)+1)*sizeof(SQChar);
|
||||
stemp = sq_getscratchpad(v,memsize);
|
||||
memcpy(stemp,str,memsize);
|
||||
SQChar *start = stemp;
|
||||
SQChar *end = stemp;
|
||||
sq_newarray(v,0);
|
||||
while(*end != '\0')
|
||||
{
|
||||
SQChar cur = *end;
|
||||
for(SQInteger i = 0; i < sepsize; i++)
|
||||
{
|
||||
if(cur == seps[i])
|
||||
{
|
||||
*end = 0;
|
||||
sq_pushstring(v,start,-1);
|
||||
sq_arrayappend(v,-2);
|
||||
start = end + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
end++;
|
||||
}
|
||||
if(end != start)
|
||||
{
|
||||
sq_pushstring(v,start,-1);
|
||||
sq_arrayappend(v,-2);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _string_escape(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *str;
|
||||
SQChar *dest,*resstr;
|
||||
SQInteger size;
|
||||
sq_getstring(v,2,&str);
|
||||
size = sq_getsize(v,2);
|
||||
if(size == 0) {
|
||||
sq_push(v,2);
|
||||
return 1;
|
||||
}
|
||||
#ifdef SQUNICODE
|
||||
#if WCHAR_SIZE == 2
|
||||
const SQChar *escpat = _SC("\\x%04x");
|
||||
const SQInteger maxescsize = 6;
|
||||
#else //WCHAR_SIZE == 4
|
||||
const SQChar *escpat = _SC("\\x%08x");
|
||||
const SQInteger maxescsize = 10;
|
||||
#endif
|
||||
#else
|
||||
const SQChar *escpat = _SC("\\x%02x");
|
||||
const SQInteger maxescsize = 4;
|
||||
#endif
|
||||
SQInteger destcharsize = (size * maxescsize); //assumes every char could be escaped
|
||||
resstr = dest = (SQChar *)sq_getscratchpad(v,destcharsize * sizeof(SQChar));
|
||||
SQChar c;
|
||||
SQChar escch;
|
||||
SQInteger escaped = 0;
|
||||
for(int n = 0; n < size; n++){
|
||||
c = *str++;
|
||||
escch = 0;
|
||||
if(scisprint(c) || c == 0) {
|
||||
switch(c) {
|
||||
case '\a': escch = 'a'; break;
|
||||
case '\b': escch = 'b'; break;
|
||||
case '\t': escch = 't'; break;
|
||||
case '\n': escch = 'n'; break;
|
||||
case '\v': escch = 'v'; break;
|
||||
case '\f': escch = 'f'; break;
|
||||
case '\r': escch = 'r'; break;
|
||||
case '\\': escch = '\\'; break;
|
||||
case '\"': escch = '\"'; break;
|
||||
case '\'': escch = '\''; break;
|
||||
case 0: escch = '0'; break;
|
||||
}
|
||||
if(escch) {
|
||||
*dest++ = '\\';
|
||||
*dest++ = escch;
|
||||
escaped++;
|
||||
}
|
||||
else {
|
||||
*dest++ = c;
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
dest += scsprintf(dest, destcharsize, escpat, c);
|
||||
escaped++;
|
||||
}
|
||||
}
|
||||
|
||||
if(escaped) {
|
||||
sq_pushstring(v,resstr,dest - resstr);
|
||||
}
|
||||
else {
|
||||
sq_push(v,2); //nothing escaped
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _string_startswith(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *str,*cmp;
|
||||
sq_getstring(v,2,&str);
|
||||
sq_getstring(v,3,&cmp);
|
||||
SQInteger len = sq_getsize(v,2);
|
||||
SQInteger cmplen = sq_getsize(v,3);
|
||||
SQBool ret = SQFalse;
|
||||
if(cmplen <= len) {
|
||||
ret = memcmp(str,cmp,sq_rsl(cmplen)) == 0 ? SQTrue : SQFalse;
|
||||
}
|
||||
sq_pushbool(v,ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _string_endswith(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *str,*cmp;
|
||||
sq_getstring(v,2,&str);
|
||||
sq_getstring(v,3,&cmp);
|
||||
SQInteger len = sq_getsize(v,2);
|
||||
SQInteger cmplen = sq_getsize(v,3);
|
||||
SQBool ret = SQFalse;
|
||||
if(cmplen <= len) {
|
||||
ret = memcmp(&str[len - cmplen],cmp,sq_rsl(cmplen)) == 0 ? SQTrue : SQFalse;
|
||||
}
|
||||
sq_pushbool(v,ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define SETUP_REX(v) \
|
||||
SQRex *self = NULL; \
|
||||
sq_getinstanceup(v,1,(SQUserPointer *)&self,0);
|
||||
|
||||
static SQInteger _rexobj_releasehook(SQUserPointer p, SQInteger SQ_UNUSED_ARG(size))
|
||||
{
|
||||
SQRex *self = ((SQRex *)p);
|
||||
sqstd_rex_free(self);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _regexp_match(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_REX(v);
|
||||
const SQChar *str;
|
||||
sq_getstring(v,2,&str);
|
||||
if(sqstd_rex_match(self,str) == SQTrue)
|
||||
{
|
||||
sq_pushbool(v,SQTrue);
|
||||
return 1;
|
||||
}
|
||||
sq_pushbool(v,SQFalse);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void _addrexmatch(HSQUIRRELVM v,const SQChar *str,const SQChar *begin,const SQChar *end)
|
||||
{
|
||||
sq_newtable(v);
|
||||
sq_pushstring(v,_SC("begin"),-1);
|
||||
sq_pushinteger(v,begin - str);
|
||||
sq_rawset(v,-3);
|
||||
sq_pushstring(v,_SC("end"),-1);
|
||||
sq_pushinteger(v,end - str);
|
||||
sq_rawset(v,-3);
|
||||
}
|
||||
|
||||
static SQInteger _regexp_search(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_REX(v);
|
||||
const SQChar *str,*begin,*end;
|
||||
SQInteger start = 0;
|
||||
sq_getstring(v,2,&str);
|
||||
if(sq_gettop(v) > 2) sq_getinteger(v,3,&start);
|
||||
if(sqstd_rex_search(self,str+start,&begin,&end) == SQTrue) {
|
||||
_addrexmatch(v,str,begin,end);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SQInteger _regexp_capture(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_REX(v);
|
||||
const SQChar *str,*begin,*end;
|
||||
SQInteger start = 0;
|
||||
sq_getstring(v,2,&str);
|
||||
if(sq_gettop(v) > 2) sq_getinteger(v,3,&start);
|
||||
if(sqstd_rex_search(self,str+start,&begin,&end) == SQTrue) {
|
||||
SQInteger n = sqstd_rex_getsubexpcount(self);
|
||||
SQRexMatch match;
|
||||
sq_newarray(v,0);
|
||||
for(SQInteger i = 0;i < n; i++) {
|
||||
sqstd_rex_getsubexp(self,i,&match);
|
||||
if(match.len > 0)
|
||||
_addrexmatch(v,str,match.begin,match.begin+match.len);
|
||||
else
|
||||
_addrexmatch(v,str,str,str); //empty match
|
||||
sq_arrayappend(v,-2);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SQInteger _regexp_subexpcount(HSQUIRRELVM v)
|
||||
{
|
||||
SETUP_REX(v);
|
||||
sq_pushinteger(v,sqstd_rex_getsubexpcount(self));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _regexp_constructor(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *error,*pattern;
|
||||
sq_getstring(v,2,&pattern);
|
||||
SQRex *rex = sqstd_rex_compile(pattern,&error);
|
||||
if(!rex) return sq_throwerror(v,error);
|
||||
sq_setinstanceup(v,1,rex);
|
||||
sq_setreleasehook(v,1,_rexobj_releasehook);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SQInteger _regexp__typeof(HSQUIRRELVM v)
|
||||
{
|
||||
sq_pushstring(v,_SC("regexp"),-1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define _DECL_REX_FUNC(name,nparams,pmask) {_SC(#name),_regexp_##name,nparams,pmask}
|
||||
static const SQRegFunction rexobj_funcs[]={
|
||||
_DECL_REX_FUNC(constructor,2,_SC(".s")),
|
||||
_DECL_REX_FUNC(search,-2,_SC("xsn")),
|
||||
_DECL_REX_FUNC(match,2,_SC("xs")),
|
||||
_DECL_REX_FUNC(capture,-2,_SC("xsn")),
|
||||
_DECL_REX_FUNC(subexpcount,1,_SC("x")),
|
||||
_DECL_REX_FUNC(_typeof,1,_SC("x")),
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
#undef _DECL_REX_FUNC
|
||||
|
||||
#define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_string_##name,nparams,pmask}
|
||||
static const SQRegFunction stringlib_funcs[]={
|
||||
_DECL_FUNC(format,-2,_SC(".s")),
|
||||
_DECL_FUNC(strip,2,_SC(".s")),
|
||||
_DECL_FUNC(lstrip,2,_SC(".s")),
|
||||
_DECL_FUNC(rstrip,2,_SC(".s")),
|
||||
_DECL_FUNC(split,3,_SC(".ss")),
|
||||
_DECL_FUNC(escape,2,_SC(".s")),
|
||||
_DECL_FUNC(startswith,3,_SC(".ss")),
|
||||
_DECL_FUNC(endswith,3,_SC(".ss")),
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
#undef _DECL_FUNC
|
||||
|
||||
|
||||
SQInteger sqstd_register_stringlib(HSQUIRRELVM v)
|
||||
{
|
||||
sq_pushstring(v,_SC("regexp"),-1);
|
||||
sq_newclass(v,SQFalse);
|
||||
SQInteger i = 0;
|
||||
while(rexobj_funcs[i].name != 0) {
|
||||
const SQRegFunction &f = rexobj_funcs[i];
|
||||
sq_pushstring(v,f.name,-1);
|
||||
sq_newclosure(v,f.f,0);
|
||||
sq_setparamscheck(v,f.nparamscheck,f.typemask);
|
||||
sq_setnativeclosurename(v,-1,f.name);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
i++;
|
||||
}
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
|
||||
i = 0;
|
||||
while(stringlib_funcs[i].name!=0)
|
||||
{
|
||||
sq_pushstring(v,stringlib_funcs[i].name,-1);
|
||||
sq_newclosure(v,stringlib_funcs[i].f,0);
|
||||
sq_setparamscheck(v,stringlib_funcs[i].nparamscheck,stringlib_funcs[i].typemask);
|
||||
sq_setnativeclosurename(v,-1,stringlib_funcs[i].name);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
i++;
|
||||
}
|
||||
return 1;
|
||||
}
|
146
external/Squirrel/Lib/sqstdsystem.cpp
vendored
Normal file
146
external/Squirrel/Lib/sqstdsystem.cpp
vendored
Normal file
@ -0,0 +1,146 @@
|
||||
/* see copyright notice in squirrel.h */
|
||||
#include <squirrel.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sqstdsystem.h>
|
||||
|
||||
#ifdef SQUNICODE
|
||||
#include <wchar.h>
|
||||
#define scgetenv _wgetenv
|
||||
#define scsystem _wsystem
|
||||
#define scasctime _wasctime
|
||||
#define scremove _wremove
|
||||
#define screname _wrename
|
||||
#else
|
||||
#define scgetenv getenv
|
||||
#define scsystem system
|
||||
#define scasctime asctime
|
||||
#define scremove remove
|
||||
#define screname rename
|
||||
#endif
|
||||
|
||||
static SQInteger _system_getenv(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *s;
|
||||
if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
|
||||
sq_pushstring(v,scgetenv(s),-1);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static SQInteger _system_system(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *s;
|
||||
if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
|
||||
sq_pushinteger(v,scsystem(s));
|
||||
return 1;
|
||||
}
|
||||
return sq_throwerror(v,_SC("wrong param"));
|
||||
}
|
||||
|
||||
|
||||
static SQInteger _system_clock(HSQUIRRELVM v)
|
||||
{
|
||||
sq_pushfloat(v,((SQFloat)clock())/(SQFloat)CLOCKS_PER_SEC);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _system_time(HSQUIRRELVM v)
|
||||
{
|
||||
SQInteger t = (SQInteger)time(NULL);
|
||||
sq_pushinteger(v,t);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _system_remove(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *s;
|
||||
sq_getstring(v,2,&s);
|
||||
if(scremove(s)==-1)
|
||||
return sq_throwerror(v,_SC("remove() failed"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SQInteger _system_rename(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *oldn,*newn;
|
||||
sq_getstring(v,2,&oldn);
|
||||
sq_getstring(v,3,&newn);
|
||||
if(screname(oldn,newn)==-1)
|
||||
return sq_throwerror(v,_SC("rename() failed"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void _set_integer_slot(HSQUIRRELVM v,const SQChar *name,SQInteger val)
|
||||
{
|
||||
sq_pushstring(v,name,-1);
|
||||
sq_pushinteger(v,val);
|
||||
sq_rawset(v,-3);
|
||||
}
|
||||
|
||||
static SQInteger _system_date(HSQUIRRELVM v)
|
||||
{
|
||||
time_t t;
|
||||
SQInteger it;
|
||||
SQInteger format = 'l';
|
||||
if(sq_gettop(v) > 1) {
|
||||
sq_getinteger(v,2,&it);
|
||||
t = it;
|
||||
if(sq_gettop(v) > 2) {
|
||||
sq_getinteger(v,3,(SQInteger*)&format);
|
||||
}
|
||||
}
|
||||
else {
|
||||
time(&t);
|
||||
}
|
||||
tm *date;
|
||||
if(format == 'u')
|
||||
date = gmtime(&t);
|
||||
else
|
||||
date = localtime(&t);
|
||||
if(!date)
|
||||
return sq_throwerror(v,_SC("crt api failure"));
|
||||
sq_newtable(v);
|
||||
_set_integer_slot(v, _SC("sec"), date->tm_sec);
|
||||
_set_integer_slot(v, _SC("min"), date->tm_min);
|
||||
_set_integer_slot(v, _SC("hour"), date->tm_hour);
|
||||
_set_integer_slot(v, _SC("day"), date->tm_mday);
|
||||
_set_integer_slot(v, _SC("month"), date->tm_mon);
|
||||
_set_integer_slot(v, _SC("year"), date->tm_year+1900);
|
||||
_set_integer_slot(v, _SC("wday"), date->tm_wday);
|
||||
_set_integer_slot(v, _SC("yday"), date->tm_yday);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_system_##name,nparams,pmask}
|
||||
static const SQRegFunction systemlib_funcs[]={
|
||||
_DECL_FUNC(getenv,2,_SC(".s")),
|
||||
_DECL_FUNC(system,2,_SC(".s")),
|
||||
_DECL_FUNC(clock,0,NULL),
|
||||
_DECL_FUNC(time,1,NULL),
|
||||
_DECL_FUNC(date,-1,_SC(".nn")),
|
||||
_DECL_FUNC(remove,2,_SC(".s")),
|
||||
_DECL_FUNC(rename,3,_SC(".ss")),
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
#undef _DECL_FUNC
|
||||
|
||||
SQInteger sqstd_register_systemlib(HSQUIRRELVM v)
|
||||
{
|
||||
SQInteger i=0;
|
||||
while(systemlib_funcs[i].name!=0)
|
||||
{
|
||||
sq_pushstring(v,systemlib_funcs[i].name,-1);
|
||||
sq_newclosure(v,systemlib_funcs[i].f,0);
|
||||
sq_setparamscheck(v,systemlib_funcs[i].nparamscheck,systemlib_funcs[i].typemask);
|
||||
sq_setnativeclosurename(v,-1,systemlib_funcs[i].name);
|
||||
sq_newslot(v,-3,SQFalse);
|
||||
i++;
|
||||
}
|
||||
return 1;
|
||||
}
|
2
external/Squirrel/sqapi.cpp
vendored
2
external/Squirrel/sqapi.cpp
vendored
@ -180,7 +180,7 @@ SQBool sq_release(HSQUIRRELVM v,HSQOBJECT *po)
|
||||
#endif
|
||||
}
|
||||
|
||||
SQUnsignedInteger sq_getvmrefcount(HSQUIRRELVM v, const HSQOBJECT *po)
|
||||
SQUnsignedInteger sq_getvmrefcount(HSQUIRRELVM SQ_UNUSED_ARG(v), const HSQOBJECT *po)
|
||||
{
|
||||
if (!ISREFCOUNTED(type(*po))) return 0;
|
||||
return po->_unVal.pRefCounted->_uiRef;
|
||||
|
49
external/Squirrel/sqbaselib.cpp
vendored
49
external/Squirrel/sqbaselib.cpp
vendored
@ -40,7 +40,7 @@ static bool str2num(const SQChar *s,SQObjectPtr &res,SQInteger base)
|
||||
return true;
|
||||
}
|
||||
|
||||
static SQInteger base_dummy(HSQUIRRELVM v)
|
||||
static SQInteger base_dummy(HSQUIRRELVM SQ_UNUSED_ARG(v))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@ -272,7 +272,7 @@ static SQInteger base_callee(HSQUIRRELVM v)
|
||||
return sq_throwerror(v,_SC("no closure in the calls stack"));
|
||||
}
|
||||
|
||||
static SQRegFunction base_funcs[]={
|
||||
static const SQRegFunction base_funcs[]={
|
||||
//generic
|
||||
{_SC("seterrorhandler"),base_seterrorhandler,2, NULL},
|
||||
{_SC("setdebughook"),base_setdebughook,2, NULL},
|
||||
@ -296,7 +296,7 @@ static SQRegFunction base_funcs[]={
|
||||
{_SC("collectgarbage"),base_collectgarbage,0, NULL},
|
||||
{_SC("resurrectunreachable"),base_resurectureachable,0, NULL},
|
||||
#endif
|
||||
{0,0}
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
void sq_base_register(HSQUIRRELVM v)
|
||||
@ -464,7 +464,7 @@ static SQInteger table_getdelegate(HSQUIRRELVM v)
|
||||
return SQ_SUCCEEDED(sq_getdelegate(v,-1))?1:SQ_ERROR;
|
||||
}
|
||||
|
||||
SQRegFunction SQSharedState::_table_default_delegate_funcz[]={
|
||||
const SQRegFunction SQSharedState::_table_default_delegate_funcz[]={
|
||||
{_SC("len"),default_delegate_len,1, _SC("t")},
|
||||
{_SC("rawget"),container_rawget,2, _SC("t")},
|
||||
{_SC("rawset"),container_rawset,3, _SC("t")},
|
||||
@ -475,7 +475,7 @@ SQRegFunction SQSharedState::_table_default_delegate_funcz[]={
|
||||
{_SC("clear"),obj_clear,1, _SC(".")},
|
||||
{_SC("setdelegate"),table_setdelegate,2, _SC(".t|o")},
|
||||
{_SC("getdelegate"),table_getdelegate,1, _SC(".")},
|
||||
{0,0}
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
//ARRAY DEFAULT DELEGATE///////////////////////////////////////
|
||||
@ -721,7 +721,7 @@ static bool _hsort_sift_down(HSQUIRRELVM v,SQArray *arr, SQInteger root, SQInteg
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool _hsort(HSQUIRRELVM v,SQObjectPtr &arr, SQInteger l, SQInteger r,SQInteger func)
|
||||
static bool _hsort(HSQUIRRELVM v,SQObjectPtr &arr, SQInteger SQ_UNUSED_ARG(l), SQInteger SQ_UNUSED_ARG(r),SQInteger func)
|
||||
{
|
||||
SQArray *a = _array(arr);
|
||||
SQInteger i;
|
||||
@ -773,7 +773,7 @@ static SQInteger array_slice(HSQUIRRELVM v)
|
||||
|
||||
}
|
||||
|
||||
SQRegFunction SQSharedState::_array_default_delegate_funcz[]={
|
||||
const SQRegFunction SQSharedState::_array_default_delegate_funcz[]={
|
||||
{_SC("len"),default_delegate_len,1, _SC("a")},
|
||||
{_SC("append"),array_append,2, _SC("a")},
|
||||
{_SC("extend"),array_extend,2, _SC("aa")},
|
||||
@ -794,7 +794,7 @@ SQRegFunction SQSharedState::_array_default_delegate_funcz[]={
|
||||
{_SC("reduce"),array_reduce,2, _SC("ac")},
|
||||
{_SC("filter"),array_filter,2, _SC("ac")},
|
||||
{_SC("find"),array_find,2, _SC("a.")},
|
||||
{0,0}
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
//STRING DEFAULT DELEGATE//////////////////////////
|
||||
@ -853,7 +853,7 @@ static SQInteger string_find(HSQUIRRELVM v)
|
||||
STRING_TOFUNCZ(tolower)
|
||||
STRING_TOFUNCZ(toupper)
|
||||
|
||||
SQRegFunction SQSharedState::_string_default_delegate_funcz[]={
|
||||
const SQRegFunction SQSharedState::_string_default_delegate_funcz[]={
|
||||
{_SC("len"),default_delegate_len,1, _SC("s")},
|
||||
{_SC("tointeger"),default_delegate_tointeger,-1, _SC("sn")},
|
||||
{_SC("tofloat"),default_delegate_tofloat,1, _SC("s")},
|
||||
@ -863,17 +863,17 @@ SQRegFunction SQSharedState::_string_default_delegate_funcz[]={
|
||||
{_SC("tolower"),string_tolower,-1, _SC("s n n")},
|
||||
{_SC("toupper"),string_toupper,-1, _SC("s n n")},
|
||||
{_SC("weakref"),obj_delegate_weakref,1, NULL },
|
||||
{0,0}
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
//INTEGER DEFAULT DELEGATE//////////////////////////
|
||||
SQRegFunction SQSharedState::_number_default_delegate_funcz[]={
|
||||
const SQRegFunction SQSharedState::_number_default_delegate_funcz[]={
|
||||
{_SC("tointeger"),default_delegate_tointeger,1, _SC("n|b")},
|
||||
{_SC("tofloat"),default_delegate_tofloat,1, _SC("n|b")},
|
||||
{_SC("tostring"),default_delegate_tostring,1, _SC(".")},
|
||||
{_SC("tochar"),number_delegate_tochar,1, _SC("n|b")},
|
||||
{_SC("weakref"),obj_delegate_weakref,1, NULL },
|
||||
{0,0}
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
//CLOSURE DEFAULT DELEGATE//////////////////////////
|
||||
@ -972,7 +972,7 @@ static SQInteger closure_getinfos(HSQUIRRELVM v) {
|
||||
|
||||
|
||||
|
||||
SQRegFunction SQSharedState::_closure_default_delegate_funcz[]={
|
||||
const SQRegFunction SQSharedState::_closure_default_delegate_funcz[]={
|
||||
{_SC("call"),closure_call,-1, _SC("c")},
|
||||
{_SC("pcall"),closure_pcall,-1, _SC("c")},
|
||||
{_SC("acall"),closure_acall,2, _SC("ca")},
|
||||
@ -983,7 +983,7 @@ SQRegFunction SQSharedState::_closure_default_delegate_funcz[]={
|
||||
{_SC("getinfos"),closure_getinfos,1, _SC("c")},
|
||||
{_SC("getroot"),closure_getroot,1, _SC("c")},
|
||||
{_SC("setroot"),closure_setroot,2, _SC("ct")},
|
||||
{0,0}
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
//GENERATOR DEFAULT DELEGATE
|
||||
@ -998,11 +998,11 @@ static SQInteger generator_getstatus(HSQUIRRELVM v)
|
||||
return 1;
|
||||
}
|
||||
|
||||
SQRegFunction SQSharedState::_generator_default_delegate_funcz[]={
|
||||
const SQRegFunction SQSharedState::_generator_default_delegate_funcz[]={
|
||||
{_SC("getstatus"),generator_getstatus,1, _SC("g")},
|
||||
{_SC("weakref"),obj_delegate_weakref,1, NULL },
|
||||
{_SC("tostring"),default_delegate_tostring,1, _SC(".")},
|
||||
{0,0}
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
//THREAD DEFAULT DELEGATE
|
||||
@ -1154,7 +1154,7 @@ static SQInteger thread_getstackinfos(HSQUIRRELVM v)
|
||||
return sq_throwerror(v,_SC("wrong parameter"));
|
||||
}
|
||||
|
||||
SQRegFunction SQSharedState::_thread_default_delegate_funcz[] = {
|
||||
const SQRegFunction SQSharedState::_thread_default_delegate_funcz[] = {
|
||||
{_SC("call"), thread_call, -1, _SC("v")},
|
||||
{_SC("wakeup"), thread_wakeup, -1, _SC("v")},
|
||||
{_SC("wakeupthrow"), thread_wakeupthrow, -2, _SC("v.b")},
|
||||
@ -1162,7 +1162,7 @@ SQRegFunction SQSharedState::_thread_default_delegate_funcz[] = {
|
||||
{_SC("weakref"),obj_delegate_weakref,1, NULL },
|
||||
{_SC("getstackinfos"),thread_getstackinfos,2, _SC("vn")},
|
||||
{_SC("tostring"),default_delegate_tostring,1, _SC(".")},
|
||||
{0,0},
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
static SQInteger class_getattributes(HSQUIRRELVM v)
|
||||
@ -1217,7 +1217,7 @@ static SQInteger class_rawnewmember(HSQUIRRELVM v)
|
||||
return SQ_SUCCEEDED(sq_rawnewmember(v,-4,bstatic))?1:SQ_ERROR;
|
||||
}
|
||||
|
||||
SQRegFunction SQSharedState::_class_default_delegate_funcz[] = {
|
||||
const SQRegFunction SQSharedState::_class_default_delegate_funcz[] = {
|
||||
{_SC("getattributes"), class_getattributes, 2, _SC("y.")},
|
||||
{_SC("setattributes"), class_setattributes, 3, _SC("y..")},
|
||||
{_SC("rawget"),container_rawget,2, _SC("y")},
|
||||
@ -1229,7 +1229,7 @@ SQRegFunction SQSharedState::_class_default_delegate_funcz[] = {
|
||||
{_SC("getbase"),class_getbase,1, _SC("y")},
|
||||
{_SC("newmember"),class_newmember,-3, _SC("y")},
|
||||
{_SC("rawnewmember"),class_rawnewmember,-3, _SC("y")},
|
||||
{0,0}
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
|
||||
@ -1240,14 +1240,14 @@ static SQInteger instance_getclass(HSQUIRRELVM v)
|
||||
return SQ_ERROR;
|
||||
}
|
||||
|
||||
SQRegFunction SQSharedState::_instance_default_delegate_funcz[] = {
|
||||
const SQRegFunction SQSharedState::_instance_default_delegate_funcz[] = {
|
||||
{_SC("getclass"), instance_getclass, 1, _SC("x")},
|
||||
{_SC("rawget"),container_rawget,2, _SC("x")},
|
||||
{_SC("rawset"),container_rawset,3, _SC("x")},
|
||||
{_SC("rawin"),container_rawexists,2, _SC("x")},
|
||||
{_SC("weakref"),obj_delegate_weakref,1, NULL },
|
||||
{_SC("tostring"),default_delegate_tostring,1, _SC(".")},
|
||||
{0,0}
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
static SQInteger weakref_ref(HSQUIRRELVM v)
|
||||
@ -1257,10 +1257,9 @@ static SQInteger weakref_ref(HSQUIRRELVM v)
|
||||
return 1;
|
||||
}
|
||||
|
||||
SQRegFunction SQSharedState::_weakref_default_delegate_funcz[] = {
|
||||
const SQRegFunction SQSharedState::_weakref_default_delegate_funcz[] = {
|
||||
{_SC("ref"),weakref_ref,1, _SC("r")},
|
||||
{_SC("weakref"),obj_delegate_weakref,1, NULL },
|
||||
{_SC("tostring"),default_delegate_tostring,1, _SC(".")},
|
||||
{0,0}
|
||||
{NULL,(SQFUNCTION)0,0,NULL}
|
||||
};
|
||||
|
||||
|
2
external/Squirrel/sqclass.cpp
vendored
2
external/Squirrel/sqclass.cpp
vendored
@ -189,7 +189,7 @@ SQInstance::~SQInstance()
|
||||
if(_class){ Finalize(); } //if _class is null it was already finalized by the GC
|
||||
}
|
||||
|
||||
bool SQInstance::GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res)
|
||||
bool SQInstance::GetMetaMethod(SQVM SQ_UNUSED_ARG(*v),SQMetaMethod mm,SQObjectPtr &res)
|
||||
{
|
||||
if(type(_class->_metamethods[mm]) != OT_NULL) {
|
||||
res = _class->_metamethods[mm];
|
||||
|
1
external/Squirrel/sqcompiler.cpp
vendored
1
external/Squirrel/sqcompiler.cpp
vendored
@ -982,6 +982,7 @@ public:
|
||||
SQInteger val = _fs->PopTarget();
|
||||
SQInteger key = _fs->PopTarget();
|
||||
SQInteger attrs = hasattrs ? _fs->PopTarget():-1;
|
||||
((void)attrs);
|
||||
assert((hasattrs && (attrs == key-1)) || !hasattrs);
|
||||
unsigned char flags = (hasattrs?NEW_SLOT_ATTRIBUTES_FLAG:0)|(isstatic?NEW_SLOT_STATIC_FLAG:0);
|
||||
SQInteger table = _fs->TopTarget(); //<<BECAUSE OF THIS NO COMMON EMIT FUNC IS POSSIBLE
|
||||
|
96
external/Squirrel/sqlexer.cpp
vendored
96
external/Squirrel/sqlexer.cpp
vendored
@ -287,6 +287,65 @@ SQInteger SQLexer::GetIDType(const SQChar *s,SQInteger len)
|
||||
return TK_IDENTIFIER;
|
||||
}
|
||||
|
||||
#ifdef SQUNICODE
|
||||
#if WCHAR_SIZE == 2
|
||||
SQInteger SQLexer::AddUTF16(SQUnsignedInteger ch)
|
||||
{
|
||||
if (ch >= 0x10000)
|
||||
{
|
||||
SQUnsignedInteger code = (ch - 0x10000);
|
||||
APPEND_CHAR((SQChar)(0xD800 | (code >> 10)));
|
||||
APPEND_CHAR((SQChar)(0xDC00 | (code & 0x3FF)));
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
APPEND_CHAR((SQChar)ch);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
SQInteger SQLexer::AddUTF8(SQUnsignedInteger ch)
|
||||
{
|
||||
if (ch < 0x80) {
|
||||
APPEND_CHAR((char)ch);
|
||||
return 1;
|
||||
}
|
||||
if (ch < 0x800) {
|
||||
APPEND_CHAR((SQChar)((ch >> 6) | 0xC0));
|
||||
APPEND_CHAR((SQChar)((ch & 0x3F) | 0x80));
|
||||
return 2;
|
||||
}
|
||||
if (ch < 0x10000) {
|
||||
APPEND_CHAR((SQChar)((ch >> 12) | 0xE0));
|
||||
APPEND_CHAR((SQChar)(((ch >> 6) & 0x3F) | 0x80));
|
||||
APPEND_CHAR((SQChar)((ch & 0x3F) | 0x80));
|
||||
return 3;
|
||||
}
|
||||
if (ch < 0x110000) {
|
||||
APPEND_CHAR((SQChar)((ch >> 18) | 0xF0));
|
||||
APPEND_CHAR((SQChar)(((ch >> 12) & 0x3F) | 0x80));
|
||||
APPEND_CHAR((SQChar)(((ch >> 6) & 0x3F) | 0x80));
|
||||
APPEND_CHAR((SQChar)((ch & 0x3F) | 0x80));
|
||||
return 4;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
SQInteger SQLexer::ProcessStringHexEscape(SQChar *dest, SQInteger maxdigits)
|
||||
{
|
||||
NEXT();
|
||||
if (!isxdigit(CUR_CHAR)) Error(_SC("hexadecimal number expected"));
|
||||
SQInteger n = 0;
|
||||
while (isxdigit(CUR_CHAR) && n < maxdigits) {
|
||||
dest[n] = CUR_CHAR;
|
||||
n++;
|
||||
NEXT();
|
||||
}
|
||||
dest[n] = 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
SQInteger SQLexer::ReadString(SQInteger ndelim,bool verbatim)
|
||||
{
|
||||
@ -295,7 +354,8 @@ SQInteger SQLexer::ReadString(SQInteger ndelim,bool verbatim)
|
||||
if(IS_EOB()) return -1;
|
||||
for(;;) {
|
||||
while(CUR_CHAR != ndelim) {
|
||||
switch(CUR_CHAR) {
|
||||
SQInteger x = CUR_CHAR;
|
||||
switch (x) {
|
||||
case SQUIRREL_EOB:
|
||||
Error(_SC("unfinished string"));
|
||||
return -1;
|
||||
@ -311,19 +371,29 @@ SQInteger SQLexer::ReadString(SQInteger ndelim,bool verbatim)
|
||||
else {
|
||||
NEXT();
|
||||
switch(CUR_CHAR) {
|
||||
case _SC('x'): NEXT(); {
|
||||
if(!isxdigit(CUR_CHAR)) Error(_SC("hexadecimal number expected"));
|
||||
const SQInteger maxdigits = 4;
|
||||
SQChar temp[maxdigits+1];
|
||||
SQInteger n = 0;
|
||||
while(isxdigit(CUR_CHAR) && n < maxdigits) {
|
||||
temp[n] = CUR_CHAR;
|
||||
n++;
|
||||
NEXT();
|
||||
case _SC('x'): {
|
||||
const SQInteger maxdigits = sizeof(SQChar) * 2;
|
||||
SQChar temp[maxdigits + 1];
|
||||
ProcessStringHexEscape(temp, maxdigits);
|
||||
SQChar *stemp;
|
||||
APPEND_CHAR((SQChar)scstrtoul(temp, &stemp, 16));
|
||||
}
|
||||
temp[n] = 0;
|
||||
SQChar *sTemp;
|
||||
APPEND_CHAR((SQChar)scstrtoul(temp,&sTemp,16));
|
||||
break;
|
||||
case _SC('U'):
|
||||
case _SC('u'): {
|
||||
const SQInteger maxdigits = x == 'u' ? 4 : 8;
|
||||
SQChar temp[8 + 1];
|
||||
ProcessStringHexEscape(temp, maxdigits);
|
||||
SQChar *stemp;
|
||||
#ifdef SQUNICODE
|
||||
#if WCHAR_SIZE == 2
|
||||
AddUTF16(scstrtoul(temp, &stemp, 16));
|
||||
#else
|
||||
ADD_CHAR((SQChar)scstrtoul(temp, &stemp, 16));
|
||||
#endif
|
||||
#else
|
||||
AddUTF8(scstrtoul(temp, &stemp, 16));
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
case _SC('t'): APPEND_CHAR(_SC('\t')); NEXT(); break;
|
||||
|
8
external/Squirrel/sqlexer.h
vendored
8
external/Squirrel/sqlexer.h
vendored
@ -24,6 +24,14 @@ private:
|
||||
void LexLineComment();
|
||||
SQInteger ReadID();
|
||||
void Next();
|
||||
#ifdef SQUNICODE
|
||||
#if WCHAR_SIZE == 2
|
||||
SQInteger AddUTF16(SQUnsignedInteger ch);
|
||||
#endif
|
||||
#else
|
||||
SQInteger AddUTF8(SQUnsignedInteger ch);
|
||||
#endif
|
||||
SQInteger ProcessStringHexEscape(SQChar *dest, SQInteger maxdigits);
|
||||
SQInteger _curtoken;
|
||||
SQTable *_keywords;
|
||||
SQBool _reached_eof;
|
||||
|
4
external/Squirrel/sqmem.cpp
vendored
4
external/Squirrel/sqmem.cpp
vendored
@ -5,7 +5,7 @@
|
||||
#ifndef SQ_EXCLUDE_DEFAULT_MEMFUNCTIONS
|
||||
void *sq_vm_malloc(SQUnsignedInteger size){ return malloc(size); }
|
||||
|
||||
void *sq_vm_realloc(void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size){ return realloc(p, size); }
|
||||
void *sq_vm_realloc(void *p, SQUnsignedInteger SQ_UNUSED_ARG(oldsize), SQUnsignedInteger size){ return realloc(p, size); }
|
||||
|
||||
void sq_vm_free(void *p, SQUnsignedInteger size){ free(p); }
|
||||
void sq_vm_free(void *p, SQUnsignedInteger SQ_UNUSED_ARG(size)){ free(p); }
|
||||
#endif
|
||||
|
2
external/Squirrel/sqopcodes.h
vendored
2
external/Squirrel/sqopcodes.h
vendored
@ -110,7 +110,7 @@ struct SQInstruction
|
||||
{
|
||||
SQInstruction(){};
|
||||
SQInstruction(SQOpcode _op,SQInteger a0=0,SQInteger a1=0,SQInteger a2=0,SQInteger a3=0)
|
||||
{ op = _op;
|
||||
{ op = (unsigned char)_op;
|
||||
_arg0 = (unsigned char)a0;_arg1 = (SQInt32)a1;
|
||||
_arg2 = (unsigned char)a2;_arg3 = (unsigned char)a3;
|
||||
}
|
||||
|
5
external/Squirrel/sqstate.cpp
vendored
5
external/Squirrel/sqstate.cpp
vendored
@ -81,7 +81,7 @@ bool CompileTypemask(SQIntVec &res,const SQChar *typemask)
|
||||
return true;
|
||||
}
|
||||
|
||||
SQTable *CreateDefaultDelegate(SQSharedState *ss,SQRegFunction *funcz)
|
||||
SQTable *CreateDefaultDelegate(SQSharedState *ss,const SQRegFunction *funcz)
|
||||
{
|
||||
SQInteger i=0;
|
||||
SQTable *t=SQTable::Create(ss,0);
|
||||
@ -250,8 +250,7 @@ void SQSharedState::MarkObject(SQObjectPtr &o,SQCollectable **chain)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SQSharedState::RunMark(SQVM *vm,SQCollectable **tchain)
|
||||
void SQSharedState::RunMark(SQVM SQ_UNUSED_ARG(*vm),SQCollectable **tchain)
|
||||
{
|
||||
SQVM *vms = _thread(_root_vm);
|
||||
|
||||
|
20
external/Squirrel/sqstate.h
vendored
20
external/Squirrel/sqstate.h
vendored
@ -84,25 +84,25 @@ public:
|
||||
#endif
|
||||
SQObjectPtr _root_vm;
|
||||
SQObjectPtr _table_default_delegate;
|
||||
static SQRegFunction _table_default_delegate_funcz[];
|
||||
static const SQRegFunction _table_default_delegate_funcz[];
|
||||
SQObjectPtr _array_default_delegate;
|
||||
static SQRegFunction _array_default_delegate_funcz[];
|
||||
static const SQRegFunction _array_default_delegate_funcz[];
|
||||
SQObjectPtr _string_default_delegate;
|
||||
static SQRegFunction _string_default_delegate_funcz[];
|
||||
static const SQRegFunction _string_default_delegate_funcz[];
|
||||
SQObjectPtr _number_default_delegate;
|
||||
static SQRegFunction _number_default_delegate_funcz[];
|
||||
static const SQRegFunction _number_default_delegate_funcz[];
|
||||
SQObjectPtr _generator_default_delegate;
|
||||
static SQRegFunction _generator_default_delegate_funcz[];
|
||||
static const SQRegFunction _generator_default_delegate_funcz[];
|
||||
SQObjectPtr _closure_default_delegate;
|
||||
static SQRegFunction _closure_default_delegate_funcz[];
|
||||
static const SQRegFunction _closure_default_delegate_funcz[];
|
||||
SQObjectPtr _thread_default_delegate;
|
||||
static SQRegFunction _thread_default_delegate_funcz[];
|
||||
static const SQRegFunction _thread_default_delegate_funcz[];
|
||||
SQObjectPtr _class_default_delegate;
|
||||
static SQRegFunction _class_default_delegate_funcz[];
|
||||
static const SQRegFunction _class_default_delegate_funcz[];
|
||||
SQObjectPtr _instance_default_delegate;
|
||||
static SQRegFunction _instance_default_delegate_funcz[];
|
||||
static const SQRegFunction _instance_default_delegate_funcz[];
|
||||
SQObjectPtr _weakref_default_delegate;
|
||||
static SQRegFunction _weakref_default_delegate_funcz[];
|
||||
static const SQRegFunction _weakref_default_delegate_funcz[];
|
||||
|
||||
SQCOMPILERERROR _compilererrorhandler;
|
||||
SQPRINTFUNCTION _printfunc;
|
||||
|
16
external/Squirrel/sqvm.cpp
vendored
16
external/Squirrel/sqvm.cpp
vendored
@ -495,12 +495,12 @@ bool SQVM::DerefInc(SQInteger op,SQObjectPtr &target, SQObjectPtr &self, SQObjec
|
||||
}
|
||||
|
||||
#define arg0 (_i_._arg0)
|
||||
#define sarg0 ((SQInteger)*((signed char *)&_i_._arg0))
|
||||
#define sarg0 ((SQInteger)*((const signed char *)&_i_._arg0))
|
||||
#define arg1 (_i_._arg1)
|
||||
#define sarg1 (*((SQInt32 *)&_i_._arg1))
|
||||
#define sarg1 (*((const SQInt32 *)&_i_._arg1))
|
||||
#define arg2 (_i_._arg2)
|
||||
#define arg3 (_i_._arg3)
|
||||
#define sarg3 ((SQInteger)*((signed char *)&_i_._arg3))
|
||||
#define sarg3 ((SQInteger)*((const signed char *)&_i_._arg3))
|
||||
|
||||
SQRESULT SQVM::Suspend()
|
||||
{
|
||||
@ -514,7 +514,7 @@ SQRESULT SQVM::Suspend()
|
||||
|
||||
#define _FINISH(howmuchtojump) {jump = howmuchtojump; return true; }
|
||||
bool SQVM::FOREACH_OP(SQObjectPtr &o1,SQObjectPtr &o2,SQObjectPtr
|
||||
&o3,SQObjectPtr &o4,SQInteger arg_2,int exitpos,int &jump)
|
||||
&o3,SQObjectPtr &o4,SQInteger SQ_UNUSED_ARG(arg_2),int exitpos,int &jump)
|
||||
{
|
||||
SQInteger nrefidx;
|
||||
switch(type(o1)) {
|
||||
@ -717,7 +717,7 @@ exception_restore:
|
||||
#else
|
||||
TARGET = (SQInteger)((SQInt32)arg1); continue;
|
||||
#endif
|
||||
case _OP_LOADFLOAT: TARGET = *((SQFloat *)&arg1); continue;
|
||||
case _OP_LOADFLOAT: TARGET = *((const SQFloat *)&arg1); continue;
|
||||
case _OP_DLOAD: TARGET = ci->_literals[arg1]; STK(arg2) = ci->_literals[arg3];continue;
|
||||
case _OP_TAILCALL:{
|
||||
SQObjectPtr &t = STK(arg1);
|
||||
@ -914,7 +914,7 @@ exception_restore:
|
||||
break;
|
||||
case AAT_FLOAT:
|
||||
val._type = OT_FLOAT;
|
||||
val._unVal.fFloat = *((SQFloat *)&arg1);
|
||||
val._unVal.fFloat = *((const SQFloat *)&arg1);
|
||||
break;
|
||||
case AAT_BOOL:
|
||||
val._type = OT_BOOL;
|
||||
@ -1516,7 +1516,7 @@ bool SQVM::DeleteSlot(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr
|
||||
_table(self)->Remove(key);
|
||||
}
|
||||
else {
|
||||
Raise_IdxError((SQObject &)key);
|
||||
Raise_IdxError((const SQObject &)key);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -1573,7 +1573,7 @@ SQInteger prevstackbase = _stackbase;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SQVM::CallMetaMethod(SQObjectPtr &closure,SQMetaMethod mm,SQInteger nparams,SQObjectPtr &outres)
|
||||
bool SQVM::CallMetaMethod(SQObjectPtr &closure,SQMetaMethod SQ_UNUSED_ARG(mm),SQInteger nparams,SQObjectPtr &outres)
|
||||
{
|
||||
//SQObjectPtr closure;
|
||||
|
||||
|
@ -394,6 +394,12 @@ SQUIRREL_API void sq_setnativedebughook(HSQUIRRELVM v,SQDEBUGHOOK hook);
|
||||
#define SQ_FAILED(res) (res<0)
|
||||
#define SQ_SUCCEEDED(res) (res>=0)
|
||||
|
||||
#ifdef __GNUC__
|
||||
# define SQ_UNUSED_ARG(x) __attribute__((unused)) x
|
||||
#else
|
||||
# define SQ_UNUSED_ARG(x) x
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /*extern "C"*/
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user