чување 1fc3e991adec2674d8efd4e6b09c863b056c79e1
родитељ 1ff2416357edaf85f2a925442a1213d4c172d96d
Аутор: Страхиња Радић <sr@strahinja.org>
Датум: Tue, 20 Jan 2026 11:26:37 +0100
Optimize: Remove brackets as a node
Diffstat:
измењених датотека: 2, додавања: 1(+), брисања: 23(-)
diff --git a/bool.h b/bool.h
@@ -1,6 +1,5 @@
enum NodeType {
NT_IDENT,
- NT_BRACKETS,
NT_UNARY,
NT_BINARY
};
@@ -20,10 +19,6 @@ struct expr_ident {
int id;
};
-struct expr_brackets {
- struct expr_node* arg;
-};
-
struct expr_unary {
enum Operator operator;
struct expr_node* arg;
@@ -38,7 +33,6 @@ struct expr_node {
enum NodeType type;
union {
struct expr_ident* ident;
- struct expr_brackets* brackets;
struct expr_unary* unary;
struct expr_binary* binary;
} expr;
diff --git a/bool.y b/bool.y
@@ -62,7 +62,6 @@ extern int yylex(void);
const char* node_type_ids[] = {
NTN(NT_IDENT),
- NTN(NT_BRACKETS),
NTN(NT_UNARY),
NTN(NT_BINARY)
};
@@ -173,12 +172,6 @@ new_expr(const enum NodeType node_type, const enum Operator operator,
np->type = node_type;
switch (node_type)
{
- case NT_BRACKETS:
- if (!(np->expr.brackets = malloc(sizeof(struct expr_brackets))))
- goto new_expr_malloc_error;
- if (!(np->expr.brackets->arg = va_arg(ap, struct expr_node*)))
- goto new_expr_va_arg_error;
- break;
case NT_UNARY:
if (!(np->expr.unary = malloc(sizeof(struct expr_unary))))
goto new_expr_malloc_error;
@@ -224,12 +217,6 @@ print_node(const int level, const int debug_only, struct expr_node* node)
fprintf(debug_only && debug ? stderr : stdout, "\t");
switch (node->type)
{
- case NT_BRACKETS:
- //printf("( ");
- fprintf(debug_only && debug ? stderr : stdout, "()\n");
- print_node(level+1, debug_only, node->expr.brackets->arg);
- //printf(" )");
- break;
case NT_UNARY:
//printf("%s { ", operator_images[node->expr.unary->operator]);
fprintf(debug_only && debug ? stderr : stdout,
@@ -288,9 +275,6 @@ evaluate(const struct expr_node* node)
default:;
}
break;
- case NT_BRACKETS:
- result = evaluate(node->expr.brackets->arg);
- break;
case NT_IDENT:
result = get_val(node->expr.ident->id);
break;
@@ -360,7 +344,7 @@ expr: IDENT { $$ = new_expr(NT_IDENT, OP_NONE, 1, $1);
print_node(0, 1, $$);
root = $$;
}
- | '(' expr ')' { $$ = new_expr(NT_BRACKETS, OP_NONE, 1, $2);
+ | '(' expr ')' { $$ = $2;
print_node(0, 1, $$);
root = $$;
}