-
Notifications
You must be signed in to change notification settings - Fork 1
/
tok.h
137 lines (116 loc) · 3.92 KB
/
tok.h
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
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#ifndef TOK_H
#define TOK_H
struct ByAst_Node *fake_parse_statement(char *statement)
{
if (strstr(statement, "=") != NULL)
{
printf("This is an assignment ");
char lhs_name[256];
char rhs[256];
int rhs_int;
sscanf(strtok(statement, "="), "%s", lhs_name);
if (sscanf(strtok(NULL, "="), "%s", rhs))
{
printf("(list).\n");
int leftbound, rightbound;
sscanf(strtok(rhs, "..."), "%d", &leftbound);
sscanf(strtok(NULL, "..."), "%d", &rightbound);
if (leftbound > rightbound)
exit(0);
struct ByAst_Node *ast = (struct ByAst_Node *)malloc(sizeof(struct ByAst_Node));
ast->operation = Assign_List;
ast->param1 = (struct ByAst_Node *)malloc(sizeof(struct ByAst_Node));
ast->param2 = (struct ByAst_Node *)malloc(sizeof(struct ByAst_Node));
ast->param3 = (struct ByAst_Node *)malloc(sizeof(struct ByAst_Node));
strcpy(ast->param1->name.char_value, lhs_name);
ast->param2->value.int_value = leftbound;
ast->param3->value.int_value = rightbound;
return ast;
}
else
// if (sscanf(strtok(NULL, "="), "%d", &rhs_int))
{
printf("(numeric)\n");
struct ByAst_Node *ast = (struct ByAst_Node *)malloc(sizeof(struct ByAst_Node));
ast->operation = Assign;
ast->param1 = (struct ByAst_Node *)malloc(sizeof(struct ByAst_Node));
ast->param2 = (struct ByAst_Node *)malloc(sizeof(struct ByAst_Node));
strcpy(ast->param1->name.char_value, lhs_name);
ast->param2->value.int_value = rhs_int;
return ast;
}
}
else if (strcmp(statement, "exit") == 0)
{
printf("Bye!\n");
exit(0);
}
else if (strstr(statement, "+") != NULL)
{
printf("This is an operation.\n");
exit(0);
// 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 if (strstr(statement, "func") != NULL)
{
printf("This is a function definition\n");
exit(0);
}
else
{
printf("This is loading.\n");
struct ByAst_Node *ast = (struct ByAst_Node *)malloc(sizeof(struct ByAst_Node));
ast->operation = Load;
ast->param1 = (struct ByAst_Node *)malloc(sizeof(struct ByAst_Node));
strcpy(ast->param1->name.char_value, statement);
return ast;
}
}
#endif /* TOK_H */
// struct ByAST_Node* 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
// {
// 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;
// }