return ret;
}
-#if 0
+#if 1
+
+#define WRITE_N(_buffer, _offset, _len, _dataptr) do { \
+ if(_buffer) memcpy((char*)_buffer + _offset, _dataptr, _len);\
+ _offset += _len; \
+} while(0)
+
+#define WRITE_8(_buffer, _offset, _val) do {\
+ uint8_t v = (_val);\
+ WRITE_N(_buffer, _offset, 1, &v);\
+} while(0)
+#define WRITE_16(_buffer, _offset, _val) do {\
+ uint16_t v = (_val);\
+ WRITE_N(_buffer, _offset, 2, &v);\
+} while(0)
+#define WRITE_32(_buffer, _offset, _val) do {\
+ uint32_t v = (_val);\
+ WRITE_N(_buffer, _offset, 4, &v);\
+} while(0)
+#define WRITE_64(_buffer, _offset, _val) do {\
+ uint64_t v = (_val);\
+ WRITE_N(_buffer, _offset, 8, &v);\
+} while(0)
+#define WRITE_REAL(_buffer, _offset, _val) do {\
+ double v = (_val);\
+ WRITE_N(_buffer, _offset, sizeof(double), &v);\
+} while(0)
+
+#define WRITE_STR(_buffer, _offset, _string) do {\
+ int len = strlen(_string);\
+ WRITE_16(_buffer, _offset, len);\
+ WRITE_N(_buffer, _offset, len, _string);\
+} while(0)
+#define WRITE_NODELIST(_buffer, _offset, _listHead) do {\
+ tAST_Node *node; \
+ size_t ptr = -1;\
+ for(node=(_listHead); node; node = node->NextSibling) {\
+ ptr = _offset;\
+ _offset += AST_WriteNode(_buffer, _offset, node); \
+ WRITE_32(_buffer, ptr, ptr); \
+ } \
+ if(ptr != -1){ptr -= 4; WRITE_32(_buffer, ptr, 0);} \
+} while(0)
+
+/**
+ * \brief Writes a script dump to a buffer
+ * \return Size of encoded data
+ * \note If \a Buffer is NULL, no write is done, but the size is still returned
+ */
+size_t AST_WriteScript(void *Buffer, tAST_Script *Script)
+{
+ tAST_Function *fcn;
+ size_t ret = 0, ptr;
+
+ for( fcn = Script->Functions; fcn; fcn = fcn->Next )
+ {
+ ptr = ret;
+ WRITE_32(Buffer, ret, 0); // Next
+ WRITE_STR(Buffer, ret, fcn->Name);
+ WRITE_NODELIST(Buffer, ret, fcn->Arguments); // TODO: Cheaper way
+ ret += AST_WriteNode(Buffer, ret, fcn->Code);
+ WRITE_32(Buffer, ptr, ret); // Actually set next
+ }
+ ptr -= 4;
+ WRITE_32(Buffer, ptr, 0); // Clear next for final
+
+ return ret;
+}
+
+/**
+ * \brief Write a node to a file
+ */
+size_t AST_WriteNode(void *Buffer, size_t Offset, tAST_Node *Node)
+{
+ off_t ptr;
+ typeof(Offset) baseOfs = Offset;
+
+ if(!Node) {
+ fprintf(stderr, "Possible Bug - NULL passed to AST_WriteNode\n");
+ return 0;
+ }
+
+ WRITE_32(Buffer, Offset, 0); // Next
+ WRITE_8(Buffer, Offset, Node->Type);
+ //WRITE_32(Buffer, Offset, 0); // File
+ WRITE_16(Buffer, Offset, Node->Line); // Line
+
+ switch(Node->Type)
+ {
+ // Block of code
+ case NODETYPE_BLOCK:
+ WRITE_NODELIST(Buffer, Offset, Node->Block.FirstChild);
+ break;
+
+ // Function Call
+ case NODETYPE_FUNCTIONCALL:
+ WRITE_STR(Buffer, Offset, Node->FunctionCall.Name);
+ WRITE_NODELIST(Buffer, Offset, Node->FunctionCall.FirstArg);
+ break;
+
+ // If node
+ case NODETYPE_IF:
+ ptr = Offset;
+ WRITE_32(Buffer, Offset, 0); // Condition
+ WRITE_32(Buffer, Offset, 0); // True
+ WRITE_32(Buffer, Offset, 0); // False
+
+ Offset += AST_WriteNode(Buffer, Offset, Node->If.Condition);
+ WRITE_32(Buffer, ptr, Offset);
+ Offset += AST_WriteNode(Buffer, Offset, Node->If.True);
+ WRITE_32(Buffer, ptr, Offset);
+ Offset += AST_WriteNode(Buffer, Offset, Node->If.False);
+ WRITE_32(Buffer, ptr, Offset);
+ break;
+
+ // Looping Construct (For loop node)
+ case NODETYPE_LOOP:
+ WRITE_8(Buffer, Offset, Node->For.bCheckAfter);
+ ptr = Offset;
+ WRITE_32(Buffer, Offset, 0); // Init
+ WRITE_32(Buffer, Offset, 0); // Condition
+ WRITE_32(Buffer, Offset, 0); // Increment
+ WRITE_32(Buffer, Offset, 0); // Code
+
+ Offset += AST_WriteNode(Buffer, Offset, Node->For.Init);
+ WRITE_32(Buffer, ptr, Offset);
+ Offset += AST_WriteNode(Buffer, Offset, Node->For.Condition);
+ WRITE_32(Buffer, ptr, Offset);
+ Offset += AST_WriteNode(Buffer, Offset, Node->For.Increment);
+ WRITE_32(Buffer, ptr, Offset);
+ Offset += AST_WriteNode(Buffer, Offset, Node->For.Code);
+ WRITE_32(Buffer, ptr, Offset);
+ break;
+
+ // Asignment
+ case NODETYPE_ASSIGN:
+ WRITE_8(Buffer, Offset, Node->Assign.Operation);
+ ptr = Offset;
+ WRITE_32(Buffer, Offset, 0); // Dest
+ WRITE_32(Buffer, Offset, 0); // Value
+
+ Offset += AST_WriteNode(Buffer, Offset, Node->Assign.Dest);
+ WRITE_32(Buffer, ptr, Offset);
+ Offset += AST_WriteNode(Buffer, Offset, Node->Assign.Value);
+ WRITE_32(Buffer, ptr, Offset);
+ break;
+
+ // Casting
+ case NODETYPE_CAST:
+ WRITE_8(Buffer, Offset, Node->Cast.DataType);
+ ptr = Offset;
+ WRITE_32(Buffer, Offset, 0);
+
+ Offset += AST_WriteNode(Buffer, Offset, Node->Cast.Value);
+ WRITE_32(Buffer, ptr, Offset);
+ break;
+
+ // Define a variable
+ case NODETYPE_DEFVAR:
+ WRITE_8(Buffer, Offset, Node->DefVar.DataType);
+ WRITE_8(Buffer, Offset, Node->DefVar.Depth);
+ WRITE_STR(Buffer, Offset, Node->DefVar.Name);
+
+ WRITE_NODELIST(Buffer, Offset, Node->DefVar.LevelSizes);
+ break;
+
+ // Unary Operations
+ case NODETYPE_RETURN:
+ ptr = Offset;
+ WRITE_32(Buffer, Offset, 0);
+ Offset += AST_WriteNode(Buffer, Offset, Node->UniOp.Value);
+ WRITE_32(Buffer, ptr, Offset);
+ break;
+
+ // Binary Operations
+ case NODETYPE_INDEX:
+ case NODETYPE_ADD:
+ case NODETYPE_SUBTRACT:
+ case NODETYPE_MULTIPLY:
+ case NODETYPE_DIVIDE:
+ case NODETYPE_MODULO:
+ case NODETYPE_BITSHIFTLEFT:
+ case NODETYPE_BITSHIFTRIGHT:
+ case NODETYPE_BITROTATELEFT:
+ case NODETYPE_BWAND: case NODETYPE_LOGICALAND:
+ case NODETYPE_BWOR: case NODETYPE_LOGICALOR:
+ case NODETYPE_BWXOR: case NODETYPE_LOGICALXOR:
+ case NODETYPE_EQUALS:
+ case NODETYPE_LESSTHAN:
+ case NODETYPE_GREATERTHAN:
+ ptr = Offset;
+ WRITE_32(Buffer, Offset, 0); // Left
+ WRITE_32(Buffer, Offset, 0); // Right
+ Offset += AST_WriteNode(Buffer, Offset, Node->BinOp.Left);
+ WRITE_32(Buffer, ptr, Offset);
+ Offset += AST_WriteNode(Buffer, Offset, Node->BinOp.Right);
+ WRITE_32(Buffer, ptr, Offset);
+ break;
+
+ // Node types with no children
+ case NODETYPE_NOP:
+ break;
+ case NODETYPE_VARIABLE:
+ case NODETYPE_CONSTANT:
+ WRITE_STR(Buffer, Offset, Node->Variable.Name);
+ break;
+ case NODETYPE_STRING:
+ WRITE_32(Buffer, Offset, Node->String.Length);
+ WRITE_N(Buffer, Offset, Node->String.Length, Node->String.Data);
+ break;
+ case NODETYPE_INTEGER:
+ WRITE_64(Buffer, Offset, Node->Integer);
+ break;
+ case NODETYPE_REAL:
+ WRITE_REAL(Buffer, Offset, Node->Real);
+ break;
+
+ default:
+ fprintf(stderr, "AST_WriteNode: Unknown node type %i\n", Node->Type);
+ break;
+ }
+
+ return Offset - baseOfs;
+}
+#elif 0
/**
* \brief Write a node to a file
*/
*/
#include <stdlib.h>
#include <stdio.h>
+#include <stdarg.h>
#include <string.h>
#include "ast.h"
tSpiderValue *SpiderScript_CreateString(int Length, const char *Data);
tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source);
int SpiderScript_IsValueTrue(tSpiderValue *Value);
+void SpiderScript_FreeValue(tSpiderValue *Value);
char *SpiderScript_DumpValue(tSpiderValue *Value);
tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node);
+tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpiderValue *Left, tSpiderValue *Right);
tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name);
int Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value);
tSpiderValue *Variable_GetValue(tAST_BlockState *Block, const char *Name);
void Variable_Destroy(tAST_Variable *Variable);
+void AST_RuntimeError(tAST_Node *Node, const char *Format, ...);
+
// === CODE ===
/**
* \brief Dereference a created object
if(!Object) return ;
if(Object == ERRPTR) return ;
Object->ReferenceCount --;
+// printf("%p Dereferenced (%i)\n", Object, Object->ReferenceCount);
if( Object->ReferenceCount == 0 ) {
switch( (enum eSpiderScript_DataTypes) Object->Type )
{
{
if(!Object) return ;
Object->ReferenceCount ++;
+// printf("%p Referenced (%i)\n", Object, Object->ReferenceCount);
}
/**
case SS_DATATYPE_UNDEF:
case SS_DATATYPE_ARRAY:
case SS_DATATYPE_OPAQUE:
- fprintf(stderr, "SpiderScript_CastValueTo - Invalid cast to %i\n", Type);
+ AST_RuntimeError(NULL, "Invalid cast to %i", Type);
return ERRPTR;
case SS_DATATYPE_INTEGER:
case SS_DATATYPE_STRING: ret->Integer = atoi(Source->String.Data); break;
case SS_DATATYPE_REAL: ret->Integer = Source->Real; break;
default:
- fprintf(stderr, "SpiderScript_CastValueTo - Invalid cast from %i\n", Source->Type);
+ AST_RuntimeError(NULL, "Invalid cast from %i to Integer", Source->Type);
free(ret);
ret = ERRPTR;
break;
case SS_DATATYPE_INTEGER: sprintf(ret->String.Data, "%li", Source->Integer); break;
case SS_DATATYPE_REAL: sprintf(ret->String.Data, "%f", Source->Real); break;
default:
- fprintf(stderr, "SpiderScript_CastValueTo - Invalid cast from %i\n", Source->Type);
+ AST_RuntimeError(NULL, "Invalid cast from %i to String", Source->Type);
free(ret);
ret = ERRPTR;
break;
break;
default:
- fprintf(stderr, "BUG REPORT: Unimplemented cast target\n");
+ AST_RuntimeError(NULL, "BUG - BUG REPORT: Unimplemented cast target");
break;
}
case SS_DATATYPE_ARRAY:
return Value->Array.Length > 0;
default:
- fprintf(stderr, "Spiderscript internal error: Unknown type %i in SpiderScript_IsValueTrue\n", Value->Type);
+ AST_RuntimeError(NULL, "Unknown type %i in SpiderScript_IsValueTrue", Value->Type);
return 0;
}
return 0;
}
+/**
+ * \brief Free a value
+ * \note Just calls Object_Dereference
+ */
+void SpiderScript_FreeValue(tSpiderValue *Value)
+{
+ Object_Dereference(Value);
+}
+
/**
* \brief Dump a value into a string
* \return Heap string
return strdup("Array");
default:
- fprintf(stderr, "Spiderscript internal error: Unknown type %i in Object_Dump\n", Value->Type);
+ AST_RuntimeError(NULL, "Unknown type %i in Object_Dump", Value->Type);
return NULL;
}
break ;
}
if(tmpobj) Object_Dereference(tmpobj); // Free unused value
+ tmpobj = NULL;
}
// Clean up variables
while(blockInfo.FirstVar)
// Assignment
case NODETYPE_ASSIGN:
if( Node->Assign.Dest->Type != NODETYPE_VARIABLE ) {
- fprintf(stderr, "Syntax error: LVALUE of assignment is not a variable\n");
+ AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
return ERRPTR;
}
ret = AST_ExecuteNode(Block, Node->Assign.Value);
- if(ret != ERRPTR)
+ if(ret == ERRPTR)
+ return ERRPTR;
+
+ if( Node->Assign.Operation != NODETYPE_NOP )
{
- if( Variable_SetValue( Block, Node->Assign.Dest->Variable.Name, ret ) ) {
- Object_Dereference( ret );
- fprintf(stderr, "on line %i\n", Node->Line);
+ tSpiderValue *varVal = Variable_GetValue(Block, Node->Assign.Dest->Variable.Name);
+ tSpiderValue *value;
+ value = AST_ExecuteNode_BinOp(Block, Node->Assign.Operation, varVal, ret);
+ if( value == ERRPTR )
return ERRPTR;
- }
+ if(ret) Object_Dereference(ret);
+ Object_Dereference(varVal);
+ ret = value;
+ }
+
+ if( Variable_SetValue( Block, Node->Assign.Dest->Variable.Name, ret ) ) {
+ Object_Dereference( ret );
+ return ERRPTR;
}
break;
case NODETYPE_IF:
ret = AST_ExecuteNode(Block, Node->If.Condition);
if( SpiderScript_IsValueTrue(ret) ) {
- AST_ExecuteNode(Block, Node->If.True);
+ Object_Dereference(AST_ExecuteNode(Block, Node->If.True));
}
else {
- AST_ExecuteNode(Block, Node->If.False);
+ Object_Dereference(AST_ExecuteNode(Block, Node->If.False));
}
Object_Dereference(ret);
+ ret = NULL;
break;
// Loop
case NODETYPE_LOOP:
ret = AST_ExecuteNode(Block, Node->For.Init);
- if( Node->For.bCheckAfter ) {
+ if( Node->For.bCheckAfter )
+ {
do {
Object_Dereference(ret);
ret = AST_ExecuteNode(Block, Node->For.Code);
Object_Dereference(ret);
+ ret = AST_ExecuteNode(Block, Node->For.Increment);
+ Object_Dereference(ret);
ret = AST_ExecuteNode(Block, Node->For.Condition);
} while( SpiderScript_IsValueTrue(ret) );
}
- else {
+ else
+ {
Object_Dereference(ret);
ret = AST_ExecuteNode(Block, Node->For.Condition);
while( SpiderScript_IsValueTrue(ret) ) {
Object_Dereference(ret);
ret = AST_ExecuteNode(Block, Node->For.Code);
Object_Dereference(ret);
+ ret = AST_ExecuteNode(Block, Node->For.Increment);
+ Object_Dereference(ret);
ret = AST_ExecuteNode(Block, Node->For.Condition);
}
- Object_Dereference(ret);
}
+ Object_Dereference(ret);
+ ret = NULL;
break;
// Return
case NODETYPE_RETURN:
ret = AST_ExecuteNode(Block, Node->UniOp.Value);
Block->RetVal = ret; // Return value set
+ //Object_Reference(ret); // Make sure it exists after return
+ ret = NULL; // the `return` statement does not return a value
break;
// Define a variable
// Cast a value to another
case NODETYPE_CAST:
- ret = SpiderScript_CastValueTo(
- Node->Cast.DataType,
- AST_ExecuteNode(Block, Node->Cast.Value)
- );
+ {
+ tSpiderValue *tmp = AST_ExecuteNode(Block, Node->Cast.Value);
+ ret = SpiderScript_CastValueTo( Node->Cast.DataType, tmp );
+ Object_Dereference(tmp);
+ }
break;
// Index into an array
case NODETYPE_INDEX:
- fprintf(stderr, "TODO: Array indexing\n");
+ AST_RuntimeError(Node, "TODO - Array Indexing");
ret = ERRPTR;
break;
// TODO: Implement runtime constants
case NODETYPE_CONSTANT:
- fprintf(stderr, "TODO: Runtime Constants\n");
+ AST_RuntimeError(Node, "TODO - Runtime Constants");
ret = ERRPTR;
break;
// Constant Values
}
// If statically typed, this should never happen, but catch it anyway
else {
- fprintf(stderr, "PARSER ERROR: Statically typed implicit cast (line %i)\n",
- Node->Line);
+ AST_RuntimeError(Node, "Statically typed implicit cast");
ret = ERRPTR;
break;
}
cmp = -1;
}
break;
+
+ // - Integer Comparisons
+ case SS_DATATYPE_INTEGER:
+ if( op1->Integer == op2->Integer )
+ cmp = 0;
+ else if( op1->Integer < op2->Integer )
+ cmp = -1;
+ else
+ cmp = 1;
+ break;
default:
- fprintf(stderr, "SpiderScript internal error: TODO: Comparison of type %i\n", op1->Type);
+ AST_RuntimeError(Node, "TODO - Comparison of type %i", op1->Type);
ret = ERRPTR;
break;
}
case NODETYPE_LESSTHAN: ret = SpiderScript_CreateInteger(cmp < 0); break;
case NODETYPE_GREATERTHAN: ret = SpiderScript_CreateInteger(cmp > 0); break;
default:
- fprintf(stderr, "SpiderScript internal error: Exec,CmpOp unknown op %i", Node->Type);
+ AST_RuntimeError(Node, "Exec,CmpOp unknown op %i", Node->Type);
ret = ERRPTR;
break;
}
return ERRPTR;
}
- // Convert types
- if( op1 && op2 && op1->Type != op2->Type ) {
- // If dynamically typed, convert op2 to op1's type
- if(Block->Script->Variant->bDyamicTyped)
+ ret = AST_ExecuteNode_BinOp(Block, Node->Type, op1, op2);
+
+ // Free intermediate objects
+ Object_Dereference(op1);
+ Object_Dereference(op2);
+ break;
+
+ //default:
+ // ret = NULL;
+ // AST_RuntimeError(Node, "BUG - SpiderScript AST_ExecuteNode Unimplemented %i\n", Node->Type);
+ // break;
+ }
+_return:
+ return ret;
+}
+
+tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpiderValue *Left, tSpiderValue *Right)
+{
+ tSpiderValue *preCastValue = Right;
+ tSpiderValue *ret;
+
+ // Convert types
+ if( Left && Right && Left->Type != Right->Type )
+ {
+ #if 0
+ // Object types
+ // - Operator overload functions
+ if( Left->Type == SS_DATATYPE_OBJECT )
+ {
+ const char *fcnname;
+ switch(Operation)
{
- tmpobj = op2;
- op2 = SpiderScript_CastValueTo(op1->Type, op2);
- Object_Dereference(tmpobj);
- if(op2 == ERRPTR) {
- Object_Dereference(op1);
- return ERRPTR;
- }
+ case NODETYPE_ADD: fcnname = "+"; break;
+ case NODETYPE_SUBTRACT: fcnname = "-"; break;
+ case NODETYPE_MULTIPLY: fcnname = "*"; break;
+ case NODETYPE_DIVIDE: fcnname = "/"; break;
+ case NODETYPE_MODULO: fcnname = "%"; break;
+ case NODETYPE_BWAND: fcnname = "&"; break;
+ case NODETYPE_BWOR: fcnname = "|"; break;
+ case NODETYPE_BWXOR: fcnname = "^"; break;
+ case NODETYPE_BITSHIFTLEFT: fcnname = "<<"; break;
+ case NODETYPE_BITSHIFTRIGHT:fcnname = ">>"; break;
+ case NODETYPE_BITROTATELEFT:fcnname = "<<<"; break;
+ default: fcnname = NULL; break;
}
- // If statically typed, this should never happen, but catch it anyway
- else {
- fprintf(stderr,
- "PARSER ERROR: Statically typed implicit cast (from %i to %i)\n",
- op2->Type, op1->Type
- );
- ret = ERRPTR;
- break;
+
+ if( fcnname )
+ {
+ ret = Object_ExecuteMethod(Left->Object, fcnname, Right);
+ if( ret != ERRPTR )
+ return ret;
+ // Fall through and try casting (which will usually fail)
}
}
+ #endif
- // NULL Check
- if( op1 == NULL || op2 == NULL ) {
- ret = NULL;
- break;
+ // If implicit casts are allowed, convert Right to Left's type
+ if(Block->Script->Variant->bImplicitCasts)
+ {
+ Right = SpiderScript_CastValueTo(Left->Type, Right);
+ if(Right == ERRPTR)
+ return ERRPTR;
}
-
- // Do operation
- switch(op1->Type)
+ // If statically typed, this should never happen, but catch it anyway
+ else {
+ AST_RuntimeError(NULL, "Implicit cast not allowed (from %i to %i)\n", Right->Type, Left->Type);
+ return ERRPTR;
+ }
+ }
+
+ // NULL Check
+ if( Left == NULL || Right == NULL ) {
+ if(Right && Right != preCastValue) free(Right);
+ return NULL;
+ }
+
+ // Do operation
+ switch(Left->Type)
+ {
+ // String Concatenation
+ case SS_DATATYPE_STRING:
+ switch(Operation)
{
- // String Concatenation
- case SS_DATATYPE_STRING:
- switch(Node->Type)
- {
- case NODETYPE_ADD: // Concatenate
- ret = Object_StringConcat(op1, op2);
- break;
- default:
- fprintf(stderr, "SpiderScript internal error: Exec,BinOP,String unknown op %i\n", Node->Type);
- ret = ERRPTR;
- break;
- }
+ case NODETYPE_ADD: // Concatenate
+ ret = Object_StringConcat(Left, Right);
break;
- // Integer Operations
- case SS_DATATYPE_INTEGER:
- switch(Node->Type)
- {
- case NODETYPE_ADD: ret = SpiderScript_CreateInteger( op1->Integer + op2->Integer ); break;
- case NODETYPE_SUBTRACT: ret = SpiderScript_CreateInteger( op1->Integer - op2->Integer ); break;
- case NODETYPE_MULTIPLY: ret = SpiderScript_CreateInteger( op1->Integer * op2->Integer ); break;
- case NODETYPE_DIVIDE: ret = SpiderScript_CreateInteger( op1->Integer / op2->Integer ); break;
- case NODETYPE_MODULO: ret = SpiderScript_CreateInteger( op1->Integer % op2->Integer ); break;
- case NODETYPE_BWAND: ret = SpiderScript_CreateInteger( op1->Integer & op2->Integer ); break;
- case NODETYPE_BWOR: ret = SpiderScript_CreateInteger( op1->Integer | op2->Integer ); break;
- case NODETYPE_BWXOR: ret = SpiderScript_CreateInteger( op1->Integer ^ op2->Integer ); break;
- case NODETYPE_BITSHIFTLEFT: ret = SpiderScript_CreateInteger( op1->Integer << op2->Integer ); break;
- case NODETYPE_BITSHIFTRIGHT:ret = SpiderScript_CreateInteger( op1->Integer >> op2->Integer ); break;
- case NODETYPE_BITROTATELEFT:
- ret = SpiderScript_CreateInteger( (op1->Integer << op2->Integer) | (op1->Integer >> (64-op2->Integer)) );
- break;
- default:
- fprintf(stderr, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i\n", Node->Type);
- ret = ERRPTR;
- break;
- }
+ default:
+ AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,String unknown op %i", Operation);
+ ret = ERRPTR;
break;
-
- // Real Numbers
- case SS_DATATYPE_REAL:
- switch(Node->Type)
- {
- default:
- fprintf(stderr, "SpiderScript internal error: Exec,BinOP,Real unknown op %i\n", Node->Type);
- ret = ERRPTR;
- break;
- }
+ }
+ break;
+ // Integer Operations
+ case SS_DATATYPE_INTEGER:
+ switch(Operation)
+ {
+ case NODETYPE_ADD: ret = SpiderScript_CreateInteger( Left->Integer + Right->Integer ); break;
+ case NODETYPE_SUBTRACT: ret = SpiderScript_CreateInteger( Left->Integer - Right->Integer ); break;
+ case NODETYPE_MULTIPLY: ret = SpiderScript_CreateInteger( Left->Integer * Right->Integer ); break;
+ case NODETYPE_DIVIDE: ret = SpiderScript_CreateInteger( Left->Integer / Right->Integer ); break;
+ case NODETYPE_MODULO: ret = SpiderScript_CreateInteger( Left->Integer % Right->Integer ); break;
+ case NODETYPE_BWAND: ret = SpiderScript_CreateInteger( Left->Integer & Right->Integer ); break;
+ case NODETYPE_BWOR: ret = SpiderScript_CreateInteger( Left->Integer | Right->Integer ); break;
+ case NODETYPE_BWXOR: ret = SpiderScript_CreateInteger( Left->Integer ^ Right->Integer ); break;
+ case NODETYPE_BITSHIFTLEFT: ret = SpiderScript_CreateInteger( Left->Integer << Right->Integer ); break;
+ case NODETYPE_BITSHIFTRIGHT:ret = SpiderScript_CreateInteger( Left->Integer >> Right->Integer ); break;
+ case NODETYPE_BITROTATELEFT:
+ ret = SpiderScript_CreateInteger( (Left->Integer << Right->Integer) | (Left->Integer >> (64-Right->Integer)) );
break;
-
default:
- fprintf(stderr, "SpiderScript error: Invalid operation (%i) on type (%i)\n", Node->Type, op1->Type);
+ AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i\n", Operation);
ret = ERRPTR;
break;
}
-
- // Free intermediate objects
- Object_Dereference(op1);
- Object_Dereference(op2);
break;
- //default:
- // ret = NULL;
- // fprintf(stderr, "ERROR: SpiderScript AST_ExecuteNode Unimplemented %i\n", Node->Type);
- // break;
+ // Real Numbers
+ case SS_DATATYPE_REAL:
+ switch(Operation)
+ {
+ default:
+ AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,Real unknown op %i", Operation);
+ ret = ERRPTR;
+ break;
+ }
+ break;
+
+ default:
+ AST_RuntimeError(NULL, "BUG - Invalid operation (%i) on type (%i)", Operation, Left->Type);
+ ret = ERRPTR;
+ break;
}
-_return:
+
+ if(Right && Right != preCastValue) free(Right);
+
return ret;
}
for( var = Block->FirstVar; var; prev = var, var = var->Next )
{
if( strcmp(var->Name, Name) == 0 ) {
- fprintf(stderr, "ERROR: Redefinition of variable '%s'\n", Name);
+ AST_RuntimeError(NULL, "Redefinition of variable '%s'", Name);
return ERRPTR;
}
}
{
for( var = bs->FirstVar; var; var = var->Next )
{
- if( strcmp(var->Name, Name) == 0 ) {
+ if( strcmp(var->Name, Name) == 0 )
+ {
if( !Block->Script->Variant->bDyamicTyped
- && (Value && var->Type != Value->Type) ) {
- fprintf(stderr, "ERROR: Type mismatch assigning to '%s'\n", Name);
+ && (Value && var->Type != Value->Type) )
+ {
+ AST_RuntimeError(NULL, "Type mismatch assigning to '%s'", Name);
return -2;
}
+// printf("Assign %p to '%s'\n", Value, var->Name);
Object_Reference(Value);
Object_Dereference(var->Object);
var->Object = Value;
}
else
{
- fprintf(stderr, "ERROR: Variable '%s' set while undefined\n", Name);
+ AST_RuntimeError(NULL, "Variable '%s' set while undefined", Name);
return -1;
}
}
}
}
- fprintf(stderr, "ERROR: Variable '%s' used undefined\n", Name);
+
+ AST_RuntimeError(NULL, "Variable '%s' used undefined", Name);
return ERRPTR;
}
*/
void Variable_Destroy(tAST_Variable *Variable)
{
+// printf("Variable_Destroy: (%p'%s')\n", Variable, Variable->Name);
Object_Dereference(Variable->Object);
free(Variable);
}
+
+void AST_RuntimeError(tAST_Node *Node, const char *Format, ...)
+{
+ va_list args;
+
+ fprintf(stderr, "ERROR: ");
+ va_start(args, Format);
+ vfprintf(stderr, Format, args);
+ va_end(args);
+ fprintf(stderr, "\n");
+
+ if(Node)
+ {
+ fprintf(stderr, " at %s:%i\n", Node->File, Node->Line);
+ }
+}