33 lines
940 B
C
33 lines
940 B
C
|
|
#include"ast.h"
|
||
|
|
|
||
|
|
#include<stdint.h>
|
||
|
|
#include<string.h>
|
||
|
|
#include<stdlib.h>
|
||
|
|
|
||
|
|
int BINOP_COMMUTATIVE[] = {
|
||
|
|
[BINOP_ADD] = 1,
|
||
|
|
[BINOP_SUB] = 0,
|
||
|
|
[BINOP_MUL] = 1,
|
||
|
|
[BINOP_DIV] = 0
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
AST *ast_expression_optimize(AST *ast) {
|
||
|
|
return ast;
|
||
|
|
}
|
||
|
|
|
||
|
|
int ast_expression_equal(AST *a, AST *b) {
|
||
|
|
if(a->nodeKind != b->nodeKind) return 0;
|
||
|
|
|
||
|
|
if(a->nodeKind == AST_EXPR_PRIMITIVE) {
|
||
|
|
return a->exprPrim.val == b->exprPrim.val;
|
||
|
|
} else if(a->nodeKind == AST_EXPR_VAR) {
|
||
|
|
return a->exprVar.thing == b->exprVar.thing;
|
||
|
|
} else if(a->nodeKind == AST_EXPR_UNARY_OP) {
|
||
|
|
return a->exprUnOp.operator == b->exprUnOp.operator && ast_expression_equal(a->exprUnOp.operand, b->exprUnOp.operand);
|
||
|
|
} else if(a->nodeKind == AST_EXPR_BINARY_OP) {
|
||
|
|
return a->exprBinOp.operator == b->exprBinOp.operator && ast_expression_equal(a->exprBinOp.operands[0], b->exprBinOp.operands[0]) && ast_expression_equal(a->exprBinOp.operands[1], b->exprBinOp.operands[1]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|