YREA-SLS/SLS_C/include/sls/lexer.h

127 lines
3.0 KiB
C

// Kyler Olsen
// YREA SLS
// Lexer Header
// October 2025
#ifndef SLS_LEXER_H
#define SLS_LEXER_H
#include <stdint.h>
#include "sls_errors.h"
typedef struct {
const char *filename;
const char *source_code;
size_t pos;
size_t column;
size_t line;
} LexerInfo;
typedef enum {
TOKEN_EOF,
TOKEN_IDENTIFIER,
TOKEN_INTEGER,
TOKEN_FLOAT,
TOKEN_DOUBLE,
TOKEN_STRING,
TOKEN_BOOLEAN,
TOKEN_ARRAY,
TOKEN_TOKEN_STRING,
TOKEN_TYPE_TUPLE,
} TokenType;
typedef struct {
const char *name;
size_t length;
Boolean is_literal;
} Identifier;
typedef enum {
INTEGER_I64,
INTEGER_I32,
INTEGER_I16,
INTEGER_I8,
INTEGER_U64,
INTEGER_U32,
INTEGER_U16,
INTEGER_U8,
} IntegerBuiltInType;
typedef struct {
uint64_t value;
IntegerBuiltInType type;
} IntegerLiteral;
typedef struct {
const char *value;
size_t length;
} StringLiteral;
typedef struct {
TokenType type;
union {
Identifier *identifiers; // type == TOKEN_IDENTIFIER
IntegerLiteral *integer_literals; // type == TOKEN_INTEGER
float *float_literals; // type == TOKEN_FLOAT
double *double_literals; // type == TOKEN_DOUBLE
StringLiteral *string_literals; // type == TOKEN_STRING
uint8_t *boolean_literals; // type == TOKEN_BOOLEAN
ArrayLiteral *array_literals; // type == TOKEN_ARRAY
TokenString *token_strings; // type == TOKEN_TOKEN_STRING
TypeTuple *type_tuples; // type == TOKEN_TYPE_TUPLE
};
size_t length;
} ArrayLiteral;
typedef struct {
const Token *tokens;
size_t length;
} TokenString;
typedef struct {
Identifier *input_identifiers;
size_t input_length;
Identifier *output_identifiers;
size_t output_length;
} TypeTuple;
typedef struct {
TokenType type;
union {
Identifier identifier; // type == TOKEN_IDENTIFIER
IntegerLiteral integer_literal; // type == TOKEN_INTEGER
float float_literal; // type == TOKEN_FLOAT
double double_literal; // type == TOKEN_DOUBLE
StringLiteral string_literal; // type == TOKEN_STRING
uint8_t boolean_literal; // type == TOKEN_BOOLEAN
ArrayLiteral array_literal; // type == TOKEN_ARRAY
TokenString token_string; // type == TOKEN_TOKEN_STRING
TypeTuple type_tuple; // type == TOKEN_TYPE_TUPLE
};
} Token;
typedef struct LexerTokenResult {
SlsResultType type;
union {
Token result; // type == SLS_RESULT
SlsError error; // type == SLS_ERROR
};
FileInfo file_info;
struct LexerTokenResult *next;
} LexerTokenResult;
typedef struct {
SlsResultType type;
union {
LexerTokenResult *result; // type == SLS_RESULT
SlsError error; // type == SLS_ERROR
};
} LexerResult;
void init_lexer(LexerInfo *lexer_info, const char *filename, const char *source_code);
LexerResult lexical_analysis(LexerInfo *lexer_info);
void clean_token_result(LexerTokenResult* head);
#endif // SLS_LEXER_H