|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <ctype.h> |
| 4 | +#include "token.h" |
| 5 | + |
| 6 | +static char *st_line; |
| 7 | +static int st_line_pos; |
| 8 | + |
| 9 | +typedef enum { |
| 10 | + INITIAL_STATUS, |
| 11 | + IN_INT_PART_STATUS, |
| 12 | + DOT_STATUS, |
| 13 | + IN_FRAC_PART_STATUS |
| 14 | +} LexerStatus; |
| 15 | + |
| 16 | +void |
| 17 | +get_token(Token *token) |
| 18 | +{ |
| 19 | + int out_pos = 0; |
| 20 | + LexerStatus status = INITIAL_STATUS; |
| 21 | + char current_char; |
| 22 | + |
| 23 | + token->kind = BAD_TOKEN; |
| 24 | + while (st_line[st_line_pos] != '\0') { |
| 25 | + current_char = st_line[st_line_pos]; |
| 26 | + if ((status == IN_INT_PART_STATUS || status == IN_FRAC_PART_STATUS) |
| 27 | + && !isdigit(current_char) && current_char != '.') { |
| 28 | + token->kind = NUMBER_TOKEN; |
| 29 | + sscanf(token->str, "%lf", &token->value); |
| 30 | + return; |
| 31 | + } |
| 32 | + if (isspace(current_char)) { |
| 33 | + if (current_char == '\n') { |
| 34 | + token->kind = END_OF_LINE_TOKEN; |
| 35 | + return; |
| 36 | + } |
| 37 | + st_line_pos++; |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + if (out_pos >= MAX_TOKEN_SIZE-1) { |
| 42 | + fprintf(stderr, "token too long.\n"); |
| 43 | + exit(1); |
| 44 | + } |
| 45 | + token->str[out_pos] = st_line[st_line_pos]; |
| 46 | + st_line_pos++; |
| 47 | + out_pos++; |
| 48 | + token->str[out_pos] = '\0'; |
| 49 | + |
| 50 | + if (current_char == '+') { |
| 51 | + token->kind = ADD_OPERATOR_TOKEN; |
| 52 | + return; |
| 53 | + } else if (current_char == '-') { |
| 54 | + token->kind = SUB_OPERATOR_TOKEN; |
| 55 | + return; |
| 56 | + } else if (current_char == '*') { |
| 57 | + token->kind = MUL_OPERATOR_TOKEN; |
| 58 | + return; |
| 59 | + } else if (current_char == '/') { |
| 60 | + token->kind = DIV_OPERATOR_TOKEN; |
| 61 | + return; |
| 62 | + } else if (current_char == '(') { |
| 63 | + token->kind = LEFT_PAREN_TOKEN; |
| 64 | + return; |
| 65 | + } else if (current_char == ')') { |
| 66 | + token->kind = RIGHT_PAREN_TOKEN; |
| 67 | + return; |
| 68 | + } else if (isdigit(current_char)) { |
| 69 | + if (status == INITIAL_STATUS) { |
| 70 | + status = IN_INT_PART_STATUS; |
| 71 | + } else if (status == DOT_STATUS) { |
| 72 | + status = IN_FRAC_PART_STATUS; |
| 73 | + } |
| 74 | + } else if (current_char == '.') { |
| 75 | + if (status == IN_INT_PART_STATUS) { |
| 76 | + status = DOT_STATUS; |
| 77 | + } else { |
| 78 | + fprintf(stderr, "syntax error.\n"); |
| 79 | + exit(1); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void |
| 86 | +set_line(char *line) |
| 87 | +{ |
| 88 | + st_line = line; |
| 89 | + st_line_pos = 0; |
| 90 | +} |
| 91 | + |
| 92 | +#if 0 |
| 93 | +void |
| 94 | +parse_line(void) |
| 95 | +{ |
| 96 | + Token token; |
| 97 | + st_line_pos = 0; |
| 98 | + |
| 99 | + for (;;) { |
| 100 | + get_token(&token); |
| 101 | + if (token.kind == END_OF_LINE_TOKEN) { |
| 102 | + break; |
| 103 | + } else { |
| 104 | + printf("kind..%d, str..%s\n", token.kind, token.str); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +int |
| 110 | +main(int argc, char **argv) |
| 111 | +{ |
| 112 | + while (fgets(st_line, LINE_BUF_SIZE, stdin) != NULL) { |
| 113 | + parse_line(); |
| 114 | + } |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
| 118 | +#endif |
0 commit comments