-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.c
126 lines (106 loc) · 3.04 KB
/
repl.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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char* buffer;
size_t buffer_length;
ssize_t input_length;
}InputBuffer;
typedef enum{
META_COMMAND_SUCCESS,
META_COMMAND_UNRECOGNIZED_COMMAND
} MetaCommandResult;
typedef enum{
PREPARE_SUCCESS,
PREPARE_UNRECOGNIZED_STATEMENT
}PrepareResult;
typedef enum{
STATEMENT_INSERT,
STATEMENT_SELECT
}StatementType;
typedef struct{
StatementType type;
}Statement;
InputBuffer* new_input_buffer(){
InputBuffer* input_buffer = malloc(sizeof(InputBuffer));
input_buffer->buffer = NULL;
input_buffer->buffer_length = 0;
input_buffer->input_length = 0;
return input_buffer;
}
void close_input_buffer(InputBuffer* input_buffer){
free(input_buffer->buffer);
free(input_buffer);
}
MetaCommandResult do_meta_command(InputBuffer* input_buffer){
if(strcmp(input_buffer->buffer,".exit")==0){
close_input_buffer(input_buffer);
printf("Have a good day. Goodbye.\n");
exit(EXIT_SUCCESS);
}
else{
return META_COMMAND_UNRECOGNIZED_COMMAND;
}
}
PrepareResult prepare_statement(InputBuffer* input_buffer, Statement* statement){
if(strncmp(input_buffer->buffer,"insert",6)==0){
statement->type = STATEMENT_INSERT;
return PREPARE_SUCCESS;
}
if(strncmp(input_buffer->buffer,"select",6)==0){ //strncmp compares the first n characters of the input string which is the first parameter to the give const char * viz the second parameter
statement->type = STATEMENT_SELECT;
return PREPARE_SUCCESS;
}
return PREPARE_UNRECOGNIZED_STATEMENT;
}
void execute_statement(Statement* statement){ //going to act like our VM
switch(statement->type){
case(STATEMENT_INSERT):
printf("This is where we would do an insert.\n");
break;
case(STATEMENT_SELECT):
printf("This is where we would do an select.\n");
break;
}
}
void print_prompt(){
printf("karkidb >> ");
}
void read_input(InputBuffer* input_buffer){
ssize_t bytes_read = getline(&(input_buffer->buffer), &(input_buffer->buffer_length), stdin);
if(bytes_read<=0){
printf("Error reading input.\n");
exit(EXIT_FAILURE);
}
input_buffer->input_length = bytes_read - 1;
input_buffer->buffer[bytes_read - 1] = 0;
}
//*************************** START OF MAIN FUNCTION, REPL ******************************//
int main(){
InputBuffer* input_buffer = new_input_buffer();
while(1){
print_prompt();
read_input(input_buffer);
if(input_buffer->buffer[0]=='.'){
switch(do_meta_command(input_buffer)){
case(META_COMMAND_SUCCESS):
continue;
case(META_COMMAND_UNRECOGNIZED_COMMAND):
printf("Unrecognized command '%s'\n", input_buffer->buffer);
continue;
}
}
Statement statement;
switch(prepare_statement(input_buffer,&statement)){
case(PREPARE_SUCCESS):
break;
case(PREPARE_UNRECOGNIZED_STATEMENT):
printf("Unrecognized keyword at start of '%s'.\n",input_buffer->buffer);
continue;
}
execute_statement(&statement);
printf("One statement executed.\n");
}
}