diff options
Diffstat (limited to 'syntax.c')
-rw-r--r-- | syntax.c | 148 |
1 files changed, 134 insertions, 14 deletions
@@ -1,5 +1,10 @@ #include "syntax.h" +/* options avaliable */ +#define ASSERT_ON +#define __EXIT_ABBORT +#include "assert.h" + //token postion/type #define TP(POS) tl_tok_type(tl,(POS)) #define TP_EQ(POS,TYPE) (tl_tok_type(tl,(POS)) == TYPE) @@ -14,10 +19,13 @@ /*****************************************************************************/ ast_root* ast_syntax( token_list *tl ) { + AS_NULL(tl) + ast_root *ast_tree = NULL; ast_range *rng = NULL; tk_pos=0; + ast_tree = atrt_i(); while ( tk_pos < tl_size(tl) ) @@ -27,15 +35,23 @@ ast_root* ast_syntax( token_list *tl ) && TP_NEQ(tk_pos+1,TOK_RANGE)) { rng = att_range( tl, tk_pos ); - if (rng == NULL ) break; - atrt_a_range( &ast_tree, rng, 0 ); + if (rng == NULL ) + { + ERROR("rng=null\n"); + break; + } + atrt_a_range( &ast_tree, rng ); // current - INT|HEX next RANGE } else if ( TP_EQ2(tk_pos,TOK_INT,TOK_HEX) && TP_EQ(tk_pos+1,TOK_RANGE) ) { rng = att_range( tl, tk_pos ); - if (rng == NULL ) break; - atrt_a_range( &ast_tree, rng, 0 ); + if (rng == NULL ) + { + ERROR("rng == NULL\n"); + break; + } + atrt_a_range( &ast_tree, rng ); } else { printf("ERR:%d\n",tl_tok_type(tl,tk_pos)); @@ -52,7 +68,8 @@ ast_root* ast_syntax( token_list *tl ) ast_if* att_if( token_list *tl, int pos ) { //PRINT("IF:pos->%d %d\n",tk_pos,pos); - + AS_NULL(tl) + AS_EXPR(pos<0) ast_if *ret = NULL; ast_expr *expr = NULL; @@ -89,6 +106,8 @@ ast_if* att_if( token_list *tl, int pos ) ast_range* att_range( token_list *tl, int pos ) { //PRINT("RNG:pos->%d %d\n",tk_pos,pos); + AS_NULL(tl) + ast_range *ret = NULL; ast_value *val = NULL; ast_if *iff = NULL; @@ -106,7 +125,11 @@ ast_range* att_range( token_list *tl, int pos ) ret = atrn_i(); atrn_s_rng0( &ret, tok2int(tl_get_tok(tl,pos)) ); val = att_value( tl, pos+1 ); - if (val==NULL) return NULL; + if (val==NULL) + { + ERROR("val == NULL\n"); + return NULL; + } atrn_s_val( &ret, val ); tk_pos = pos+2; //current HEX IF ... @@ -115,7 +138,11 @@ ast_range* att_range( token_list *tl, int pos ) ret = atrn_i(); atrn_s_rng0( &ret, tok2int(tl_get_tok(tl,pos))); iff = att_if( tl, pos+1 ); - if (iff==NULL) return NULL; + if (iff==NULL) + { + ERROR("iff == NULL\n"); + return NULL; + } atrn_a_if( &ret, iff ); //current HEX RNG HEX } else if (TP_EQ(pos+1,TOK_RANGE) && @@ -126,13 +153,21 @@ ast_range* att_range( token_list *tl, int pos ) if (TP_EQ(pos+3,TOK_STR)) { val = att_value(tl,pos+3); - if (val==NULL) return NULL; + if (val==NULL) + { + ERROR("val == NULL\n"); + return NULL; + } atrn_s_val( &ret, val ); tk_pos = pos+4; } else if (TP_EQ(pos+3,TOK_IF)) { iff = att_if( tl, pos+3 ); - if (iff==NULL) return NULL; + if (iff==NULL) + { + ERROR("iff == NULL\n"); + return NULL; + } atrn_a_if( &ret, iff ); } } else @@ -147,7 +182,11 @@ ast_range* att_range( token_list *tl, int pos ) pos = tk_pos; //PRINT("There is more ifs\n"); iff = att_if( tl, pos ); - if (iff==NULL) return NULL; + if (iff==NULL) + { + ERROR("iff == NULL\n"); + return NULL; + } atrn_a_if( &ret, iff ); //PNL(); } @@ -159,6 +198,7 @@ ast_range* att_range( token_list *tl, int pos ) /*****************************************************************************/ ast_expr* att_expr( token_list *tl, int pos ) { + AS_NULL(tl) //PRINT("EXPR:pos->%d %d\n",tk_pos,pos); ast_expr *ret = NULL; @@ -190,6 +230,7 @@ ast_expr* att_expr( token_list *tl, int pos ) /*****************************************************************************/ ast_expr_cmp* att_expr_cmp( token_list *tl, int pos ) { + AS_NULL(tl) //PRINT("CMP:pos->%d %d\n",tk_pos,pos); ast_expr_cmp *ret = NULL; @@ -212,12 +253,14 @@ ast_expr_cmp* att_expr_cmp( token_list *tl, int pos ) /*****************************************************************************/ ast_expr_bit* att_expr_bit( token_list *tl, int pos ) { + AS_NULL(tl) return NULL; } /*****************************************************************************/ ast_value* att_value( token_list *tl, int pos ) { + AS_NULL(tl) //PRINT("VAL:pos->%d %d\n", tk_pos, pos); ast_value *val = NULL; token *tok = NULL; @@ -252,15 +295,21 @@ ast_root* atrt_i() } memset( ret, 0, sizeof(ast_root) ); + + return ret; } /*****************************************************************************/ -int atrt_a_range( ast_root **rt, ast_range *rng, int idx) +int atrt_a_range( ast_root **rt, ast_range *rng) { - //PNL(); - (*rt)->range = realloc( (*rt)->range, sizeof(void*)*(*rt)->total_tokens+1 ); + AS_NULL(rt) + AS_NULL(*rt) + AS_NULL(rng) + + (*rt)->range = realloc( (*rt)->range, sizeof(void*)*((*rt)->total_tokens+1) ); + AS_NULL((*rt)->range) (*rt)->range[(*rt)->total_tokens] = rng; (*rt)->total_tokens += 1; @@ -288,8 +337,11 @@ ast_range* atrn_i() /*****************************************************************************/ int atrn_a_if( ast_range **rng, ast_if *iff ) { + AS_NULL(rng) + AS_NULL(iff) //PNL(); - (*rng)->iff = realloc( (*rng)->iff, sizeof(void*)*(*rng)->size_iff+1 ); + (*rng)->iff = realloc( (*rng)->iff, sizeof(void*)*((*rng)->size_iff+1) ); + AS_NULL((*rng)->iff) (*rng)->iff[(*rng)->size_iff] = iff; (*rng)->size_iff += 1; return 0; @@ -299,6 +351,10 @@ int atrn_a_if( ast_range **rng, ast_if *iff ) /*****************************************************************************/ int atrn_a_val( ast_range **rng, ast_value *val ) { + AS_NULL(rng) + AS_NULL(*rng) + AS_NULL(val) + (*rng)->val = val; return 0; @@ -308,6 +364,8 @@ int atrn_a_val( ast_range **rng, ast_value *val ) /*****************************************************************************/ int atrn_s_rng0( ast_range **rng, int value ) { + AS_NULL(rng) + (*rng)->range_type = 0; (*rng)->start = value; (*rng)->end = value; @@ -319,6 +377,9 @@ int atrn_s_rng0( ast_range **rng, int value ) /*****************************************************************************/ int atrn_s_rng1( ast_range **rng, unsigned long start, unsigned long end) { + AS_NULL(rng) + AS_EXPR(start>end) + (*rng)->range_type = 1; (*rng)->start = start; (*rng)->end = end; @@ -330,6 +391,10 @@ int atrn_s_rng1( ast_range **rng, unsigned long start, unsigned long end) /*****************************************************************************/ int atrn_s_val( ast_range **rng, ast_value *val ) { + AS_NULL(rng) + AS_NULL(*rng) + AS_NULL(val) + (*rng)->val = val; return 0; @@ -356,6 +421,9 @@ ast_if* ati_i() /*****************************************************************************/ int ati_s_expr( ast_if **iff, ast_expr *expr ) { + AS_NULL(iff) + AS_NULL(expr) + (*iff)->expr = expr; return -1; } @@ -364,6 +432,9 @@ int ati_s_expr( ast_if **iff, ast_expr *expr ) /*****************************************************************************/ int ati_s_val( ast_if **iff, ast_value *val ) { + AS_NULL(iff) + AS_NULL(val) + (*iff)->val = val; return -1; } @@ -389,15 +460,20 @@ ast_expr* ate_i() /*****************************************************************************/ int ate_a_bit( ast_expr **expr, ast_expr_bit *bit ) { + AS_NULL(expr) + AS_NULL(*expr) + AS_NULL(bit) //PNL(); if ((*expr)->bit == NULL) { (*expr)->bit = malloc(sizeof(void *)); + AS_NULL((*expr)->bit) (*expr)->bit[0] = bit; (*expr)->size_bit = 1; } else { (*expr)->bit = realloc( (*expr)->bit, sizeof(void*)*((*expr)->size_bit+1) ); + AS_NULL((*expr)->bit) (*expr)->bit[(*expr)->size_bit] = bit; (*expr)->size_bit += 1; } @@ -409,15 +485,19 @@ int ate_a_bit( ast_expr **expr, ast_expr_bit *bit ) /*****************************************************************************/ int ate_a_cmp( ast_expr **expr, ast_expr_cmp *cmp ) { + AS_NULL(expr) + AS_NULL(cmp) //PNL(); if ((*expr)->cmp == NULL) { (*expr)->cmp = malloc(sizeof(void *)); + AS_NULL((*expr)->cmp) (*expr)->cmp[0] = cmp; (*expr)->size_cmp = 1; } else { (*expr)->cmp = realloc( (*expr)->cmp, sizeof(void*)*((*expr)->size_cmp+1) ); + AS_NULL((*expr)->cmp) (*expr)->cmp[(*expr)->size_cmp] = cmp; (*expr)->size_cmp += 1; } @@ -429,6 +509,8 @@ int ate_a_cmp( ast_expr **expr, ast_expr_cmp *cmp ) /*****************************************************************************/ int ate_eval( ast_expr *expr ) { + AS_NULL(expr) + return -1; } @@ -453,6 +535,8 @@ ast_expr_bit* ateb_i() /*****************************************************************************/ int ateb_s_mask( ast_expr_bit **expr, unsigned char mask ) { + AS_NULL(expr) + (*expr)->mask = mask; return 0; @@ -479,7 +563,11 @@ ast_expr_cmp* atec_i() /*****************************************************************************/ int atec_s_val( ast_expr_cmp **expr, unsigned char val ) { + AS_NULL(expr) + AS_NULL(*expr) + (*expr)->val = val; + return 0; } @@ -504,6 +592,10 @@ ast_value* atv_i() /*****************************************************************************/ int atv_s_note( ast_value **val, char *note, int size) { + AS_NULL(val) + AS_NULL(note) + AS_EXPR(size<1) + (*val)->note = note; (*val)->size = size; @@ -513,6 +605,8 @@ int atv_s_note( ast_value **val, char *note, int size) /*****************************************************************************/ int print_ast( ast_root *root ) { + AS_NULL(root) + int i,j,k; ast_range *rng = NULL; @@ -526,6 +620,7 @@ int print_ast( ast_root *root ) { printf("RANGE:%d\n",i ); rng = root->range[i]; + AS_NULL(rng) if ( rng->range_type == 0 || rng->range_type == 1 ) { printf("%d-%d:\n",rng->start, rng->end); @@ -538,6 +633,7 @@ int print_ast( ast_root *root ) printf("\tIF\n"); for (j=0;j<rng->size_iff;j++) { + AS_NULL(rng->iff[j]) if (rng->iff[j]->expr != NULL) { printf("\t\tCMP:"); @@ -556,4 +652,28 @@ int print_ast( ast_root *root ) } } return 0; +} + +/*****************************************************************************/ +int ast_destroy( ast_root *root ) +{ + AS_NULL(root) + + int i,j,k; + ast_range *rng; + + + if (root == NULL) + return -1; + + for (i=0;i<root->total_tokens;i++) + { + rng = root->range[i]; + if (rng == NULL) continue; + + } + free( root->range ); + free( root ); + + return 0; }
\ No newline at end of file |