-
Notifications
You must be signed in to change notification settings - Fork 0
/
asttest.c
54 lines (44 loc) · 1.64 KB
/
asttest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include <stdio.h>
#include "visitor.h"
static void show(Expression* expr) {
switch(expr->type) {
case ADD_EXPRESSION:
fprintf(stderr, "add\n");
show(expr->u.binary_expression.left);
show(expr->u.binary_expression.right);
break;
case SUB_EXPRESSION:
fprintf(stderr, "sub\n");
show(expr->u.binary_expression.left);
show(expr->u.binary_expression.right);
break;
case INT_EXPRESSION:
fprintf(stderr, "int:%d\n", expr->u.int_value);
break;
case DOUBLE_EXPRESSION:
fprintf(stderr, "double:%f\n", expr->u.double_value);
break;
default:
break;
}
}
int main(void) {
MAS_Interpreter *interp = mas_create_interpreter(); // this is required to create ast_storage
Visitor* visitor = create_visitor();
Expression* int_expr = mas_create_int_expression(10);
Expression* d_expr = mas_create_double_expression(3.5);
Expression* int_expr2 = mas_create_int_expression(1);
Expression* int_expr3 = mas_create_int_expression(2);
Expression* bexpr2 = mas_create_binary_expression(SUB_EXPRESSION, int_expr2, int_expr3);
Expression* bexpr = mas_create_binary_expression(ADD_EXPRESSION, bexpr2, d_expr);
fprintf(stderr, "-- show ---\n");
// show(bexpr);
traverse_expr(bexpr, visitor);
mas_delete_interpreter();
return 0;
}