SpiderScript - Huge changes to introduce bytecode support
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / exec_bytecode.c
1 /*
2  * SpiderScript Library
3  * by John Hodge (thePowersGang)
4  * 
5  * bytecode_makefile.c
6  * - Generate a bytecode file
7  */
8 #include <stdlib.h>
9 #include "bytecode_ops.h"
10
11 typedef sBC_StackEnt    tBC_StackEnt;
12
13 enum eBC_StackEntTypes
14 {
15         ET_NULL,        // Start of the stack
16         ET_FUNCTION_START,      // Start of the function
17         ET_INTEGER,     // Integer / Boolean
18         ET_REAL,        // Real number
19         ET_OBJECT,      // Object
20         ET_REFERENCE    // Reference to a tSpiderValue
21 };
22
23 struct sBC_StackEnt
24 {
25         uint8_t EntType;
26         uint8_t _padding[3];
27         union {
28                 uint64_t        Integer;
29                 double          Real;
30                 tSpiderValue    *Reference;
31                 tSpiderObject   *Object;
32         };
33 };
34
35 struct sBC_Stack
36 {
37          int    EntrySpace;
38          int    EntryCount;
39         tBC_StackEnt    Entries[];
40 };
41
42 // === CODE ===
43 int Bytecode_int_StackPop(tBC_Stack *Stack, tBC_StackEnt *Dest)
44 {
45         if( Stack->EntryCount == 0 )    return 1;
46         Stack->EntryCount --;
47         *Dest = Stack->Entries[Stack->EntryCount];
48         return 0;
49 }
50
51 int Bytecode_int_StackPush(tBC_Stack *Stack, tBC_StackEnt *Src)
52 {
53         if( Stack->EntryCount == Stack->EntrySpace )    return 1;
54         Stack->Entries[Stack->EntryCount] = *Src;
55         Stack->EntryCount ++;
56         return 0;
57 }
58
59 #define GET_STACKVAL(dst)       if((ret = Bytecode_int_StackPop(Stack, &dst)))    return ret;
60
61 int Bytecode_ExecuteFunction(tBC_Function *Fcn, tBC_Stack *Stack, int ArgCount);
62 {
63         tBC_Op  *op;
64         tBC_StackEnt    val1, val2;
65         tBC_StackEnt    local_vars[Fcn->MaxVariableCount+Fcn->ArgumentCount];
66         
67         // Pop off arguments
68         
69         // Mark the start
70
71         // Execute!
72         op = Fcn->Operations;
73         while(op)
74         {
75                 tBC_Op  *nextop = op->Next;
76                 switch(op->Type)
77                 {
78                 case BC_OP_JUMP:
79                         nextop = Fcn->Labels[op->StringInt.Integer];
80                         break;
81                 case BC_OP_JUMPIF:
82                         GET_STACKVAL(val1);
83                         if( Bytecode_int_IsStackEntTrue(&val1) )
84                                 nextop = Fcn->Labels[op->StringInt.Integer];
85                         break;
86                 case BC_OP_JUMPIFNOT:
87                         GET_STACKVAL(val1);
88                         if( !Bytecode_int_IsStackEntTrue(&val1) )
89                                 nextop = Fcn->Labels[op->StringInt.Integer];
90                         break;
91                 default:
92                         // TODO:
93                         break;
94                 }
95                 op = nextop;
96         }
97         
98         // Clean up
99 }

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