slr

S Lang Runtime
git clone https://codeberg.org/night0721/slr
Log | Files | Refs | README | LICENSE

lexer.c (3236B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <ctype.h>
      5 
      6 #include "lexer.h"
      7 
      8 Token *lexer(char *buf)
      9 {
     10     char *current = strdup(buf);
     11     int current_index = 0, tokens_index = 0;
     12 
     13     Token *tokens = malloc(sizeof(Token) * 1024);
     14     while(current[current_index] != '\0') {
     15         Token *token = NULL;
     16         if(current[current_index] == ';') {
     17 			token = generate_separator(current, &current_index);
     18             tokens[tokens_index] = *token;
     19             tokens_index++;
     20         } else if(current[current_index] == '(') {
     21             token = generate_separator(current, &current_index);
     22             tokens[tokens_index] = *token;
     23             tokens_index++;
     24         } else if(current[current_index] == ')') {
     25 			token = generate_separator(current, &current_index);
     26             tokens[tokens_index] = *token;
     27             tokens_index++;
     28         } else if(isdigit(current[current_index])) {
     29             token = generate_number(current, &current_index); 
     30             tokens[tokens_index] = *token;
     31             tokens_index++;
     32             current_index--;
     33         } else if(isalpha(current[current_index])) {
     34             token = generate_keyword(current, &current_index);
     35             tokens[tokens_index] = *token;
     36             tokens_index++;
     37             current_index--;
     38         }
     39         current_index++;
     40     }
     41     tokens[tokens_index].type = END_OF_TOKENS;
     42     tokens[tokens_index].value = '\0';
     43     return tokens;
     44 }
     45 
     46 void print_token(Token token)
     47 {
     48   printf("Token Value: \"");
     49   for (int i = 0; token.value[i] != '\0'; i++) {
     50     printf("%c", token.value[i]);
     51   }
     52   printf("\"\nToken Type: ");
     53   switch (token.type) {
     54       case INT:
     55           printf("int\n");
     56           break;
     57       case KEYWORD:
     58           printf("Keyword\n");
     59           break;
     60       case SEPARATOR:
     61           printf("Separator\n");
     62           break;
     63 	  case END_OF_TOKENS:
     64 		  printf("End of Tokens\n");
     65 		  break;
     66   }
     67 }
     68 
     69 Token *generate_separator(char *current, int *current_index)
     70 {
     71   Token *token = malloc(sizeof(Token));
     72   token->value = malloc(sizeof(char) * 2);
     73   token->value[0] = current[*current_index];
     74   token->value[1] = '\0';
     75   token->type = SEPARATOR;
     76   return token;
     77 }
     78 
     79 Token *generate_number(char *current, int *current_index)
     80 {
     81     Token *token = malloc(sizeof(Token));
     82     token->type = INT;
     83     char *value = malloc(8 * sizeof(char));
     84     int value_index = 0;
     85     while(isdigit(current[*current_index]) && current[*current_index] != '\0') {
     86         value[value_index] = current[*current_index];
     87         value_index++;
     88         (*current_index)++;
     89     }
     90     value[value_index] = '\0';
     91     token->value = value;
     92     return token;
     93 }
     94 
     95 Token *generate_keyword(char *current, int *current_index)
     96 {
     97     Token *token = malloc(sizeof(Token));
     98     char *keyword = malloc(8 * sizeof(char));
     99     int keyword_index = 0;
    100     while(isalpha(current[*current_index]) && current[*current_index] != '\0') {
    101         keyword[keyword_index] = current[*current_index];
    102         keyword_index++;
    103         (*current_index)++;
    104     }
    105     keyword[keyword_index] = '\0';
    106     if(strcmp(keyword, "exit") == 0) {
    107         printf("TYPE EXIT\n");
    108         token->type = KEYWORD;
    109     }
    110     token->type = KEYWORD;
    111     token->value = keyword;
    112     return token;
    113 }