language

some fools attempt at an interpreted language
Log | Files | Refs | README

var.h (4005B)


      1 /*  var.h -> Provide implemenation of types
      2  */
      3 
      4 #ifndef VAR_H
      5 #define VAR_H
      6 
      7 #include <stdlib.h>
      8 #include <stdio.h>
      9 
     10 #include "helper.h"
     11 
     12 typedef unsigned int bc_addr;
     13 typedef unsigned int ns_addr;
     14 
     15 typedef enum {
     16 	VOID,   // 0
     17 	ADDR,   // 1
     18 	TYPE,   // 2
     19 	PLIST,  // 3
     20 	FUNC,   // 4
     21 	OBJBLDR,// 5
     22 	OBJECT, // 6
     23 	G_INT,  // 7
     24 	G_FLOAT,// 8
     25 	G_CHAR, // 9
     26 	G_STR,  // A
     27 	S_ARRAY,// B
     28 	D_ARRAY,// C
     29 	H_TABLE,// D
     30 	G_FIFO, // E
     31 	G_PTR   // F
     32 } b_type;
     33 
     34 typedef struct var_cont {
     35 	long ownership;
     36 	b_type type;
     37 	unsigned int refstat;
     38 	void*  data;
     39 } var_cont;
     40 
     41 #define MAXIMUM_TRACKING_VARS 0xFFFFFF
     42 struct var_track {
     43 	unsigned int ptr;
     44 	var_cont* vars[MAXIMUM_TRACKING_VARS];
     45 };
     46 
     47 struct var_track VAR_TRACK;
     48 
     49 typedef struct var_data {
     50 	long ownership;
     51 } var_data;
     52 
     53 typedef struct var_data_type {
     54 	long ownership;
     55 	b_type v;
     56 } var_data_type;
     57 
     58 typedef struct var_data_plist {
     59 	long ownership;
     60 	b_type* v;
     61 } var_data_plist;
     62 
     63 typedef struct var_data_func {
     64 	long ownership;
     65 	bc_addr loc;
     66 	bc_addr end;
     67 	b_type  type;
     68 	ns_addr size;
     69 	size_t  paramlen;
     70 	b_type* param;
     71 } var_data_func;
     72 
     73 typedef struct var_data_objbldr {
     74 	long ownership;
     75 	ns_addr id;
     76 	bc_addr loc;
     77 	bc_addr end;
     78 	ns_addr size;
     79 	ns_addr instc;
     80 	size_t paramlen;
     81 	b_type* param;
     82 } var_data_objbldr;
     83 
     84 typedef struct var_data_int {
     85 	long ownership;
     86 	int v;
     87 } var_data_int;
     88 
     89 typedef struct var_data_float {
     90 	long ownership;
     91 	double v;
     92 } var_data_float;
     93 
     94 typedef struct var_data_char {
     95 	long ownership;
     96 	char v;
     97 } var_data_char;
     98 
     99 typedef struct var_data_str {
    100 	long ownership;
    101 	size_t size;
    102 	char* v;
    103 } var_data_str;
    104 
    105 typedef struct var_data_array {
    106 	long ownership;
    107 	size_t size;
    108 	var_cont* v;
    109 } var_data_array;
    110 
    111 typedef struct var_data_ptr {
    112 	long ownership;
    113 	var_cont* v;
    114 } var_data_ptr;
    115 
    116 #include "bc.h"
    117 
    118 void init_var_track();
    119 
    120 void var_clean();
    121 
    122 /* Initialze variable with type
    123  */
    124 var_cont* var_new(b_type);
    125 
    126 void* var_data_alloc_TYPE(b_type);
    127 void* var_data_alloc_PLIST(size_t);
    128 void* var_data_alloc_FUNC(b_type);
    129 void* var_data_alloc_OBJBLDR(void);
    130 void* var_data_alloc_OBJECT();
    131 void* var_data_alloc_G_INT(int);
    132 void* var_data_alloc_G_FLOAT(double);
    133 void* var_data_alloc_G_CHAR(char);
    134 void* var_data_alloc_G_STR(size_t);
    135 void* var_data_alloc_S_ARRAY(size_t);
    136 
    137 /* Frees variable
    138  */
    139 void var_del(var_cont*);
    140 void var_data_free(void*, b_type);
    141 
    142 void var_data_free_PLIST(void*);
    143 void var_data_free_FUNC(void*);
    144 void var_data_free_OBJBLDR(void*);
    145 void var_data_free_OBJECT(void*);
    146 void var_data_free_G_STR(void*);
    147 
    148 /* Sets variable
    149  * void* -> pointer to allocated space of memory that agrees with b_type
    150  */
    151 void var_set(var_cont*, void*, b_type);
    152 
    153 b_type           var_data_get_TYPE(var_cont*);
    154 var_data_func*   var_data_get_FUNC(var_cont*);
    155 var_data_objbldr* var_data_get_OBJBLDR(var_cont*);
    156 int              var_data_get_G_INT(var_cont*);
    157 double           var_data_get_G_FLOAT(var_cont*);
    158 char             var_data_get_G_CHAR(var_cont*);
    159 char*            var_data_get_G_STR(var_cont*);
    160 b_type*          var_data_get_PLIST(var_cont*);
    161 
    162 
    163 void* var_data_cpy_G_INT(var_data_int*);
    164 void* var_data_cpy_G_FLOAT(var_data_float*);
    165 void* var_data_cpy_G_CHAR(var_data_char*);
    166 void* var_data_cpy_G_STR(var_data_str*);
    167 
    168 var_cont* var_data_cpy(var_cont*);
    169 
    170 /* Lovely little function to take bytes and turn it into an integer.
    171  *  int     - sizeof(bytes)
    172  *  int     - offset
    173  *  byte_t* - array of bytes
    174  */
    175 var_cont* raw_to_int(int, int, byte_t*, long);
    176 
    177 /* Byte to b_type.
    178  *  byte_t - value maps to enum b_type
    179  */
    180 var_cont* byte_to_type(byte_t, long);
    181 
    182 /* Converts array of bytes of size n into parameter list
    183  *
    184  *  int     - sizeof(bytes)
    185  *  byte_t* - array of bytes
    186  */
    187 var_cont* raw_to_plist(int, byte_t*, long);
    188 
    189 /* Converts raw (ascii) string into normal string type
    190  * int     - sizeof(bytes)
    191  * int     - offset
    192  * byte_t* - array of bytes
    193  */
    194 var_cont* raw_to_str(int, int, byte_t*, long);
    195 
    196 /* Raw variable to var_cont
    197  *
    198  *  int     - sizeof(bytes)
    199  *  byte_t* - array of bytes
    200  */
    201 var_cont* raw_to_var(int, byte_t*, long);
    202 
    203 #endif // var_H