1
0
Fork 0

Move enums to other modules

This commit is contained in:
Alex Kotov 2023-05-05 13:50:24 +04:00
parent 8850290d64
commit 68b32c3799
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
10 changed files with 90 additions and 128 deletions

View File

@ -6,7 +6,6 @@ CFLAGS = -Wall -Wextra
OBJS = \
src/builtins.c.o \
src/ctype.c.o \
src/enums.c.o \
src/lexer.c.o \
src/main.c.o \
src/object.c.o \

View File

@ -1,71 +0,0 @@
#include "enums.h"
#include <stdbool.h>
#include <stddef.h>
const char *State_to_str(const enum State state)
{
switch (state) {
case STATE_INIT: return "STATE_INIT";
case STATE_WHITESPACE: return "STATE_WHITESPACE";
case STATE_ROUND_OPEN: return "STATE_ROUND_OPEN";
case STATE_ROUND_CLOSE: return "STATE_ROUND_CLOSE";
case STATE_SQUARE_OPEN: return "STATE_SQUARE_OPEN";
case STATE_SQUARE_CLOSE: return "STATE_SQUARE_CLOSE";
case STATE_CURLY_OPEN: return "STATE_CURLY_OPEN";
case STATE_CURLY_CLOSE: return "STATE_CURLY_CLOSE";
case STATE_QUOTE: return "STATE_QUOTE";
case STATE_IDENT: return "STATE_IDENT";
case STATE_NUM: return "STATE_NUM";
}
return NULL;
}
const char *TokenType_to_str(const enum TokenType token_type)
{
switch (token_type) {
case TOKEN_ROUND_OPEN: return "TOKEN_ROUND_OPEN";
case TOKEN_ROUND_CLOSE: return "TOKEN_ROUND_CLOSE";
case TOKEN_SQUARE_OPEN: return "TOKEN_SQUARE_OPEN";
case TOKEN_SQUARE_CLOSE: return "TOKEN_SQUARE_CLOSE";
case TOKEN_CURLY_OPEN: return "TOKEN_CURLY_OPEN";
case TOKEN_CURLY_CLOSE: return "TOKEN_CURLY_CLOSE";
case TOKEN_QUOTE: return "TOKEN_QUOTE";
case TOKEN_IDENT: return "TOKEN_IDENT";
case TOKEN_NUM: return "TOKEN_NUM";
}
return NULL;
}
const char *Type_to_str(const enum Type type)
{
switch (type) {
case TYPE_PROCEDURE: return "TYPE_PROCEDURE";
case TYPE_PAIR: return "TYPE_PAIR";
case TYPE_BOOLEAN: return "TYPE_BOOLEAN";
case TYPE_CHAR: return "TYPE_CHAR";
case TYPE_SYMBOL: return "TYPE_SYMBOL";
case TYPE_STRING: return "TYPE_STRING";
case TYPE_NUMBER: return "TYPE_NUMBER";
}
return NULL;
}
bool
State_to_token_type(const enum State state, enum TokenType *const token_type)
{
switch (state) {
case STATE_ROUND_OPEN: *token_type = TOKEN_ROUND_OPEN; break;
case STATE_ROUND_CLOSE: *token_type = TOKEN_ROUND_CLOSE; break;
case STATE_SQUARE_OPEN: *token_type = TOKEN_SQUARE_OPEN; break;
case STATE_SQUARE_CLOSE: *token_type = TOKEN_SQUARE_CLOSE; break;
case STATE_CURLY_OPEN: *token_type = TOKEN_CURLY_OPEN; break;
case STATE_CURLY_CLOSE: *token_type = TOKEN_CURLY_CLOSE; break;
case STATE_QUOTE: *token_type = TOKEN_QUOTE; break;
case STATE_IDENT: *token_type = TOKEN_IDENT; break;
case STATE_NUM: *token_type = TOKEN_NUM; break;
default: return false;
}
return true;
}

View File

@ -1,48 +0,0 @@
#ifndef __LISP_ENUMS_H__
#define __LISP_ENUMS_H__
#include <stdbool.h>
enum State {
STATE_INIT,
STATE_WHITESPACE,
STATE_ROUND_OPEN,
STATE_ROUND_CLOSE,
STATE_SQUARE_OPEN,
STATE_SQUARE_CLOSE,
STATE_CURLY_OPEN,
STATE_CURLY_CLOSE,
STATE_QUOTE,
STATE_IDENT,
STATE_NUM,
};
enum TokenType {
TOKEN_ROUND_OPEN,
TOKEN_ROUND_CLOSE,
TOKEN_SQUARE_OPEN,
TOKEN_SQUARE_CLOSE,
TOKEN_CURLY_OPEN,
TOKEN_CURLY_CLOSE,
TOKEN_QUOTE,
TOKEN_IDENT,
TOKEN_NUM,
};
enum Type {
TYPE_PROCEDURE,
TYPE_PAIR,
TYPE_BOOLEAN,
TYPE_CHAR,
TYPE_SYMBOL,
TYPE_STRING,
TYPE_NUMBER,
};
const char *State_to_str(enum State state);
const char *TokenType_to_str(enum TokenType token_type);
const char *Type_to_str(enum Type type);
bool State_to_token_type(enum State state, enum TokenType *token_type);
#endif

View File

@ -1,17 +1,31 @@
#include "lexer.h"
#include "ctype.h"
#include "enums.h"
#include "tokens.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
enum State {
STATE_INIT,
STATE_WHITESPACE,
STATE_ROUND_OPEN,
STATE_ROUND_CLOSE,
STATE_SQUARE_OPEN,
STATE_SQUARE_CLOSE,
STATE_CURLY_OPEN,
STATE_CURLY_CLOSE,
STATE_QUOTE,
STATE_IDENT,
STATE_NUM,
};
static char buffer[1024];
static size_t buffer_index = 0;
static enum State state = STATE_INIT;
static bool State_to_token_type(enum State state, enum TokenType *token_type);
static void buffer_add(char chr);
static void buffer_clean();
static void token_add(Tokens tokens, enum State state, char *val);
@ -32,6 +46,25 @@ void Lexer_delete(const Lexer self)
free(self);
}
bool
State_to_token_type(const enum State state, enum TokenType *const token_type)
{
switch (state) {
case STATE_ROUND_OPEN: *token_type = TOKEN_ROUND_OPEN; break;
case STATE_ROUND_CLOSE: *token_type = TOKEN_ROUND_CLOSE; break;
case STATE_SQUARE_OPEN: *token_type = TOKEN_SQUARE_OPEN; break;
case STATE_SQUARE_CLOSE: *token_type = TOKEN_SQUARE_CLOSE; break;
case STATE_CURLY_OPEN: *token_type = TOKEN_CURLY_OPEN; break;
case STATE_CURLY_CLOSE: *token_type = TOKEN_CURLY_CLOSE; break;
case STATE_QUOTE: *token_type = TOKEN_QUOTE; break;
case STATE_IDENT: *token_type = TOKEN_IDENT; break;
case STATE_NUM: *token_type = TOKEN_NUM; break;
default: return false;
}
return true;
}
void buffer_add(char chr)
{
assert(buffer_index < 1000);

View File

@ -1,5 +1,4 @@
#include "builtins.h"
#include "enums.h"
#include "lexer.h"
#include "object.h"
#include "parser.h"

View File

@ -1,4 +1,3 @@
#include "enums.h"
#include "object.h"
#include <assert.h>
@ -7,6 +6,20 @@
#include <stdlib.h>
#include <string.h>
const char *Type_to_str(const enum Type type)
{
switch (type) {
case TYPE_PROCEDURE: return "TYPE_PROCEDURE";
case TYPE_PAIR: return "TYPE_PAIR";
case TYPE_BOOLEAN: return "TYPE_BOOLEAN";
case TYPE_CHAR: return "TYPE_CHAR";
case TYPE_SYMBOL: return "TYPE_SYMBOL";
case TYPE_STRING: return "TYPE_STRING";
case TYPE_NUMBER: return "TYPE_NUMBER";
}
return NULL;
}
static struct Object *new(const enum Type type)
{
struct Object *const object = malloc(sizeof(struct Object));

View File

@ -1,11 +1,19 @@
#ifndef __LISP_OBJECT_H__
#define __LISP_OBJECT_H__
#include "enums.h"
#include <stdbool.h>
#include <stdint.h>
enum Type {
TYPE_PROCEDURE,
TYPE_PAIR,
TYPE_BOOLEAN,
TYPE_CHAR,
TYPE_SYMBOL,
TYPE_STRING,
TYPE_NUMBER,
};
struct Object {
enum Type type;
union {
@ -29,6 +37,8 @@ struct Object {
};
};
const char *Type_to_str(enum Type type);
struct Object *Object_new_procedure(
const char *name,
struct Object *(*func)(struct Object *args)

View File

@ -1,6 +1,5 @@
#include "parser.h"
#include "enums.h"
#include "object.h"
#include "tokens.h"

View File

@ -6,6 +6,22 @@
#include <stdlib.h>
#include <string.h>
const char *TokenType_to_str(const enum TokenType token_type)
{
switch (token_type) {
case TOKEN_ROUND_OPEN: return "TOKEN_ROUND_OPEN";
case TOKEN_ROUND_CLOSE: return "TOKEN_ROUND_CLOSE";
case TOKEN_SQUARE_OPEN: return "TOKEN_SQUARE_OPEN";
case TOKEN_SQUARE_CLOSE: return "TOKEN_SQUARE_CLOSE";
case TOKEN_CURLY_OPEN: return "TOKEN_CURLY_OPEN";
case TOKEN_CURLY_CLOSE: return "TOKEN_CURLY_CLOSE";
case TOKEN_QUOTE: return "TOKEN_QUOTE";
case TOKEN_IDENT: return "TOKEN_IDENT";
case TOKEN_NUM: return "TOKEN_NUM";
}
return NULL;
}
Tokens Tokens_new()
{
Tokens tokens = malloc(sizeof(struct Tokens));

View File

@ -1,8 +1,6 @@
#ifndef __LISP_TOKENS_H__
#define __LISP_TOKENS_H__
#include "enums.h"
#include <stdbool.h>
#include <stddef.h>
@ -11,6 +9,18 @@
tokens = NULL; \
} while (0)
enum TokenType {
TOKEN_ROUND_OPEN,
TOKEN_ROUND_CLOSE,
TOKEN_SQUARE_OPEN,
TOKEN_SQUARE_CLOSE,
TOKEN_CURLY_OPEN,
TOKEN_CURLY_CLOSE,
TOKEN_QUOTE,
TOKEN_IDENT,
TOKEN_NUM,
};
typedef struct Tokens {
struct Token *top;
} *Tokens;
@ -21,6 +31,8 @@ typedef struct Token {
char *val;
} *Token;
const char *TokenType_to_str(enum TokenType token_type);
Tokens Tokens_new();
void Tokens_delete(Tokens self);