Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| de0cad11b2 | |||
| 416d86ad63 |
@@ -0,0 +1,61 @@
|
|||||||
|
#include "lex.h"
|
||||||
|
|
||||||
|
#include "token.h"
|
||||||
|
|
||||||
|
#define LEX_TOKEN_DEF(i, n) {.id = (i), .name = (n)}
|
||||||
|
|
||||||
|
static struct lex_token_def keywords[] = {
|
||||||
|
LEX_TOKEN_DEF(KW_FUNC, "func"),
|
||||||
|
};
|
||||||
|
static const size_t nr_keywords = sizeof keywords / sizeof keywords[0];
|
||||||
|
|
||||||
|
static struct lex_token_def symbols[] = {
|
||||||
|
LEX_TOKEN_DEF(SYM_SQUOTE, "'"),
|
||||||
|
LEX_TOKEN_DEF(SYM_DQUOTE, "\""),
|
||||||
|
LEX_TOKEN_DEF(SYM_LEFT_BRACE, "{"),
|
||||||
|
LEX_TOKEN_DEF(SYM_RIGHT_BRACE, "}"),
|
||||||
|
LEX_TOKEN_DEF(SYM_LEFT_BRACKET, "["),
|
||||||
|
LEX_TOKEN_DEF(SYM_RIGHT_BRACKET, "]"),
|
||||||
|
LEX_TOKEN_DEF(SYM_LEFT_PAREN, "("),
|
||||||
|
LEX_TOKEN_DEF(SYM_RIGHT_PAREN, ")"),
|
||||||
|
};
|
||||||
|
static const size_t nr_symbols = sizeof symbols / sizeof symbols[0];
|
||||||
|
|
||||||
|
enum bshell_status lex_ctx_init(struct lex_ctx *ctx, struct line_source *src)
|
||||||
|
{
|
||||||
|
memset(ctx, 0x0, sizeof *ctx);
|
||||||
|
|
||||||
|
ctx->lex_status = BSHELL_SUCCESS;
|
||||||
|
ctx->lex_src = src;
|
||||||
|
|
||||||
|
return BSHELL_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum bshell_status lex_ctx_cleanup(struct lex_ctx *ctx)
|
||||||
|
{
|
||||||
|
return BSHELL_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct lex_token *dequeue_token(struct lex_ctx *ctx)
|
||||||
|
{
|
||||||
|
fx_queue_entry *entry = fx_queue_first(&ctx->lex_tokens);
|
||||||
|
return fx_unbox(struct lex_token, entry, tok_entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
static enum bshell_status pump_tokens(struct lex_ctx *ctx)
|
||||||
|
{
|
||||||
|
return BSHELL_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct lex_token *lex_ctx_peek(struct lex_ctx *ctx)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void lex_ctx_advance(struct lex_ctx *ctx)
|
||||||
|
{
|
||||||
|
struct lex_token *tok = dequeue_token(ctx);
|
||||||
|
if (tok) {
|
||||||
|
lex_token_destroy(tok);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef LEX_H_
|
||||||
|
#define LEX_H_
|
||||||
|
|
||||||
|
#include "status.h"
|
||||||
|
|
||||||
|
#include <fx/queue.h>
|
||||||
|
|
||||||
|
struct lex_token;
|
||||||
|
struct line_source;
|
||||||
|
|
||||||
|
struct lex_token_def {
|
||||||
|
int id;
|
||||||
|
const char *name;
|
||||||
|
uint64_t name_hash;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct lex_ctx {
|
||||||
|
fx_queue lex_tokens;
|
||||||
|
struct line_source *lex_src;
|
||||||
|
enum bshell_status lex_status;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern enum bshell_status lex_ctx_init(
|
||||||
|
struct lex_ctx *ctx,
|
||||||
|
struct line_source *src);
|
||||||
|
extern enum bshell_status lex_ctx_cleanup(struct lex_ctx *ctx);
|
||||||
|
|
||||||
|
extern struct lex_token *lex_ctx_peek(struct lex_ctx *ctx);
|
||||||
|
extern void lex_ctx_advance(struct lex_ctx *ctx);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
#ifndef IVY_LANG_LEX_H_
|
||||||
|
#define IVY_LANG_LEX_H_
|
||||||
|
|
||||||
|
#include <fx/queue.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
struct char_cell {
|
||||||
|
unsigned long c_row, c_col;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum token_type {
|
||||||
|
TOK_NONE = 0,
|
||||||
|
__TOK_INDEX_BASE = 100,
|
||||||
|
TOK_KEYWORD,
|
||||||
|
TOK_SYMBOL,
|
||||||
|
TOK_INT,
|
||||||
|
TOK_DOUBLE,
|
||||||
|
TOK_WORD,
|
||||||
|
TOK_ARG,
|
||||||
|
TOK_STRING,
|
||||||
|
TOK_STR_START,
|
||||||
|
TOK_STR_END,
|
||||||
|
TOK_LINEFEED,
|
||||||
|
__TOK_INDEX_LIMIT,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum token_keyword {
|
||||||
|
KW_NONE = 0,
|
||||||
|
__KW_INDEX_BASE = 200,
|
||||||
|
KW_FUNC,
|
||||||
|
__KW_INDEX_LIMIT,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum token_symbol {
|
||||||
|
SYM_NONE = 0,
|
||||||
|
__SYM_INDEX_BASE = 300,
|
||||||
|
SYM_SQUOTE,
|
||||||
|
SYM_DQUOTE,
|
||||||
|
SYM_LEFT_BRACE,
|
||||||
|
SYM_RIGHT_BRACE,
|
||||||
|
SYM_LEFT_BRACKET,
|
||||||
|
SYM_RIGHT_BRACKET,
|
||||||
|
SYM_LEFT_PAREN,
|
||||||
|
SYM_RIGHT_PAREN,
|
||||||
|
__SYM_INDEX_LIMIT,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct lex_token {
|
||||||
|
enum token_type tok_type;
|
||||||
|
|
||||||
|
struct char_cell tok_start, tok_end;
|
||||||
|
|
||||||
|
fx_queue_entry tok_entry;
|
||||||
|
|
||||||
|
union {
|
||||||
|
enum token_keyword tok_keyword;
|
||||||
|
enum token_symbol tok_symbol;
|
||||||
|
long long tok_int;
|
||||||
|
double tok_double;
|
||||||
|
char *tok_str;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
extern struct lex_token *lex_token_create_discard(void);
|
||||||
|
extern struct lex_token *lex_token_create_ident(const char *s);
|
||||||
|
extern void lex_token_destroy(struct lex_token *tok);
|
||||||
|
|
||||||
|
static inline bool lex_token_is_symbol(
|
||||||
|
struct lex_token *tok,
|
||||||
|
enum token_symbol sym)
|
||||||
|
{
|
||||||
|
return (tok->tok_type == TOK_SYMBOL && tok->tok_symbol == sym);
|
||||||
|
}
|
||||||
|
static inline bool lex_token_is_keyword(
|
||||||
|
struct lex_token *tok,
|
||||||
|
enum token_keyword kw)
|
||||||
|
{
|
||||||
|
return (tok->tok_type == TOK_KEYWORD && tok->tok_keyword == kw);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern const char *lex_token_to_string(const struct lex_token *tok);
|
||||||
|
extern const char *bshell_lex_token_type_to_string(enum token_type type);
|
||||||
|
extern const char *token_keyword_to_string(enum token_keyword keyword);
|
||||||
|
extern const char *token_symbol_to_string(enum token_symbol sym);
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user