Usermode/ld-acess - Fixing Elf64 support (and incorrect Uint* sizes)
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / main.c
index ce39c08..bd29280 100644 (file)
@@ -6,10 +6,15 @@
 #include <stdio.h>
 #include <string.h>
 #include <spiderscript.h>
+#include "common.h"
 #include "ast.h"
+#include "bytecode_gen.h"
 
 // === IMPORTS ===
-extern tAST_Script     *Parse_Buffer(tSpiderVariant *Variant, char *Buffer);
+extern  int    Parse_Buffer(tSpiderScript *Script, const char *Buffer, const char *Filename);
+extern tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name);
+extern void    Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value);
+extern void    Variable_Destroy(tAST_Variable *Variable);
 
 // === CODE ===
 /**
@@ -35,43 +40,86 @@ tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filen
                return NULL;
        }
        
-       ret = malloc(sizeof(tSpiderScript));
-       ret->Variant = Variant;
-       
        fseek(fp, 0, SEEK_END);
        fLen = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        
-       data = malloc(fLen);
+       // Allocate and read data
+       data = malloc(fLen + 1);
        if(!data)       return NULL;
-       fread(data, fLen, 1, fp);
-       
+       fLen = fread(data, 1, fLen, fp);
        fclose(fp);
+       if( fLen < 0 ) {
+               free(data);
+               return NULL;
+       }
+       data[fLen] = '\0';
+       
        
-       ret->Script = Parse_Buffer(Variant, data);
+       // Create the script
+       ret = malloc(sizeof(tSpiderScript));
+       ret->Variant = Variant;
+       ret->Functions = NULL;
+       ret->LastFunction = NULL;
+       
+       ret->CurNamespace = NULL;
+       if( Parse_Buffer(ret, data, Filename) ) {
+               free(data);
+               free(ret);
+               return NULL;
+       }
        
        free(data);
        
+       
+       // HACK!!
+       #if 1
+       // - Save AST to a file
+       {
+               char    cacheFilename[strlen(Filename)+6+1];
+               strcpy(cacheFilename, Filename);
+               strcat(cacheFilename, ".ast");
+       
+               SpiderScript_SaveAST(ret, cacheFilename);       
+       }
+       #endif
+       // - Save Bytecode too
+       {
+               char    cacheFilename[strlen(Filename)+6+1];
+               strcpy(cacheFilename, Filename);
+               strcat(cacheFilename, ".bc");
+       
+               SpiderScript_SaveBytecode(ret, cacheFilename);  
+       }
+       
        return ret;
 }
 
-/**
- * \brief Execute a script function
- * \todo Arguments?
- */
-tSpiderVariable *SpiderScript_ExecuteMethod(tSpiderScript *Script, const char *Function)
+int SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename)
 {
-       tAST_Function   *fcn = Script->Script->Functions;
+       size_t  size;
+       FILE    *fp;
+       void    *data;
+       printf("Total Size: ");
+       fflush(stdout);
+       size = AST_WriteScript(NULL, Script);
+       printf("0x%x bytes\n", (unsigned)size);
        
-       // Find the function
-       for( ; fcn; fcn = fcn->Next ) {
-               if( strcmp(fcn->Name, Function) == 0 )
-                       break;
+       fp = fopen(Filename, "wb");
+       if(!fp) return 1;
+
+       data = malloc(size);
+       if(!data) {
+               fclose(fp);
+               return -1;
        }
-       if(!fcn)        return NULL;
        
-       // Execute!
-       return AST_ExecuteNode(Script, fcn->Code);
+       size = AST_WriteScript(data, Script);
+       fwrite(data, size, 1, fp);
+       free(data);
+
+       fclose(fp);
+       return 0;
 }
 
 /**
@@ -79,24 +127,19 @@ tSpiderVariable *SpiderScript_ExecuteMethod(tSpiderScript *Script, const char *F
  */
 void SpiderScript_Free(tSpiderScript *Script)
 {
-       tAST_Function   *fcn = Script->Script->Functions;
-       tAST_Function   *nextFcn;
-       tAST_Node       *var, *nextVar;
+       tScript_Function        *fcn = Script->Functions;
+       tScript_Function        *nextFcn;
        
        // Free functions
-       while(fcn) {
-               AST_FreeNode( fcn->Code );
-               
-               var = fcn->Arguments;
-               while(var)
-               {
-                       nextVar = var->NextSibling;
-                       AST_FreeNode( var );
-                       var = nextVar;
-               }
-               
+       while(fcn)
+       {
+               if(fcn->ASTFcn) AST_FreeNode( fcn->ASTFcn );
+               if(fcn->BCFcn)  Bytecode_DeleteFunction( fcn->BCFcn );
+
                nextFcn = fcn->Next;
                free( fcn );
                fcn = nextFcn;
        }
+       
+       free(Script);
 }

UCC git Repository :: git.ucc.asn.au