Beginnings of the lexer.

This commit is contained in:
Jacob Signorovitch 2024-10-02 17:57:04 -04:00
parent d95c134a54
commit ce25c5fe9f
4 changed files with 85 additions and 9 deletions

View File

@ -1,12 +1,45 @@
#ifndef LEXER_H
#define LEXER_H
#include <stdlib.h>
#include <string.h>
#include "token.h"
// Lexer: converts text to tokens.
typedef struct Lexer {
char* src;
// What the lexer is currently looking at.
typedef enum {
LEXER_STATE_CONFUSED, // Can't decide what it's looking at (also initial
// state).
LEXER_STATE_NUM, // Looking at a number.
LEXER_STATE_CALL, // Looking at a call.
} LexerState;
} lexer_t;
// Lexer: converts text to tokens.
typedef struct {
char* src; // The source text.
size_t srcl; // The number of source chars.
char* cchar; // The current character.
Token** tokens; // The tokens produced.
size_t ntokens; // The number of tokens.
LexerState state; // What the lexxer is looking at.
} Lexer;
// Create a lexer.
Lexer* lexer_init(char* src);
// Destroy a lexer.
void lexer_destroy(Lexer* lexer);
// Lex in confused mode.
void lexer_do_confused(Lexer* lexer);
// Lex in number mode.
void lexer_do_number(Lexer* lexer);
// Lex in call mode.
void lexer_do_call(Lexer* lexer);
// Convert text to tokens.
void lexer_lex(Lexer* lexer);
#endif

View File

@ -1,13 +1,13 @@
#ifndef TOKEN_H
#define TOKEN_H
typedef enum TokenType {
typedef enum {
TOKEN_TYPE_CALL,
TOKEN_TYPE_NUMBER,
} TokenType;
// Token.
typedef struct Token {
typedef struct {
TokenType type; // The type of the Token.
char* val; // The text of the Token.
} Token;

44
src/lexer.c Normal file
View File

@ -0,0 +1,44 @@
#include "include/lexer.h"
Lexer* lexer_init(char* src) {
Lexer* lexer = malloc(sizeof(Lexer));
lexer->src = src;
lexer->srcl = strlen(src);
lexer->cchar = lexer->src;
lexer->tokens = NULL;
lexer->ntokens = 0;
lexer->state = LEXER_STATE_CONFUSED;
return lexer;
}
void lexer_destroy(Lexer *lexer) {
free(lexer->src);
for (
int i = 0;
i < lexer->ntokens;
token_destroy(lexer->tokens[i++])
);
}
void lexer_do_confused(Lexer *lexer) {
}
void lexer_lex(Lexer* lexer) {
while (*lexer->cchar) {
switch (lexer->state) {
case LEXER_STATE_CONFUSED:
lexer_do_confused(lexer);
break; case LEXER_STATE_NUM:
lexer_do_number(lexer);
break; case LEXER_STATE_CALL:
lexer_do_call(lexer);
break;
default: break;
}
}
}

View File

@ -9,8 +9,8 @@ void test_token_init() {
char* s = malloc(sizeof("Hello, world!"));
s = "Hello, world!";
Token* t = token_init(TOKEN_TYPE_CALL, s);
TEST_ASSERT_EQUAL(TOKEN_TYPE_NUMBER, t->type);
TEST_ASSERT_EQUAL_STRING("Hellso, world!", t->val);
TEST_ASSERT_EQUAL(TOKEN_TYPE_CALL, t->type);
TEST_ASSERT_EQUAL_STRING("Hello, world!", t->val);
}
void test_token_destroy() {
@ -23,7 +23,6 @@ void test_token_destroy() {
void token_test() {
UNITY_BEGIN();
RUN_TEST(test_token_init);
RUN_TEST(test_token_destroy);
UNITY_END();
}