|
|
@@ -39,7 +39,7 @@ static int regmatch(regex_t reg, char* str, regmatch_t* pmatch) |
|
|
|
return (regexec(®, str, 1, pmatch, 0) != REG_NOMATCH); |
|
|
|
} |
|
|
|
|
|
|
|
static int tokens_append( |
|
|
|
static void tokens_append( |
|
|
|
lexer_tokens* tokens, |
|
|
|
lexer_token_class tokenClass, |
|
|
|
char* str, |
|
|
@@ -49,27 +49,36 @@ static int tokens_append( |
|
|
|
tokens->length += 1; |
|
|
|
if (tokens->length > tokens->allocd) |
|
|
|
{ |
|
|
|
printf("old allocd: %i\n", (int)(tokens->allocd)); |
|
|
|
if (tokens->allocd) |
|
|
|
tokens->allocd *= 2; |
|
|
|
else |
|
|
|
tokens->allocd = 1; |
|
|
|
|
|
|
|
printf("new allocd: %i\n", (int)(tokens->allocd)); |
|
|
|
|
|
|
|
tokens->pairs = realloc(tokens->pairs, tokens->allocd); |
|
|
|
if (!tokens->pairs) |
|
|
|
return 1; |
|
|
|
{ |
|
|
|
fprintf(stderr, "Allocation error."); |
|
|
|
exit(1); |
|
|
|
} |
|
|
|
puts("realloced"); |
|
|
|
} |
|
|
|
|
|
|
|
puts("appending"); |
|
|
|
lexer_token* pair = &(tokens->pairs[tokens->length - 1]); |
|
|
|
pair->tokenClass = tokenClass; |
|
|
|
pair->str = str; |
|
|
|
pair->len = len; |
|
|
|
struct lexer_token pair = |
|
|
|
{ |
|
|
|
tokenClass, |
|
|
|
str, |
|
|
|
len |
|
|
|
}; |
|
|
|
|
|
|
|
printf("class: %i, pair length: %i, tokens length: %i, allocd: %i, str: %s\n", pair->tokenClass, (int)(pair->len), (int)(tokens->length), (int)(tokens->length), pair->str); |
|
|
|
printf("appending to %i\n", (int)(tokens->length - 1)); |
|
|
|
tokens->pairs[tokens->length - 1] = pair; |
|
|
|
puts("appended"); |
|
|
|
|
|
|
|
puts("bye\n"); |
|
|
|
return 0; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
lexer_tokens* lexer_analyze(char* str) |
|
|
@@ -80,19 +89,19 @@ lexer_tokens* lexer_analyze(char* str) |
|
|
|
|
|
|
|
struct token_reg tregs[] = |
|
|
|
{ |
|
|
|
regex("\\s+", LEXER_TOKEN_WHITESPACE), |
|
|
|
regex("[a-zA-Z][a-zA-Z0-9]*", LEXER_TOKEN_IDENTIFIER), |
|
|
|
regex("if|ret|func|str|arr|err|null", LEXER_TOKEN_KEYWORD), |
|
|
|
regex("=|==", LEXER_TOKEN_OPERATOR), |
|
|
|
regex("\\,", LEXER_TOKEN_SEPARATOR), |
|
|
|
|
|
|
|
regex("[0-9]+", LEXER_TOKEN_INTEGER), |
|
|
|
regex("\\\"[^\\\"]*\\\"", LEXER_TOKEN_STRING), |
|
|
|
|
|
|
|
regex("\\{", LEXER_TOKEN_FUNCTION_START), |
|
|
|
regex("\\}", LEXER_TOKEN_FUNCTION_END), |
|
|
|
regex("\\(", LEXER_TOKEN_EXPRESSION_START), |
|
|
|
regex("\\)", LEXER_TOKEN_EXPRESSION_END) |
|
|
|
regex(LEXER_REGEX_WHITESPACE, LEXER_TOKEN_WHITESPACE), |
|
|
|
regex(LEXER_REGEX_IDENTIFIER, LEXER_TOKEN_IDENTIFIER), |
|
|
|
regex(LEXER_REGEX_KEYWORD, LEXER_TOKEN_KEYWORD), |
|
|
|
regex(LEXER_REGEX_OPERATOR, LEXER_TOKEN_OPERATOR), |
|
|
|
|
|
|
|
regex(LEXER_REGEX_INTEGER, LEXER_TOKEN_INTEGER), |
|
|
|
regex(LEXER_REGEX_STRING, LEXER_TOKEN_STRING), |
|
|
|
|
|
|
|
regex(LEXER_REGEX_COMMA, LEXER_TOKEN_COMMA), |
|
|
|
regex(LEXER_REGEX_OPENBRACE, LEXER_TOKEN_OPENBRACE), |
|
|
|
regex(LEXER_REGEX_CLOSEBRACE, LEXER_TOKEN_CLOSEBRACE), |
|
|
|
regex(LEXER_REGEX_OPENPAREN, LEXER_TOKEN_OPENPAREN), |
|
|
|
regex(LEXER_REGEX_CLOSEPAREN, LEXER_TOKEN_CLOSEPAREN) |
|
|
|
}; |
|
|
|
|
|
|
|
lexer_tokens* tokens = malloc(sizeof(lexer_tokens)); |