Browse Source

ok, why is this segfaulting

master
mort 7 years ago
parent
commit
c4d8e6d46a
2 changed files with 50 additions and 27 deletions
  1. 31
    22
      src/lexer.c
  2. 19
    5
      src/lexer.h

+ 31
- 22
src/lexer.c View File

@@ -39,7 +39,7 @@ static int regmatch(regex_t reg, char* str, regmatch_t* pmatch)
return (regexec(&reg, 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));

+ 19
- 5
src/lexer.h View File

@@ -1,21 +1,35 @@
#ifndef LEXER_H
#define LEXER_H

#define LEXER_REGEX_WHITESPACE "\\s+"
#define LEXER_REGEX_IDENTIFIER "[a-zA-Z][a-zA-Z0-9]*"
#define LEXER_REGEX_KEYWORD "if|ret|func|str|arr|err|null"
#define LEXER_REGEX_OPERATOR "=|=="

#define LEXER_REGEX_INTEGER "[0-9]+"
#define LEXER_REGEX_STRING "\\\"[^\\\"]*\\\""

#define LEXER_REGEX_COMMA "\\,"
#define LEXER_REGEX_OPENBRACE "\\{"
#define LEXER_REGEX_CLOSEBRACE "\\}"
#define LEXER_REGEX_OPENPAREN "\\("
#define LEXER_REGEX_CLOSEPAREN "\\)"

typedef enum lexer_token_class
{
LEXER_TOKEN_WHITESPACE,
LEXER_TOKEN_IDENTIFIER,
LEXER_TOKEN_KEYWORD,
LEXER_TOKEN_OPERATOR,
LEXER_TOKEN_SEPARATOR,

LEXER_TOKEN_INTEGER,
LEXER_TOKEN_STRING,

LEXER_TOKEN_FUNCTION_START,
LEXER_TOKEN_FUNCTION_END,
LEXER_TOKEN_EXPRESSION_START,
LEXER_TOKEN_EXPRESSION_END,
LEXER_TOKEN_COMMA,
LEXER_TOKEN_OPENBRACE,
LEXER_TOKEN_CLOSEBRACE,
LEXER_TOKEN_OPENPAREN,
LEXER_TOKEN_CLOSEPAREN,

LEXER_TOKEN_NONE
} lexer_token_class;

Loading…
Cancel
Save