use builtin operator associativity functionality
[ccc.git] / ast.c
diff --git a/ast.c b/ast.c
index b220f5e..4d89925 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -40,11 +40,34 @@ struct ast *ast_int(int integer)
        return res;
 }
 
+struct ast *ast_unop(enum unop op, struct ast *l)
+{
+       struct ast *res = ast_alloc();
+       res->type = an_unop;
+       res->data.an_unop.op = op;
+       res->data.an_unop.l = l;
+       return res;
+}
 static const char *binop_str[] = {
+       [binor] = "||",
+       [binand] = "&&",
+       [eq] = "==",
+       [neq] = "!=",
+       [leq] = "<=",
+       [le] = "<",
+       [geq] = ">=",
+       [ge] = ">",
+       [cons] = ":",
        [plus] = "+",
-       [minus] = "+",
-       [times] = "+",
-       [divide] = "+",
+       [minus] = "-",
+       [times] = "*",
+       [divide] = "/",
+       [modulo] = "%",
+       [power] = "^",
+};
+static const char *unop_str[] = {
+       [inverse] = "!",
+       [negate] = "-",
 };
 
 void ast_print(struct ast * ast, FILE *out)
@@ -67,6 +90,11 @@ void ast_print(struct ast * ast, FILE *out)
        case an_int:
                fprintf(out, "%d", ast->data.an_int);
                break;
+       case an_unop:
+               fprintf(out, "(-");
+               ast_print(ast->data.an_unop.l, out);
+               fprintf(out, ")");
+               break;
        default:
                fprintf(stderr, "Unsupported AST node\n");
                exit(1);