-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplparse.c
More file actions
89 lines (73 loc) · 1.85 KB
/
plparse.c
File metadata and controls
89 lines (73 loc) · 1.85 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <assert.h>
#include "plparse.h"
PLTerm *PLParseTerm(const PLToken **tokens);
PLTerm *PLParseArguments(const PLToken **tokens)
{
PLTerm *t = PLParseTerm(tokens);
PLTokenType type = (*tokens)->type;
*tokens = (*tokens)->next;
if (type == PLTokenComma) {
t->next = PLParseArguments(tokens);
} else if (type == PLTokenParenthesisClose) {
} else {
assert(0);
}
return t;
}
PLTerm *PLParseBody(const PLToken **tokens)
{
PLTerm *t = PLParseTerm(tokens);
PLTokenType type = (*tokens)->type;
*tokens = (*tokens)->next;
if (type == PLTokenComma) {
t->next = PLParseBody(tokens);
} else if (type == PLTokenPeriod) {
} else {
assert(0);
}
return t;
}
PLTerm *PLParseTerm(const PLToken **tokens)
{
PLTokenType type = (*tokens)->type;
PLTerm *t;
if (type == PLTokenConstant) {
t = PLTermCreate(PLTermCompound);
t->datum.compoundTerm.name = (char *)malloc(strlen((*tokens)->value) + 1);
strcpy(t->datum.compoundTerm.name, (*tokens)->value);
*tokens = (*tokens)->next;
type = (*tokens)->type;
if (type == PLTokenParenthesisOpen) {
*tokens = (*tokens)->next;
t->datum.compoundTerm.arguments = PLParseArguments(tokens);
}
} else if (type == PLTokenVariable) {
t = PLTermCreate(PLTermVariable);
t->datum.variable = (char *)malloc(strlen((*tokens)->value) + 1);
strcpy(t->datum.variable, (*tokens)->value);
*tokens = (*tokens)->next;
} else {
assert(0);
}
return t;
}
PLTerm *PLParse(const PLToken **tokens)
{
assert(tokens);
if (!*tokens) {
return NULL;
}
PLTerm *head = PLParseTerm(tokens);
PLTokenType type = (*tokens)->type;
*tokens = (*tokens)->next;
if (type == PLTokenPeriod) {
} else if (type == PLTokenArrow) {
assert(PLTermIsCompound(head));
head->datum.compoundTerm.body = PLParseBody(tokens);
} else if (type == PLTokenComma) {
head->next = PLParse(tokens);
} else {
assert(0);
}
return head;
}