X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2Finclude%2Fspiderscript.h;h=e1dc5078e27d9349da99a3ca7dcbf7589e15392d;hb=fd0665670cfe2c7b1e374e91b87113ae74e37651;hp=9d15d35fba04f7e693102ff4d2e814d2c9733f3c;hpb=7edeed66f89e03c3de5fb7d3b41421f23c1bc4f7;p=tpg%2Facess2.git diff --git a/Usermode/include/spiderscript.h b/Usermode/include/spiderscript.h index 9d15d35f..e1dc5078 100644 --- a/Usermode/include/spiderscript.h +++ b/Usermode/include/spiderscript.h @@ -13,10 +13,8 @@ */ typedef struct sSpiderScript tSpiderScript; -/** - * \brief Variant type - */ typedef struct sSpiderVariant tSpiderVariant; +typedef struct sSpiderNamespace tSpiderNamespace; typedef struct sSpiderFunction tSpiderFunction; typedef struct sSpiderValue tSpiderValue; typedef struct sSpiderObjectDef tSpiderObjectDef; @@ -25,20 +23,88 @@ typedef struct sSpiderObject tSpiderObject; /** * \brief SpiderScript Variable Datatypes + * \todo Expand the descriptions */ enum eSpiderScript_DataTypes { - SS_DATATYPE_UNDEF, //!< Undefined - SS_DATATYPE_NULL, //!< NULL (Probably will never be used) - SS_DATATYPE_DYNAMIC, //!< Dynamically typed variable (will this be used?) - SS_DATATYPE_OBJECT, //!< Opaque object reference - SS_DATATYPE_ARRAY, //!< Array - SS_DATATYPE_INTEGER, //!< Integer (64-bits) + /** + * \brief Undefined data + * \note Default type of an undefined dynamic variable + */ + SS_DATATYPE_UNDEF, + /** + * \brief Dynamically typed variable + * \note Used to dentote a non-fixed type for function parameters + */ + SS_DATATYPE_DYNAMIC, + /** + * \brief Opaque Data Pointer + * + * Opaque data types are used for resource handles or for system buffers. + */ + SS_DATATYPE_OPAQUE, + /** + * \brief Object reference + * + * A reference to a SpiderScript class instance. Can be accessed + * using the -> operator. + */ + SS_DATATYPE_OBJECT, + /** + * \brief Array data type + */ + SS_DATATYPE_ARRAY, + /** + * \brief Integer datatype + * + * 64-bit integer + */ + SS_DATATYPE_INTEGER, SS_DATATYPE_REAL, //!< Real Number (double) SS_DATATYPE_STRING, //!< String NUM_SS_DATATYPES }; +enum eSpiderValueOps +{ + SS_VALUEOP_NOP, + + SS_VALUEOP_ADD, + SS_VALUEOP_SUBTRACT, + SS_VALUEOP_NEGATE, + SS_VALUEOP_MULIPLY, + SS_VALUEOP_DIVIDE, + SS_VALUEOP_MODULO, + + SS_VALUEOP_BITNOT, + SS_VALUEOP_BITAND, + SS_VALUEOP_BITOR, + SS_VALUEOP_BITXOR, + + SS_VALUEOP_SHIFTLEFT, + SS_VALUEOP_SHIFTRIGHT, + SS_VALUEOP_ROTATELEFT +}; + +/** + * \brief Namespace definition + */ +struct sSpiderNamespace +{ + tSpiderNamespace *Next; + + tSpiderNamespace *FirstChild; + + tSpiderFunction *Functions; + + tSpiderObjectDef *Classes; + + int NConstants; //!< Number of constants + tSpiderValue *Constants; //!< Number of constants + + const char Name[]; +}; + /** * \brief Variant of SpiderScript */ @@ -46,13 +112,15 @@ struct sSpiderVariant { const char *Name; // Just for debug - int bDyamicTyped; + int bDyamicTyped; //!< Use dynamic typing + int bImplicitCasts; //!< Allow implicit casts (casts to lefthand side) - int NFunctions; //!< Number of functions - tSpiderFunction *Functions; //!< Functions + tSpiderFunction *Functions; //!< Functions (Linked List) int NConstants; //!< Number of constants tSpiderValue *Constants; //!< Number of constants + + tSpiderNamespace RootNamespace; }; /** @@ -60,11 +128,11 @@ struct sSpiderVariant */ struct sSpiderValue { - int Type; //!< Variable type + enum eSpiderScript_DataTypes Type; //!< Variable type int ReferenceCount; //!< Reference count union { - uint64_t Integer; //!< Integer data + int64_t Integer; //!< Integer data double Real; //!< Real Number data /** * \brief String data @@ -81,6 +149,17 @@ struct sSpiderValue tSpiderValue *Items[]; //!< Array elements (\a Length long) } Array; + /** + * \brief Opaque data + */ + struct { + void *Data; //!< Data (can be anywhere) + void (*Destroy)(void *Data); //!< Called on GC + } Opaque; + + /** + * \brief Object Instance + */ tSpiderObject *Object; }; }; @@ -92,15 +171,22 @@ struct sSpiderValue */ struct sSpiderObjectDef { + /** + */ + struct sSpiderObjectDef *Next; //!< Internal linked list + /** + * \brief Object type name + */ + const char * const Name; /** * \brief Construct an instance of the object * \param NArgs Number of arguments - * \param Args Argument count + * \param Args Argument array * \return Pointer to an object instance (which must be fully valid) * \retval NULL Invalid parameter (usually, actually just a NULL value) * \retval ERRPTR Invalid parameter count */ - tSpiderObject *(*Constructor)(int NArgs, tSpiderValue *Args); + tSpiderObject *(*Constructor)(int NArgs, tSpiderValue **Args); /** * \brief Clean up and destroy the object @@ -110,17 +196,15 @@ struct sSpiderObjectDef */ void (*Destructor)(tSpiderObject *This); + tSpiderFunction *Methods; //!< Method Definitions (linked list) + int NAttributes; //!< Number of attributes //! Attribute definitions struct { const char *Name; //!< Attribute Name int bReadOnly; //!< Allow writes to the attribute? - } *AttributeDefs; - - - int NMethods; //!< Number of methods - tSpiderFunction *Methods; //!< Method Definitions + } AttributeDefs[]; }; /** @@ -129,7 +213,7 @@ struct sSpiderObjectDef struct sSpiderObject { tSpiderObjectDef *Type; //!< Object Type - int NReferences; //!< Number of references + int ReferenceCount; //!< Number of references void *OpaqueData; //!< Pointer to the end of the \a Attributes array tSpiderValue *Attributes[]; //!< Attribute Array }; @@ -139,6 +223,11 @@ struct sSpiderObject */ struct sSpiderFunction { + /** + * \brief Next function in list + */ + struct sSpiderFunction *Next; + /** * \brief Function name */ @@ -155,7 +244,7 @@ struct sSpiderFunction * parameters, if the final entry is -1, the function has a variable * number of arguments. */ - int *ArgTypes; // Zero (or -1) terminated array of parameter types + int ArgTypes[]; // Zero (or -1) terminated array of parameter types }; @@ -168,19 +257,65 @@ struct sSpiderFunction */ extern tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename); /** - * \brief Execute a method from a script + * \brief Execute a function from a script * \param Script Script to run * \param Function Name of function to run ("" for the 'main') * \return Return value */ +extern tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script, + tSpiderNamespace *Namespace, const char *Function, + int NArguments, tSpiderValue **Arguments + ); +/** + * \brief Execute an object method + */ extern tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script, - const char *Function, + tSpiderObject *Object, const char *MethodName, int NArguments, tSpiderValue **Arguments ); +/** + * \brief Creates an object instance + */ +extern tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script, + tSpiderNamespace *Namespace, const char *ClassName, + int NArguments, tSpiderValue **Arguments + ); + +/** + * \brief Convert a script to bytecode and save to a file + */ +extern int SpiderScript_SaveBytecode(tSpiderScript *Script, const char *DestFile); +/** + * \brief Save the AST of a script to a file + */ +extern int SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename); /** * \brief Free a script + * \param Script Script structure to free */ extern void SpiderScript_Free(tSpiderScript *Script); +extern tSpiderObject *SpiderScript_AllocateObject(tSpiderObjectDef *Class, int ExtraBytes); + +/** + * \name tSpiderValue Manipulation functions + * \{ + */ +extern void SpiderScript_DereferenceValue(tSpiderValue *Object); +extern void SpiderScript_ReferenceValue(tSpiderValue *Object); +extern tSpiderValue *SpiderScript_CreateInteger(uint64_t Value); +extern tSpiderValue *SpiderScript_CreateReal(double Value); +extern tSpiderValue *SpiderScript_CreateString(int Length, const char *Data); +extern tSpiderValue *SpiderScript_StringConcat(const tSpiderValue *Str1, const tSpiderValue *Str2); +extern tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source); +extern int SpiderScript_IsValueTrue(tSpiderValue *Value); +extern void SpiderScript_FreeValue(tSpiderValue *Value); +extern char *SpiderScript_DumpValue(tSpiderValue *Value); + +extern tSpiderValue *SpiderScript_DoOp(tSpiderValue *Left, enum eSpiderValueOps Op, int bCanCast, tSpiderValue *Right); +/** + * \} + */ + #endif