language

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

stk.h (1378B)


      1 /* stk.h -> Provide implementation of a stack
      2  */
      3 
      4 #ifndef STK_H
      5 #define STK_H
      6 
      7 #define STACK_SIZE_LIMIT 0x100
      8 
      9 #include <stdlib.h>
     10 #include <stdio.h>
     11 
     12 #include "var.h"
     13 #include "helper.h"
     14 
     15 typedef struct stk_line {
     16 	int        ptr;
     17 	size_t     size;
     18 	var_cont*  data[STACK_SIZE_LIMIT];
     19 } stk_line;
     20 
     21 typedef struct stk_t {
     22 	struct stk_t* next;
     23 	stk_line* stack;
     24 } stk_t;
     25 
     26 /* Create a new stack instance
     27  */
     28 stk_t* stk_new(void);
     29 stk_line* stk_line_new(size_t);
     30 
     31 /* Delete a stack
     32  */
     33 void stk_del(stk_t*);
     34 void stk_line_del(stk_line*);
     35 
     36 /* Pushes new stack level
     37  */
     38 void stk_newlevel(stk_t**);
     39 
     40 /* Pops a stack level
     41  */
     42 void stk_poplevel(stk_t**);
     43 
     44 /* Pop the first element of the stack
     45  *  stk_t* - stack instance
     46  */
     47 var_cont* stk_pop(stk_t*);
     48 
     49 /* Pushes var_cont* to the stack
     50  *  stk_t*     - stack instance
     51  *  var_cont*  - variable instance
     52  */
     53 void stk_push(stk_t*, var_cont*);
     54 
     55 /* Returns the data at location in the stack
     56  *  stk_line* - Stack line instance
     57  *  int       - integer < (STK_SIZE_LIMIT - stk_line->ptr)
     58  */
     59 var_cont* stk_at(stk_t*, int);
     60 
     61 /* Rotates the top two elements of the stack
     62  * i.e. [4, 3, 2, 1] -> [3, 4, 2, 1]
     63  *                   ^ rot_top()
     64  */
     65 void stk_rot_top(stk_t*);
     66 
     67 /* Rotates the top three elements of the stack
     68  * i.e. [4, 3, 2, 1] -> [2, 3, 4, 1]
     69  *                   ^ rot_three()
     70  */
     71 void stk_rot_three(stk_t*);
     72 
     73 #endif // STK_H