1
0
Fork 0
This commit is contained in:
Alex Kotov 2023-05-04 01:28:58 +04:00
parent 2213a40c02
commit e4004807fc
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 42 additions and 32 deletions

View file

@ -3,7 +3,7 @@ all: lisp
CC = gcc CC = gcc
CFLAGS = -Wall -Wextra CFLAGS = -Wall -Wextra
OBJS = main.c.o OBJS = main.c.o enums.c.o
lisp: $(OBJS) lisp: $(OBJS)
$(CC) -o $@ $^ $(CFLAGS) $(CC) -o $@ $^ $(CFLAGS)

16
enums.c Normal file
View file

@ -0,0 +1,16 @@
#include "enums.h"
#include <stddef.h>
const char *State_to_str(enum State state)
{
switch (state) {
case STATE_INIT: return "STATE_INIT";
case STATE_WHITESPACE: return "STATE_WHITESPACE";
case STATE_OPEN: return "STATE_OPEN";
case STATE_CLOSE: return "STATE_CLOSE";
case STATE_ID: return "STATE_ID";
case STATE_NUM: return "STATE_NUM";
}
return NULL;
}

22
enums.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef __ENUMS_H__
#define __ENUMS_H__
enum State {
STATE_INIT,
STATE_WHITESPACE,
STATE_OPEN,
STATE_CLOSE,
STATE_ID,
STATE_NUM,
};
enum Type {
TYPE_PAIR,
TYPE_ATOM,
TYPE_STRING,
TYPE_NUMBER,
};
const char *State_to_str(enum State state);
#endif

34
main.c
View file

@ -1,3 +1,5 @@
#include "enums.h"
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <stddef.h> #include <stddef.h>
@ -6,28 +8,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
enum State {
STATE_INIT,
STATE_WHITESPACE,
STATE_OPEN,
STATE_CLOSE,
STATE_ID,
STATE_NUM,
};
struct Token { struct Token {
struct Token *next; struct Token *next;
enum State type; enum State type;
char *val; char *val;
}; };
enum Type {
TYPE_PAIR,
TYPE_ATOM,
TYPE_STRING,
TYPE_NUMBER,
};
struct Object { struct Object {
enum Type type; enum Type type;
union { union {
@ -52,7 +38,6 @@ static void error(const char *msg);
* Lexer * * Lexer *
*********/ *********/
static const char *state_str(enum State state);
static void buffer_add(char chr); static void buffer_add(char chr);
static void buffer_clean(); static void buffer_clean();
static void token_add(enum State type, char *val); static void token_add(enum State type, char *val);
@ -93,7 +78,7 @@ int main()
printf("Tokens:\n"); printf("Tokens:\n");
for (const struct Token *token = tokens; token; token = token->next) { for (const struct Token *token = tokens; token; token = token->next) {
printf("%s:%s;\n", state_str(token->type), token->val); printf("%s:%s;\n", State_to_str(token->type), token->val);
} }
struct Object *const program = parse(); struct Object *const program = parse();
@ -112,19 +97,6 @@ void error(const char *msg)
* Lexer * * Lexer *
*********/ *********/
const char *state_str(enum State state)
{
switch (state) {
case STATE_INIT: return "STATE_INIT";
case STATE_WHITESPACE: return "STATE_WHITESPACE";
case STATE_OPEN: return "STATE_OPEN";
case STATE_CLOSE: return "STATE_CLOSE";
case STATE_ID: return "STATE_ID";
case STATE_NUM: return "STATE_NUM";
}
return NULL;
}
void buffer_add(char chr) void buffer_add(char chr)
{ {
if (buffer_index >= 1000) error("token too long"); if (buffer_index >= 1000) error("token too long");