-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_pltoken.c
60 lines (52 loc) · 1.1 KB
/
test_pltoken.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
55
56
57
58
59
#include "test_pltoken.h"
void test_token(const char *filename, int stop)
{
WITH_COLOR(BLUE, printf("Testing tokens in file: %s\n", filename));
FILE *file = fopen(filename, "r");
if (!file) {
WITH_COLOR(RED, printf("Could not open file: %s\n", filename));
return;
}
PLToken *token = PLTokenise(file, stop);
WITH_COLOR(YELLOW, print_tokens(token));
PLTokensFree(token);
fclose(file);
}
void print_tokens(PLToken *t)
{
while (t) {
switch (t->type) {
case(PLTokenConstant):
case(PLTokenVariable):
printf("%s", t->value);
break;
case(PLTokenParenthesisOpen):
printf("(");
break;
case(PLTokenParenthesisClose):
printf(")");
break;
case(PLTokenComma):
printf(", ");
break;
case(PLTokenPeriod):
printf(".\n");
break;
case(PLTokenArrow):
printf(" :- ");
break;
default:
WITH_COLOR(RED, printf("Illegal token: %d\n", t->type));
}
t = t->next;
}
printf("\n");
}
void test_pltoken()
{
WITH_COLOR(GREEN, printf("test_pltoken()\n"));
test_token("sum.pl", 0);
test_token("ackermann.pl", 0);
test_token("facts.pl", 1);
test_token("parse_test.pl", 0);
}