C_TP_Forth/lexer.c
2022-12-08 06:11:40 +01:00

33 lines
718 B
C

#include "lexer.h"
#include <stdlib.h>
#include <string.h>
int numberOfDelimiters(char* string) {
int count = 0;
for (int i = 0; i < (int)strlen(string); ++i) {
if (string[i] == ' ') {
++count;
}
}
return count;
}
struct Programm* lexer(char* chaine) {
char *token, *str;
str = strdup(chaine);
int i = 0;
int arraysize = numberOfDelimiters(str) + 1;
char** programme = (char**)malloc(sizeof(char*)*arraysize);
while ((token = strsep(&str, " "))) {
programme[i] = token;
++i;
}
struct Programm* res = (struct Programm*) malloc(sizeof(struct Programm));
res->tokens = programme;
res->taille = i;
return res;
}