Compare commits
No commits in common. "2734cdb176170f584b52033d812404f4675cd425" and "b8255e4e93079e7a67ab01187a1594d28e12ef17" have entirely different histories.
2734cdb176
...
b8255e4e93
Binary file not shown.
@ -20,18 +20,11 @@ typedef struct n {
|
|||||||
* @return Pointer to the initialized node
|
* @return Pointer to the initialized node
|
||||||
* */
|
* */
|
||||||
node_t* node_init (
|
node_t* node_init (
|
||||||
node_t* nd,
|
node_t *nd, node_index_t type, void *data, uint64_t n_children, ...
|
||||||
node_index_t type,
|
|
||||||
void* data,
|
|
||||||
uint64_t n_children,
|
|
||||||
...
|
|
||||||
);
|
);
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SYM_GLOBAL_VAR,
|
SYM_GLOBAL_VAR, SYM_FUNCTION, SYM_PARAMETER, SYM_LOCAL_VAR
|
||||||
SYM_FUNCTION,
|
|
||||||
SYM_PARAMETER,
|
|
||||||
SYM_LOCAL_VAR
|
|
||||||
} symtype_t;
|
} symtype_t;
|
||||||
|
|
||||||
typedef struct s {
|
typedef struct s {
|
||||||
|
@ -12,10 +12,6 @@ tree_print(node_t* root, stem head);
|
|||||||
|
|
||||||
static void destroy_subtree ( node_t *discard );
|
static void destroy_subtree ( node_t *discard );
|
||||||
|
|
||||||
static void prune_children(node_t **simplified, node_t *root);
|
|
||||||
static void resolve_constant_expressions(node_t **simplified, node_t *root);
|
|
||||||
static void flatten(node_t **simplified, node_t *root);
|
|
||||||
|
|
||||||
|
|
||||||
/* External interface */
|
/* External interface */
|
||||||
void
|
void
|
||||||
@ -158,71 +154,18 @@ destroy_subtree ( node_t *discard )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
flatten(node_t **simplified, node_t *root)
|
|
||||||
{
|
|
||||||
/* This will flatten left-expanded lists */
|
|
||||||
if (!root)
|
|
||||||
return;
|
|
||||||
|
|
||||||
/* Do this recursivly */
|
|
||||||
for (int i = 0; i < root->n_children; i++)
|
|
||||||
flatten(&root->children[i], root->children[i]);
|
|
||||||
|
|
||||||
node_t **new_children, *result = root;
|
|
||||||
switch (root->type)
|
|
||||||
{
|
|
||||||
case GLOBAL_LIST:
|
|
||||||
case STATEMENT_LIST:
|
|
||||||
case PRINT_LIST:
|
|
||||||
case EXPRESSION_LIST:
|
|
||||||
case VARIABLE_LIST:
|
|
||||||
case DECLARATION_LIST:
|
|
||||||
// Check if node have more than two children
|
|
||||||
if (root->n_children < 2)
|
|
||||||
break;
|
|
||||||
|
|
||||||
result = root->children[0];
|
|
||||||
result->n_children++;
|
|
||||||
|
|
||||||
// Realloc the array of children to the new size
|
|
||||||
if (!(new_children = realloc(result->children, result->n_children * sizeof(node_t*))))
|
|
||||||
break;
|
|
||||||
// if successs, insert the new array
|
|
||||||
result->children = new_children;
|
|
||||||
|
|
||||||
// Insert child at the end
|
|
||||||
result->children[result->n_children - 1] = root->children[1];
|
|
||||||
|
|
||||||
node_finalize(root);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
*simplified = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
prune_children(node_t **simplified, node_t *root)
|
prune_children(node_t **simplified, node_t *root)
|
||||||
{
|
{
|
||||||
if (!root)
|
if (!root)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Do this recursivly */
|
|
||||||
for (int i = 0; i < root->n_children; i++)
|
|
||||||
prune_children(&root->children[i], root->children[i]);
|
|
||||||
|
|
||||||
node_t *result = root;
|
node_t *result = root;
|
||||||
switch (root->type)
|
switch (root->type)
|
||||||
{
|
{
|
||||||
case PROGRAM:
|
|
||||||
case GLOBAL:
|
case GLOBAL:
|
||||||
//case ARGUMENT_LIST: // For this to work, need to change order of operations
|
//case ARGUMENT_LIST: // For this to work, need to change order of operations
|
||||||
//case PARAMETER_LIST: // For this to work, need to change order of operations
|
//case PARAMETER_LIST: // For this to work, need to change order of operations
|
||||||
//case VARIABLE_LIST:
|
|
||||||
//case EXPRESSION_LIST:
|
|
||||||
case DECLARATION:
|
|
||||||
case STATEMENT:
|
case STATEMENT:
|
||||||
case PRINT_ITEM:
|
case PRINT_ITEM:
|
||||||
case PRINT_STATEMENT:
|
case PRINT_STATEMENT:
|
||||||
@ -246,10 +189,6 @@ resolve_constant_expressions(node_t **simplified, node_t *root)
|
|||||||
if (!root)
|
if (!root)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Do this recursivly */
|
|
||||||
for (int i = 0; i < root->n_children; i++)
|
|
||||||
resolve_constant_expressions(&root->children[i], root->children[i]);
|
|
||||||
|
|
||||||
if (root->type != EXPRESSION)
|
if (root->type != EXPRESSION)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -300,7 +239,6 @@ resolve_constant_expressions(node_t **simplified, node_t *root)
|
|||||||
|
|
||||||
switch (*(char*)root->data)
|
switch (*(char*)root->data)
|
||||||
{
|
{
|
||||||
/* Assignments */
|
|
||||||
case '|': *lhs |= *rhs; break;
|
case '|': *lhs |= *rhs; break;
|
||||||
case '^': *lhs ^= *rhs; break;
|
case '^': *lhs ^= *rhs; break;
|
||||||
case '&': *lhs &= *rhs; break;
|
case '&': *lhs &= *rhs; break;
|
||||||
@ -320,80 +258,57 @@ resolve_constant_expressions(node_t **simplified, node_t *root)
|
|||||||
*simplified = result;
|
*simplified = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
resolve_constant_relations( node_t** simplified, node_t* root)
|
flatten(node_t **simplified, node_t *root)
|
||||||
{
|
{
|
||||||
|
/* This will flatten left-expanded lists */
|
||||||
if (!root)
|
if (!root)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Do this recursivly */
|
node_t **new_children, *result = root;
|
||||||
for (int i = 0; i < root->n_children; i++)
|
switch (root->type)
|
||||||
resolve_constant_relations(&root->children[i], root->children[i]);
|
{
|
||||||
|
case GLOBAL_LIST:
|
||||||
if (root->type != RELATION)//|| root->type != RELATION)
|
case STATEMENT_LIST:
|
||||||
return;
|
case PRINT_LIST:
|
||||||
|
case EXPRESSION_LIST:
|
||||||
node_t *result = root;
|
case VARIABLE_LIST:
|
||||||
|
case DECLARATION_LIST:
|
||||||
if (root->n_children != 2)
|
// Check if node have more than two children
|
||||||
return;
|
if (root->n_children < 2)
|
||||||
|
break;
|
||||||
// Both children must be constant numbers
|
|
||||||
if (root->children[0]->type != NUMBER_DATA ||
|
|
||||||
root->children[1]->type != NUMBER_DATA)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Check if children does not contain null pointers
|
|
||||||
if (!root->children[0]->data)
|
|
||||||
return;
|
|
||||||
if (!root->children[1]->data)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Check if data field is not null pointer
|
|
||||||
if (!root->data)
|
|
||||||
return;
|
|
||||||
|
|
||||||
result = root->children[0];
|
result = root->children[0];
|
||||||
int64_t
|
result->n_children++;
|
||||||
*lhs = result->data,
|
|
||||||
*rhs = root->children[1]->data;
|
|
||||||
|
|
||||||
switch (*(char*)root->data)
|
// Realloc the array of children to the new size
|
||||||
{
|
if (!(new_children = realloc(result->children, result->n_children * sizeof(node_t*))))
|
||||||
/* Relations */
|
break;
|
||||||
case '=': *lhs = (*lhs == *rhs); break;
|
// if successs, insert the new array
|
||||||
case '<': *lhs = (*lhs < *rhs); break;
|
result->children = new_children;
|
||||||
case '>': *lhs = (*lhs > *rhs); break;
|
|
||||||
}
|
// Insert child at the end
|
||||||
|
result->children[result->n_children - 1] = root->children[1];
|
||||||
|
|
||||||
node_finalize(root->children[1]);
|
|
||||||
node_finalize(root);
|
node_finalize(root);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
*simplified = result;
|
*simplified = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
simplify_tree ( node_t **simplified, node_t *root )
|
simplify_tree ( node_t **simplified, node_t *root )
|
||||||
{
|
{
|
||||||
if (!root)
|
if (!root)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//for (int i = 0; i < root->n_children; i++)
|
for (int i = 0; i < root->n_children; i++)
|
||||||
// simplify_tree(&root->children[i], root->children[i]);
|
simplify_tree(&root->children[i], root->children[i]);
|
||||||
|
|
||||||
/*
|
|
||||||
Each of the functions do their operations recursivly.
|
|
||||||
This opens up for a lot more flexibility, like removing
|
|
||||||
variable list after it is flatten
|
|
||||||
*/
|
|
||||||
flatten(&root, root);
|
|
||||||
prune_children(&root, root);
|
prune_children(&root, root);
|
||||||
resolve_constant_expressions(&root, root);
|
resolve_constant_expressions(&root, root);
|
||||||
|
flatten(&root, root);
|
||||||
// The following is experimental, will resolve the constant relations
|
|
||||||
resolve_constant_relations(&root, root);
|
|
||||||
|
|
||||||
*simplified = root;
|
*simplified = root;
|
||||||
}
|
}
|
||||||
|
@ -7,18 +7,6 @@ func my_fun(a, b, c, d, e, f, g, h) begin
|
|||||||
if i = f then begin
|
if i = f then begin
|
||||||
print "hmmm"
|
print "hmmm"
|
||||||
end
|
end
|
||||||
|
|
||||||
if 1 = 1 then begin
|
|
||||||
print "true"
|
|
||||||
end
|
|
||||||
|
|
||||||
if 1 = 2 then begin
|
|
||||||
print "false"
|
|
||||||
end
|
|
||||||
|
|
||||||
if 1 < 2 then begin
|
|
||||||
print "true"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
func main() begin
|
func main() begin
|
||||||
|
Loading…
x
Reference in New Issue
Block a user