Commit 67f527a7 authored by Sergio Costas's avatar Sergio Costas

Now the typedefs works as expected

Added some debug features
parent ba4281cb
......@@ -55,6 +55,9 @@ def print_nodes(nodelist, tabs = 0):
#print_nodes(tree)
variables,functions = lib.process_tree(tree)
lib.process_functions(tree,variables,functions)
try:
variables,functions = lib.process_tree(tree)
lib.process_functions(tree,variables,functions)
except:
pass
lib.print_errors()
......@@ -201,7 +201,7 @@ class crusty(object):
for var_group in var_container:
if var_name in var_group:
return var_group[var_name]
self._add_error(self.MSG_ERROR,_("Unknown variable {:s} at line {:d}").format(var_name,line_number))
self._add_error(self.MSG_ERROR,_("Unknown variable {:s} at line {:d}").format(str(var_name),line_number))
raise self.VarNotFoundException()
......@@ -268,20 +268,20 @@ class crusty(object):
def _eval_assignment(self,assignment,var_container,function_container):
if assignment is None:
return [ (crusty.STATUS_UNINITIALIZED, var_container) ]
return [ (crusty.STATUS_UNINITIALIZED, self._copy_container(var_container)) ]
assignment = assignment[0]
if assignment.type == "CONSTANT":
if (assignment.t_null) or (assignment.intval == 0):
return [ self.STATUS_NULL ]
return [ (self.STATUS_NULL, self._copy_container(var_container)) ]
else:
return [ self.STATUS_NOT_NULL ]
return [ (self.STATUS_NOT_NULL, self._copy_container(var_container)) ]
elif assignment.type == "FCONSTANT":
if assignment.floatval == 0.0:
return [ self.STATUS_NULL ]
return [ (self.STATUS_NULL, self._copy_container(var_container)) ]
else:
return [ self.STATUS_NOT_NULL ]
return [ (self.STATUS_NOT_NULL, self._copy_container(var_container)) ]
elif assignment.type == "STRING_LITERAL":
return [ self.STATUS_NOT_NULL ]
return [ (self.STATUS_NOT_NULL, self._copy_container(var_container)) ]
elif assignment.type == "?":
states = self._eval_condition(assignment.condition,var_container,function_container)
retvals = []
......@@ -296,7 +296,7 @@ class crusty(object):
return retvals
else:
print("assignment type Unknown {:s}".format(assignment.type))
return [ self.STATUS_NOT_NULL_OR_NULL ]
return [ (self.STATUS_NOT_NULL_OR_NULL, self._copy_container(var_container)) ]
def _get_next_block(self,current_block):
......@@ -372,6 +372,9 @@ class crusty(object):
parameters.append( self._get_variable_properties(parameter,True) )
function_container[node.name] = parameters
continue
if node.type == "CRUSTY_DEBUG":
print("Debug: {:s}".format(str(var_container)))
continue
return var_container,function_container
......@@ -385,6 +388,9 @@ class crusty(object):
for counter in range(len(tree)):
node = tree[counter]
if node.type == "CRUSTY_DEBUG":
print("Debug: {:s}".format(str(var_container)))
continue
if node.type == "START_BLOCK":
# append a new block for the variables created inside this block
var_container.insert(0,{})
......@@ -405,10 +411,11 @@ class crusty(object):
if len(options) > 1:
for option in options[1:]:
# create a new "execution thread" for each possible assignment value
newvar_container = self._copy_container(var_container)
self._set_var_value(newvar_container,node.name,option,node.line)
newvar_container = option[1]
self._set_var_value(newvar_container,node.name,option[0],node.line)
self._process_block(tree[counter+1:],newvar_container,function_container)
self._set_var_value(var_container,node.name,options[0],node.line)
var_container = options[0][1]
self._set_var_value(var_container,node.name,options[0][0],node.line)
continue
if node.type == "=":
varname = node.child1[0].name
......@@ -417,10 +424,11 @@ class crusty(object):
if len(options) > 1:
for option in options[1:]:
# create a new "execution thread" for each possible assignment value
newvar_container = self._copy_container(var_container)
self._set_var_value(newvar_container,varname,option,node.line)
newvar_container = option[1]
self._set_var_value(newvar_container,varname,option[0],node.line)
self._process_block(tree[counter+1:],newvar_container,function_container)
self._set_var_value(var_container,varname,options[0],node.line)
var_container = options[0][1]
self._set_var_value(var_container,varname,options[0][0],node.line)
continue
# if node.type == "FOR":
# print("Tipo: {:s}; Nombre: {:s}".format(str(node.type),str(node.name)))
......
......@@ -197,16 +197,6 @@ struct AST *find_type(char *type) {
return NULL;
}
void add_type_element(struct AST *leaf) {
if (find_type(leaf->data) != NULL) {
printf("Repetido %s",leaf->data);
exit(-1);
}
//printf(" (Anado %s; %d (%s)) ",leaf->name,leaf->pointer,leaf->function ? "es funcion" : "no es funcion");
leaf->next = types_list;
types_list = leaf;
}
struct AST *copy_leaf(struct AST *leaf) {
struct AST *tmp = (struct AST *)malloc(sizeof(struct AST));
......@@ -235,7 +225,14 @@ void check_typedef(struct AST *name) {
return;
}
add_type_element(copy_leaf(name));
struct AST *leaf = copy_leaf(name);
if (find_type(leaf->data) != NULL) {
printf("Repetido %s",leaf->data);
exit(-1);
}
//printf(" (Anado %s; %d (%s)) ",leaf->name,leaf->pointer,leaf->function ? "es funcion" : "no es funcion");
leaf->next = types_list;
types_list = leaf;
}
......
......@@ -81,8 +81,10 @@ void set_current_line();
"__crust_recycle__" { return prepare_leaf(0,0.0,CRUSTY_RECYCLE); }
"__crust_enable__" { return prepare_leaf(0,0.0,CRUSTY_ENABLE); }
"__crust_disable__" { return prepare_leaf(0,0.0,CRUSTY_DISABLE); }
"__crust_full_disable__" { return prepare_leaf(0,0.0,CRUSTY_FULL_DISABLE); }
"__crust_debug__" { return prepare_leaf(0,0.0,CRUSTY_DEBUG); }
{L}({L}|{D})* { tmptype = check_type(); return prepare_leaf(0,0.0,tmptype); }
{L}({L}|{D})* { return prepare_leaf(0,0.0,TYPENAME_IDENTIFIER); }
0[xX]{H}+{IS}? { sscanf(yytext,"%Lx",&tmpint); return prepare_leaf(tmpint,0.0,CONSTANT); }
0[0-7]*{IS}? { sscanf(yytext,"%Lo",&tmpint); return prepare_leaf(tmpint,0.0,CONSTANT); }
......@@ -343,6 +345,18 @@ int prepare_leaf(unsigned long long int v1, double v2, int current_token) {
yylval->intval = 0;
current_token = CONSTANT;
break;
case TYPENAME_IDENTIFIER:
{
struct AST *leaf2 = find_type(yytext);
if (leaf2 != NULL) {
mix_ast_leaves(yylval,leaf2);
yylval->t_typedef = false;
current_token = TYPE_NAME;
} else {
current_token = IDENTIFIER;
}
}
break;
}
yylval->line = current_line;
......
......@@ -34,6 +34,8 @@ void add_type_element(char *);
%token CRUSTY_RECYCLE
%token CRUSTY_ENABLE
%token CRUSTY_DISABLE
%token CRUSTY_FULL_DISABLE
%token CRUSTY_DEBUG
%token VARIABLE_DEFINITION
%token T_NULL
%token FCONSTANT
......@@ -47,6 +49,7 @@ void add_type_element(char *);
%token END_DEFAULT
%token START_BLOCK
%token END_BLOCK
%token TYPENAME_IDENTIFIER
//%start translation_unit
%start start_unit
......@@ -598,10 +601,12 @@ block_item_list
;
block_item
: declaration { $$ = $1; }
| statement { $$ = $1; }
| CRUSTY_ENABLE { $$ = $1; }
| CRUSTY_DISABLE { $$ = $1; }
: declaration { $$ = $1; }
| statement { $$ = $1; }
| CRUSTY_ENABLE { $$ = $1; }
| CRUSTY_DISABLE { $$ = $1; }
| CRUSTY_FULL_DISABLE { $$ = $1; }
| CRUSTY_DEBUG { $$ = $1; }
;
expression_statement
......@@ -670,6 +675,8 @@ external_declaration
| declaration { $$ = $1; }
| CRUSTY_ENABLE { $$ = $1; }
| CRUSTY_DISABLE { $$ = $1; }
| CRUSTY_FULL_DISABLE { $$ = $1; }
| CRUSTY_DEBUG { $$ = $1; }
;
function_definition
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment