-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisp.c
126 lines (116 loc) · 2.59 KB
/
lisp.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "lisp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Node *read_expr(char *input) {
int i = 0;
char c = input[i++];
if (c == '(') {
if (input[i] == '\0') return NULL;
Node *head = NULL;
Node *tail = NULL;
while (c != '\0' && c == '(') {
Node *n = read_expr(input + i);
if (n == NULL) {
if (head != NULL) free_list(head);
return NULL;
}
if (tail == NULL) {
head = n;
tail = n;
} else {
tail->next = n;
tail = n;
}
c = input[i++];
if (c == ')') break;
}
if (tail != NULL) tail->next = NULL;
return head;
} else {
char *str_end = strchr(input + i, '(');
if (str_end == NULL) {
str_end = input + strlen(input) - 1;
} else {
str_end--;
}
int str_len = str_end - (input + i) + 1;
char *str_start = input + i - 1;
char *str = malloc(str_len + 1);
if (str == NULL) return NULL;
memcpy(str, str_start, str_len);
str[str_len] = '\0';
Node *n = malloc(sizeof(Node));
if (n == NULL) {
free(str);
return NULL;
}
n->val = malloc(str_len + 1);
if (n->val == NULL) {
free(str);
free(n);
return NULL;
}
memcpy(n->val, str, str_len + 1);
free(str);
n->next = NULL;
i += str_len + 1;
return n;
}
}
void free_list(Node *head) {
while (head != NULL) {
Node *next = head->next;
free(head->val);
free(head);
head = next;
}
}
void free_nodes(Node *n) {
if (n == NULL) return;
free_nodes(n->next);
free(n);
}
void eval_atom(Node *n) {
if (n == NULL) return;
if (strcmp(n->val, "true") == 0 || strcmp(n->val, "false") == 0) {
printf("bool: %s ", n->val);
} else if (n->val[0] == '\"' && n->val[strlen(n->val) - 1] == '\"') {
printf("string: %s ", n->val);
} else if (n->val[0] == '\'') {
printf("char: %s ", n->val);
} else if (n->val[0] == '-' || (n->val[0] >= '0' && n->val[0] <= '9')) {
printf("number: %s ", n->val);
} else {
printf("symbol: %s ", n->val);
}
}
void eval_expr(Node *n) {
if (n == NULL) return;
if (n->next == NULL) { // atom
eval_atom(n);
} else {
printf("(");
Node *curr = n;
while (curr != NULL) {
eval_expr(curr);
curr = curr->next;
}
printf(") ");
}
}
void print_result(Node *expr) {
if (expr == NULL) return;
if (expr->val[0] != '(') {
printf("%s", expr->val);
return;
}
putchar('(');
Node *head = expr;
while (head != NULL) {
print_result(head);
head = head->next;
if (head != NULL) putchar(' ');
}
putchar(')');
}