add test program

This commit is contained in:
Simon 2023-02-12 15:03:19 +01:00
parent 4581d94d4e
commit 84be2cbbba
3 changed files with 125 additions and 2 deletions

113
lexer.c
View File

@ -1,6 +1,7 @@
#include "lexer.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -347,3 +348,115 @@ void lexer_step(Lexer* lexer)
}
lexer->index += 1;
}
char* token_string(const Token* token, const char* text)
{
char* value = calloc(token->length + 1, sizeof(char));
strncpy(value, &text[token->position.index], token->length);
return value;
}
char* token_to_string(const Token* token, const char* text)
{
const char* type_string = token_type_to_string(token->type);
char* value_string = token_string(token, text);
size_t size = token->length + strlen(type_string) + 5;
char* value = calloc(size, sizeof(char));
snprintf(value, size, "(%s, %s)", type_string, value_string);
free(value_string);
return value;
}
const char* token_type_to_string(TokenType type)
{
switch (type) {
case TokenTypeEof:
return "Eof";
case TokenTypeInvalidChar:
return "InvalidChar";
case TokenTypeMalformedMultilineComment:
return "MalformedMultilineComment";
case TokenTypeMalformedChar:
return "MalformedChar";
case TokenTypeMalformedString:
return "MalformedString";
case TokenTypeId:
return "Id";
case TokenTypeInt:
return "Int";
case TokenTypeHex:
return "Hex";
case TokenTypeBinary:
return "Binary";
case TokenTypeFloat:
return "Float";
case TokenTypeChar:
return "Char";
case TokenTypeString:
return "String";
case TokenTypeIf:
return "If";
case TokenTypeElse:
return "Else";
case TokenTypeWhile:
return "While";
case TokenTypeBreak:
return "Break";
case TokenTypeLParen:
return "LParen";
case TokenTypeRParen:
return "RParen";
case TokenTypeLBrace:
return "LBrace";
case TokenTypeRBrace:
return "RBrace";
case TokenTypeLBracket:
return "LBracket";
case TokenTypeRBracket:
return "RBracket";
case TokenTypeDot:
return "Dot";
case TokenTypeComma:
return "Comma";
case TokenTypeColon:
return "Colon";
case TokenTypeSemicolon:
return "Semicolon";
case TokenTypePlusEqual:
return "PlusEqual";
case TokenTypeMinusEqual:
return "MinusEqual";
case TokenTypeAsteriskEqual:
return "AsteriskEqual";
case TokenTypeSlashEqual:
return "SlashEqual";
case TokenTypePercentEqual:
return "PercentEqual";
case TokenTypeDoubleEqual:
return "DoubleEqual";
case TokenTypeExclamationEqual:
return "ExclamationEqual";
case TokenTypeLtEqual:
return "LtEqual";
case TokenTypeGtEqual:
return "GtEqual";
case TokenTypePlus:
return "Plus";
case TokenTypeMinus:
return "Minus";
case TokenTypeAsterisk:
return "Asterisk";
case TokenTypeSlash:
return "Slash";
case TokenTypePercent:
return "Percent";
case TokenTypeEqual:
return "Equal";
case TokenTypeExclamation:
return "Exclamation";
case TokenTypeLt:
return "Lt";
case TokenTypeGt:
return "Gt";
}
}

View File

@ -54,9 +54,10 @@ typedef enum {
TokenTypeExclamation,
TokenTypeLt,
TokenTypeGt,
} TokenType;
const char* token_type_to_string(TokenType type);
typedef struct {
size_t index;
int line, column;
@ -68,6 +69,9 @@ typedef struct {
size_t length;
} Token;
char* token_string(const Token* token, const char* text);
char* token_to_string(const Token* token, const char* text);
typedef struct Lexer Lexer;
void lexer_create(Lexer* lexer, const char* text, size_t text_length);

8
main.c
View File

@ -1,3 +1,9 @@
#include <stdio.h>
int main(void) { printf("hello world\n"); }
int main(void)
{
char text[]
= "abc 123 0xFF 0b101 3.14 'a' '\\n' \"hello\" \"world\\\"\\n\" if else /* /* while */ */ "
"while break (){}[].,:; += -= *= /= %= == != <= >= + - * / % % = ! < >";
}