Commit 7ca11a46 authored by Sergio Costas's avatar Sergio Costas

Now the main program supports wildcards

Now frees all the memory blocks not used

Added a function call to the flex/byson library to free the tree

Now supports string literal concatenation ( "blah blah""bleh bleh" )
parent e71c3fc4
#!/usr/bin/env python3
import glob
import sys
from crusty import crusty
lib = crusty.crusty("lexyacclib/crusty.h","lexyacclib/crusty.so")
tree = lib.read_file(sys.argv[1],False)
if tree is None:
sys.exit()
def process_file(filename):
print("Processing file {:s}".format(filename))
lib = crusty.crusty("lexyacclib/crusty.h","lexyacclib/crusty.so")
tree = lib.read_file(filename,False)
if tree is None:
return
try:
variables = lib.process_tree(tree)
lib.process_functions(tree,variables)
except crusty.crusty.VarNotFoundException:
pass
except crusty.crusty.FunctionNotFoundException:
pass
lib.print_errors()
try:
variables = lib.process_tree(tree)
lib.process_functions(tree,variables)
except crusty.crusty.VarNotFoundException:
pass
except crusty.crusty.FunctionNotFoundException:
pass
lib.print_errors()
files = glob.glob(sys.argv[1])
for f in files:
process_file(f)
......@@ -150,6 +150,7 @@ class crusty(object):
retval = self.libcrusty.parse_data(c_bool(verbose),c_char_p(filename.encode("utf8")),rv.stdout,datasize)
if retval:
tree = self._fix_tree(retval)
self.libcrusty.free_all()
else:
# If there is an error, run again the parsing with VERBOSE = True to show the error to the user
#self.libcrusty.parse_data(c_bool(True),c_char_p(filename.encode("utf8")),rv.stdout,datasize)
......@@ -492,7 +493,22 @@ class crusty(object):
retvals += [ (self.STATUS_NOT_NULL, op2[1], self.TYPE_NO_CRUST,None, crusty.CONDITION_TRUE), (self.STATUS_NULL, op2[1], self.TYPE_NO_CRUST,None, crusty.CONDITION_FALSE) ]
return retvals
if (assignment.type == "GE_OP") or (assignment.type == "LE_OP") or (assignment.type == "<") or (assignment.type == ">"):
if (assignment.type == "GE_OP") or (assignment.type == "LE_OP") or (assignment.type == "<") or (assignment.type == ">") or (assignment.type == "|"):
retvals = []
op1list = self._eval_assignment(assignment.child1[0],var_container,True)
for op1 in op1list:
op2list = self._eval_assignment(assignment.child2[0],op1[1],True)
for op2 in op2list:
retvals += [(self.STATUS_NOT_NULL_OR_NULL, op2[1], self.TYPE_NO_CRUST,None, crusty.CONDITION_FALSE_TRUE) ]
return retvals
if (assignment.type == "+") or (assignment.type == "-") or (assignment.type == "&") or (assignment.type == "MUL_ASSIGN"):
child1 = assignment.child1
child2 = assignment.child2
if child1 is None:
return self._eval_assignment(assignment.child2[0],var_container,True)
if child2 is None:
return self._eval_assignment(assignment.child1[0],var_container,True)
retvals = []
op1list = self._eval_assignment(assignment.child1[0],var_container,True)
for op1 in op1list:
......@@ -744,5 +760,12 @@ class crusty(object):
retvals += self._check_return_block(block_false,self._copy_container(option[1]))
return retvals
if node.type == "MUL_ASSIGN":
options = self._eval_assignment(node,var_container,False)
retvals = []
for option in options:
retvals += self._check_return_block(tree[1:],option[1])
return retvals
print("Tipo: {:s}; Nombre: {:s}; Linea: {:s}".format(str(node.type),str(node.name),str(node.line)))
return self._check_return_block(tree[1:],var_container)
LIB ?= yes
ifneq ($(LIB),no)
CC = gcc -c -DLIBRARY -fpic
CC = gcc -g -c -DLIBRARY -fpic
LN = gcc -g -shared -o crusty.so
else
CC = gcc -g -c -DDO_PRINT
LN = gcc -o crusty
LN = gcc -g -o crusty
endif
all: crusty
......
......@@ -15,7 +15,7 @@ struct AST * new_leaf(int type, struct AST *template) {
ast->type = type;
if (template != NULL) {
ast->filename = template->filename;
ast->filename = strdup(template->filename);
ast->line = template->line;
} else {
ast->filename = NULL;
......@@ -240,6 +240,72 @@ void set_code_tree(struct AST *tree) {
full_tree = tree;
}
void free_all() {
free(current_file);
free_tree(full_tree);
free(previous_data_line);
free(current_data_line);
struct AST *to_delete;
while(types_list != NULL) {
to_delete = types_list->next;
free(types_list);
types_list = to_delete;
}
}
void check_tree(struct AST *tree) {
if (tree == NULL) {
return;
}
struct AST *node;
for(;tree != NULL; tree = tree->next) {
check_tree(tree->child1);
check_tree(tree->child2);
check_tree(tree->child3);
check_tree(tree->child4);
check_tree(tree->condition);
check_tree(tree->bitfield);
check_tree(tree->struct_data);
check_tree(tree->enum_data);
check_tree(tree->arrays);
check_tree(tree->assignment);
check_tree(tree->for_ch1);
check_tree(tree->for_ch2);
check_tree(tree->for_ch3);
check_tree(tree->right);
check_tree(tree->function_params);
}
}
void free_tree(struct AST *tree) {
struct AST *to_delete;
while(tree != NULL) {
free(tree->filename);
free(tree->data);
free(tree->name);
free_tree(tree->child1);
free_tree(tree->child2);
free_tree(tree->child3);
free_tree(tree->child4);
free_tree(tree->right);
free_tree(tree->condition);
free_tree(tree->function_params);
free_tree(tree->bitfield);
free_tree(tree->struct_data);
free_tree(tree->enum_data);
free_tree(tree->arrays);
free_tree(tree->assignment);
free_tree(tree->for_ch1);
free_tree(tree->for_ch2);
free_tree(tree->for_ch3);
to_delete = tree;
tree = tree->next;
free(to_delete);
}
}
void show_error(int line,struct AST *element) {
printf("\n\nUndefined Statement at line %d; Data: %s; File: %s\n\n\n",line,element->data,element->filename);
exit(-1);
......@@ -263,7 +329,7 @@ struct AST* parse_file(bool show_debug, char *filename) {
do_verbose = show_debug ? 1 : 0;
#else
int main(int argc,char **argv) {
do_verbose = 1;
do_verbose = 0;
char *filename = argv[1];
#endif
int retval = -1;
......@@ -276,6 +342,7 @@ int main(int argc,char **argv) {
if (yyin != NULL) {
retval = yyparse();
}
fclose(yyin);
printf("Error: %d\n",retval);
#ifdef LIBRARY
if (retval == 0){
......@@ -284,6 +351,8 @@ int main(int argc,char **argv) {
return (NULL);
}
#else
check_tree(full_tree);
free_all();
return retval;
#endif
}
......@@ -94,6 +94,8 @@ void append_fparams(struct AST *leaf, struct AST *param);
void mix_ast_leaves(struct AST *base, struct AST *new_leaf);
void copy_ast_data(struct AST *base, struct AST *new_leaf);
struct AST *transform_leaf(struct AST *leaf, int newtype);
void free_tree(struct AST *tree);
void free_all();
void check_typedef(struct AST *type);
......
......@@ -203,9 +203,7 @@ void set_current_line() {
char *start;
int size;
if (current_file != NULL) {
free(current_file);
}
free(current_file);
current_file = NULL;
sscanf(yytext,("# %d"),&current_line);
current_line--;
......
This diff is collapsed.
void printf(char *);
struct test {
int b;
} a;
void main() {
if (a->b >= 1)
printf("1\n");
else
printf("no 1\n");
}
void printf(char *);
struct test {
int b;
} a;
void main() {
if (a->b >= 1)
printf("1\n");
else if (a->b >= 2)
printf("2\n");
else
printf("no 1,2\n");
}
void printf(char *);
struct test {
int b;
} a;
void main() {
if (a->b >= 1) {
printf("1\n");
} else {
if (a->b >= 2) {
printf("2\n");
} else {
printf("no 1,2\n");
}
}
}
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