-
Notifications
You must be signed in to change notification settings - Fork 1
/
bython-legacy.c
187 lines (152 loc) · 4.05 KB
/
bython-legacy.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Node_t {
int op;
struct Node_t* left;
struct Node_t* right;
};
typedef struct
{
char name[64];
float value[];
} ByObject_List;
typedef struct
{
} CallStack;
int num_objects = 0;
float eval(float num1, char op, float num2)
{
printf("EVALUATE %f and %f\n", num1, num2);
if (op == '+')
return num1 + num2;
else if (op == '-')
return num1 - num2;
else if (op == '*')
return num1 * num2;
else
return num1 / num2;
}
float retrieve(char *name, int* result)
{
printf("LOAD %s\n", name);
// Named
for (int i = 0; i < num_objects; i++)
{
if (strcmp(names[i]->name, name) == 0)
{
// printf("%f\n", names[i]->value);
return names[i]->value;
}
}
}
void assign(char *name, float value)
{
printf("ASSIGN %s to %f\n", name, value);
// Check if there's an existing assignment
// if (retrieve(name)) {
// printf("Error: Attempting to reassign to an existing value\n");
// exit(1);
// }
ByObject *obj = (ByObject *)malloc(sizeof(ByObject));
strcpy(obj->name, name);
obj->value = value;
names[num_objects] = obj;
// printf("%s: %f\n", names[num_objects]->name, names[num_objects]->value);
num_objects++;
}
// Tokenise
float parse_statement(char *statement)
{
if (strstr(statement, "(") != NULL)
{
// printf("This is a callable\n");
char caller[256];
char rhs[256];
char params[256];
sscanf(strtok(statement, "("), "%s", caller);
sscanf(strtok(NULL, "("), "%s", rhs);
sscanf(strtok(rhs, ")"), "%s", params);
printf("caller: %s\n", caller);
printf("params: %s\n", rhs);
// printf("params: %s\n", params);
}
else if (strcmp(statement, "globals()") == 0)
{
for (int i = 0; i < num_objects; i++)
printf("%s: %f\n", names[i]->name, names[i]->value);
}
else if (strstr(statement, "=") != NULL)
{
// printf("This is an assignment (for now)\n");
char name[256];
char rhs[256];
sscanf(strtok(statement, "="), "%s", name);
sscanf(strtok(NULL, "="), "%s", rhs);
// printf("rhs: %s\n", rhs);
assign(name, parse_statement(rhs));
return -1;
}
else if (strstr(statement, "->") != NULL)
{
// printf("This is an assignment (for now)\n");
char name[256];
char rhs[256];
sscanf(strtok(statement, "->"), "%s", rhs);
sscanf(strtok(NULL, "->"), "%s", name);
// printf("rhs: %s\n", rhs);
assign(name, parse_statement(rhs));
return -1;
}
else if (strstr(statement, "+") != NULL)
{
// printf("This is an eval (for now)\n");
char lhs[256];
char rhs[256];
float ret;
sscanf(strtok(statement, "+"), "%s", lhs);
sscanf(strtok(NULL, "+"), "%s", rhs);
ret = eval(parse_statement(lhs), '+', parse_statement(rhs));
printf("%f\n", ret);
return ret;
}
else
{
// atoms
// printf("This is a retrieval (for now)\n");
char name[256];
float number;
int is_number;
is_number = sscanf(strtok(statement, "="), "%f", &number);
if (is_number)
return number;
else
{
sscanf(strtok(statement, "="), "%s", name);
number = retrieve(name);
return number;
}
}
return -999999;
}
int main()
{
// REPL session
while (1)
{
printf(">>> ");
// Read
char statement[256];
fgets(statement, sizeof(statement), stdin);
size_t len = strlen(statement) - 1;
if (*statement && statement[len-1] == ':')
printf("WIP\n");
else if (*statement && statement[len] == '\n')
statement[len] = '\0';
parse_statement(statement);
}
// Memory management
for (int i = 0; i < num_objects; i++)
free(names[i]);
return 0;
}