Reorganise
This commit is contained in:
		
							
								
								
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,4 +1,2 @@ | ||||
| *.dSYM | ||||
| fsm | ||||
| fsm2 | ||||
| fsm3 | ||||
| src/ignore/**/* | ||||
|   | ||||
							
								
								
									
										167
									
								
								fsm.c
									
									
									
									
									
								
							
							
						
						
									
										167
									
								
								fsm.c
									
									
									
									
									
								
							| @@ -1,167 +0,0 @@ | ||||
| #include <ctype.h> | ||||
| #include <stdbool.h> | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| #define ARR_LEN(X) (sizeof(X) / sizeof(X[0])) | ||||
|  | ||||
| typedef enum { | ||||
|   START_STATE, | ||||
|   DECIMAL_STATE, | ||||
|   NUM_STATE, | ||||
|   FRACTION_STATE, | ||||
|   EXPONENT_STATE, | ||||
|   EXP_SIGN_STATE, | ||||
|   POWER_STATE, | ||||
|   END_STATE, | ||||
|   INVALID_STATE, | ||||
| } states; | ||||
|  | ||||
| states handle_start(char input); | ||||
| states handle_decimal(char input); | ||||
| states handle_num(char input); | ||||
| states handle_fraction(char input); | ||||
| states handle_exponent(char input); | ||||
| states handle_exp_sign(char input); | ||||
| states handle_power(char input); | ||||
|  | ||||
| states state_machine(states current, char input); | ||||
|  | ||||
| bool check_number(const char *num); | ||||
|  | ||||
| const char *inputs[] = { | ||||
|     "0000000", // INVALID | ||||
|     "01", // INVALID | ||||
|     "f0", // INVALID | ||||
|     "1f", // INVALID | ||||
|     "1.f", // INVALID | ||||
|     "1.0f", // INVALID | ||||
|     "8987", // VALID | ||||
|     "9.5", // VALID | ||||
|     "    7", // VALID | ||||
|     "  7.8", // VALID | ||||
|     "732.9834e",   // INVALID | ||||
|     "732.9834e-",  // INVALID | ||||
|     "732.9834e+",  // INVALID | ||||
|     "732.9834e-g", // INVALID | ||||
|     "732.9834e-8", // VALID | ||||
|     "732.9834e+8", // VALID | ||||
| }; | ||||
|  | ||||
| int main(int argc, char *argv[]) { | ||||
|   for (int i = 0; i < ARR_LEN(inputs); ++i) { | ||||
|     printf("%s is %s\n", inputs[i], | ||||
|            check_number(inputs[i]) ? "VALID" : "INVALID"); | ||||
|   } | ||||
|  | ||||
|   return EXIT_SUCCESS; | ||||
| } | ||||
|  | ||||
| states handle_start(char input) { | ||||
|   if (isspace(input)) { | ||||
|     return START_STATE; | ||||
|   } else if (input == '0') { | ||||
|     return DECIMAL_STATE; | ||||
|   } else if (isdigit(input)) { | ||||
|     return NUM_STATE; | ||||
|   } | ||||
|  | ||||
|   return INVALID_STATE; | ||||
| } | ||||
|  | ||||
| states handle_decimal(char input) { | ||||
|   if (input == '.') { | ||||
|     return FRACTION_STATE; | ||||
|   } | ||||
|  | ||||
|   return INVALID_STATE; | ||||
| } | ||||
|  | ||||
| states handle_num(char input) { | ||||
|   if (isdigit(input)) { | ||||
|     return NUM_STATE; | ||||
|   } else if (input == '.') { | ||||
|     return FRACTION_STATE; | ||||
|   } else if (isspace(input)) { | ||||
|     return END_STATE; | ||||
|   } | ||||
|  | ||||
|   return INVALID_STATE; | ||||
| } | ||||
|  | ||||
| states handle_fraction(char input) { | ||||
|   if (isdigit(input)) { | ||||
|     return FRACTION_STATE; | ||||
|   } else if (isspace(input)) { | ||||
|     return END_STATE; | ||||
|   } else if (input == 'e' || input == 'E') { | ||||
|     return EXPONENT_STATE; | ||||
|   } | ||||
|  | ||||
|   return INVALID_STATE; | ||||
| } | ||||
|  | ||||
| states handle_exponent(char input) { | ||||
|   if (isdigit(input)) { | ||||
|     return POWER_STATE; | ||||
|   } else if (input == '+' || input == '-') { | ||||
|     return EXP_SIGN_STATE; | ||||
|   } | ||||
|  | ||||
|   return INVALID_STATE; | ||||
| } | ||||
|  | ||||
| states handle_exp_sign(char input) { | ||||
|   if (isdigit(input)) { | ||||
|     return POWER_STATE; | ||||
|   } | ||||
|  | ||||
|   return INVALID_STATE; | ||||
| } | ||||
|  | ||||
| states handle_power(char input) { | ||||
|   if (isdigit(input)) { | ||||
|     return POWER_STATE; | ||||
|   } else if (isspace(input)) { | ||||
|     return END_STATE; | ||||
|   } | ||||
|  | ||||
|   return INVALID_STATE; | ||||
| } | ||||
|  | ||||
| states state_machine(states current, char input) { | ||||
|   switch (current) { | ||||
|   case START_STATE: | ||||
|     return handle_start(input); | ||||
|   case DECIMAL_STATE: | ||||
|     return handle_decimal(input); | ||||
|   case NUM_STATE: | ||||
|     return handle_num(input); | ||||
|   case FRACTION_STATE: | ||||
|     return handle_fraction(input); | ||||
|   case EXPONENT_STATE: | ||||
|     return handle_exponent(input); | ||||
|   case EXP_SIGN_STATE: | ||||
|     return handle_exp_sign(input); | ||||
|   case POWER_STATE: | ||||
|     return handle_power(input); | ||||
|   case END_STATE: | ||||
|   case INVALID_STATE: | ||||
|     return current; | ||||
|   } | ||||
| } | ||||
|  | ||||
| bool check_number(const char *num) { | ||||
|   states current = START_STATE; | ||||
|  | ||||
|   for (const char *c = &(num[0]); *c != '\0'; ++c) { | ||||
|     current = state_machine(current, *c); | ||||
|  | ||||
|     if (current == INVALID_STATE) { | ||||
|       return false; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   return current == END_STATE || current == NUM_STATE || | ||||
|          current == FRACTION_STATE || current == POWER_STATE; | ||||
| } | ||||
							
								
								
									
										108
									
								
								fsm2.c
									
									
									
									
									
								
							
							
						
						
									
										108
									
								
								fsm2.c
									
									
									
									
									
								
							| @@ -1,108 +0,0 @@ | ||||
| #include <ctype.h> | ||||
| #include <stdbool.h> | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| #define ARR_LEN(X) (sizeof(X) / sizeof(X[0])) | ||||
|  | ||||
| typedef enum { | ||||
|   STATE_START, | ||||
|   STATE_INVALID, | ||||
|   STATE_OBJ, | ||||
|   STATE_ARR, | ||||
|   STATE_END, | ||||
| } states; | ||||
|  | ||||
| states handle_start(char input); | ||||
| states handle_obj(char input); | ||||
| states handle_arr(char input); | ||||
|  | ||||
| states state_machine(states current, char input); | ||||
|  | ||||
| bool check_input(const char *text); | ||||
|  | ||||
| // clang-format off | ||||
| const char *inputs[] = { | ||||
| 	"{}", | ||||
| 	"[]", | ||||
| 	"{           }", | ||||
| 	"[         ]", | ||||
| 	"           {}", | ||||
| 	"           []", | ||||
| 	"{}              ", | ||||
| 	"[]              ", | ||||
| 	"{", | ||||
| 	"[", | ||||
| 	"}", | ||||
| 	"]", | ||||
| 	"{f}", | ||||
| 	"[8]", | ||||
| }; | ||||
| // clang-format on | ||||
|  | ||||
| int main(int argc, char *argv[]) { | ||||
|   for (int i = 0; i < ARR_LEN(inputs); ++i) { | ||||
|     printf("%20s is %s\n", inputs[i], | ||||
|            check_input(inputs[i]) ? "VALID" : "INVALID"); | ||||
|   } | ||||
|  | ||||
|   return EXIT_SUCCESS; | ||||
| } | ||||
|  | ||||
| bool check_input(const char *text) { | ||||
|   states current = STATE_START; | ||||
|  | ||||
|   for (const char *c = &(text[0]); *c != '\0'; ++c) { | ||||
|     current = state_machine(current, *c); | ||||
|   } | ||||
|  | ||||
|   return current == STATE_END; | ||||
| } | ||||
|  | ||||
| states handle_start(char input) { | ||||
|   if (isspace(input)) { | ||||
|     return STATE_START; | ||||
|   } | ||||
|  | ||||
|   switch (input) { | ||||
|   case '{': | ||||
|     return STATE_OBJ; | ||||
|   case '[': | ||||
|     return STATE_ARR; | ||||
|   default: | ||||
|     return STATE_INVALID; | ||||
|   } | ||||
| } | ||||
|  | ||||
| states handle_obj(char input) { | ||||
|   if (isspace(input)) { | ||||
|     return STATE_OBJ; | ||||
|   } else if (input == '}') { | ||||
|     return STATE_END; | ||||
|   } | ||||
|  | ||||
|   return STATE_INVALID; | ||||
| } | ||||
|  | ||||
| states handle_arr(char input) { | ||||
|   if (isspace(input)) { | ||||
|     return STATE_ARR; | ||||
|   } else if (input == ']') { | ||||
|     return STATE_END; | ||||
|   } | ||||
|  | ||||
|   return STATE_INVALID; | ||||
| } | ||||
|  | ||||
| states state_machine(states current, char input) { | ||||
|   switch (current) { | ||||
|   case STATE_START: | ||||
|     return handle_start(input); | ||||
|   case STATE_OBJ: | ||||
|     return handle_obj(input); | ||||
|   case STATE_ARR: | ||||
|     return handle_arr(input); | ||||
|   default: | ||||
|     return current; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										110
									
								
								fsm3.c
									
									
									
									
									
								
							
							
						
						
									
										110
									
								
								fsm3.c
									
									
									
									
									
								
							| @@ -1,110 +0,0 @@ | ||||
| #include <ctype.h> | ||||
| #include <stdbool.h> | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| #define ARR_LEN(X) (sizeof(X) / sizeof(X[0])) | ||||
|  | ||||
| typedef enum { | ||||
|   STATE_START, | ||||
|   STATE_INVALID, | ||||
|   STATE_KEY, | ||||
|   STATE_STRING, | ||||
|   STATE_KEY_END, | ||||
|   STATE_END, | ||||
| } states; | ||||
|  | ||||
| typedef states (*state_handler)(char input); | ||||
|  | ||||
| states handle_start(char input); | ||||
| states handle_key(char input); | ||||
| states handle_string(char input); | ||||
| states handle_key_end(char input); | ||||
|  | ||||
| states state_machine(states current, char input); | ||||
|  | ||||
| bool check_input(const char *text); | ||||
|  | ||||
| // clang-format off | ||||
| const char *inputs[] = { | ||||
| 	"", | ||||
| 	"{\"kjehu efhkwejhf wkehjf iyg\"}", | ||||
| 	"{", | ||||
| 	"}", | ||||
| 	"{\"lkhefhkjhefhjjrj}", | ||||
| 	"{jhejhuef hkwehj fwe}", | ||||
| 	"{'wuy iygef'}", | ||||
| 	"{\"hwekhewf'}", | ||||
| }; | ||||
| // clang-format on | ||||
|  | ||||
| int main(int argc, char *argv[]) { | ||||
|   for (int i = 0; i < ARR_LEN(inputs); ++i) { | ||||
|     printf("%32s is %s\n", inputs[i], | ||||
|            check_input(inputs[i]) ? "VALID" : "INVALID"); | ||||
|   } | ||||
|  | ||||
|   return EXIT_SUCCESS; | ||||
| } | ||||
|  | ||||
| bool check_input(const char *text) { | ||||
|   states current = STATE_START; | ||||
|  | ||||
|   for (const char *c = &(text[0]); *c != '\0'; ++c) { | ||||
|     current = state_machine(current, *c); | ||||
|   } | ||||
|  | ||||
|   return current == STATE_END; | ||||
| } | ||||
|  | ||||
| states state_machine(states current, char input) { | ||||
|   switch (current) { | ||||
|   case STATE_START: | ||||
|     return handle_start(input); | ||||
|   case STATE_KEY: | ||||
|     return handle_key(input); | ||||
|   case STATE_STRING: | ||||
|     return handle_string(input); | ||||
|   case STATE_KEY_END: | ||||
|     return handle_key_end(input); | ||||
|   case STATE_INVALID: | ||||
|   case STATE_END: | ||||
|     return current; | ||||
|   } | ||||
| } | ||||
|  | ||||
| states handle_start(char input) { | ||||
|   if (isspace(input)) { | ||||
|     return STATE_START; | ||||
|   } else if (input == '{') { | ||||
|     return STATE_KEY; | ||||
|   } | ||||
|  | ||||
|   return STATE_INVALID; | ||||
| } | ||||
|  | ||||
| states handle_key(char input) { | ||||
|   if (isspace(input)) { | ||||
|     return STATE_KEY; | ||||
|   } else if (input == '"') { | ||||
|     return STATE_STRING; | ||||
|   } | ||||
|  | ||||
|   return STATE_INVALID; | ||||
| } | ||||
|  | ||||
| states handle_string(char input) { | ||||
|   if (input == '"') { | ||||
|     return STATE_KEY_END; | ||||
|   } | ||||
|  | ||||
|   return STATE_STRING; | ||||
| } | ||||
|  | ||||
| states handle_key_end(char input) { | ||||
|   if (input == '}') { | ||||
|     return STATE_END; | ||||
|   } | ||||
|  | ||||
|   return STATE_INVALID; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user