added a placeholder lexer and token struct

This commit is contained in:
ghostie 2025-07-02 20:25:05 -05:00
parent a34348be60
commit b0682bbde5
8 changed files with 105 additions and 2 deletions

View File

@ -5,7 +5,7 @@ RM=rm
CFLAGS=-Wall -Werror -std=gnu99 -O0 -g CFLAGS=-Wall -Werror -std=gnu99 -O0 -g
LIBS= LIBS=
FILES=build/main.o build/utils/vector.o build/utils/buffer.o FILES=build/main.o build/lexer.o build/utils/vector.o build/utils/buffer.o
OUT=bin/pinky.out OUT=bin/pinky.out
all: $(FILES) all: $(FILES)
@ -16,6 +16,10 @@ build/main.o: src/main.c
@$(ECHO) "CC\t\t"$< @$(ECHO) "CC\t\t"$<
@$(CC) $(CFLAGS) $< -c -o $@ $(LIBS) @$(CC) $(CFLAGS) $< -c -o $@ $(LIBS)
build/lexer.o: src/lexer.c
@$(ECHO) "CC\t\t"$<
@$(CC) $(CFLAGS) $< -c -o $@ $(LIBS)
build/utils/vector.o: src/utils/vector.c build/utils/vector.o: src/utils/vector.c
@$(ECHO) "CC\t\t"$< @$(ECHO) "CC\t\t"$<
@$(CC) $(CFLAGS) $< -c -o $@ $(LIBS) @$(CC) $(CFLAGS) $< -c -o $@ $(LIBS)

20
src/lexer.c Normal file
View File

@ -0,0 +1,20 @@
#include "lexer.h"
struct lexer
lexer_create ()
{
struct lexer l = { 0 };
l.tokens = vector_create ();
return l;
}
void
lexer_free (struct lexer *lexer)
{
if (!lexer)
return;
vector_free (&lexer->tokens);
}

14
src/lexer.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef __LEXER_H
#define __LEXER_H
#include "utils/vector.h"
struct lexer
{
struct vector tokens;
};
struct lexer lexer_create ();
void lexer_free (struct lexer *lexer);
#endif

View File

@ -1,6 +1,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "utils/buffer.h"
#include "lexer.h"
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
@ -10,5 +14,14 @@ main (int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
struct buffer code = buffer_create ();
if (!buffer_read (argv[1], &code))
return EXIT_FAILURE;
struct lexer lexer = lexer_create ();
lexer_free (&lexer);
buffer_free (&code);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

14
src/token.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef __TOKEN_H
#define __TOKEN_H
enum token_type
{
};
struct token
{
enum token_type type;
char lexeme[4];
};
#endif

View File

@ -22,7 +22,7 @@ buffer_create ()
{ {
struct buffer b = { 0 }; struct buffer b = { 0 };
b.capacity = 16; b.capacity = 4;
b.buf = calloc (b.capacity, sizeof (char)); b.buf = calloc (b.capacity, sizeof (char));
if (!b.buf) if (!b.buf)
{ {
@ -77,6 +77,41 @@ buffer_get (struct buffer *b)
return b->buf; return b->buf;
} }
_Bool
buffer_read (const char *path, struct buffer *b)
{
if (!path || !b)
return 0;
FILE *fp = fopen (path, "r");
if (!fp)
{
fputs ("buffer_read: cannot open path.", stderr);
return 0;
}
fseek (fp, 0, SEEK_END);
size_t size = ftell (fp);
fseek (fp, 0, SEEK_SET);
char *buf = malloc (sizeof (char) * size);
if (!buf)
{
fclose (fp);
fputs ("buffer_read: cannot allocate memory for temporary buffer.",
stderr);
return 0;
}
fread (buf, size, 1, fp);
buffer_append (buf, b);
free (buf);
fclose (fp);
return 1;
}
void void
buffer_free (struct buffer *b) buffer_free (struct buffer *b)
{ {

View File

@ -14,6 +14,7 @@ struct buffer buffer_create ();
void buffer_append (const char *s, struct buffer *b); void buffer_append (const char *s, struct buffer *b);
void buffer_printf (struct buffer *b, const char *fmt, ...); void buffer_printf (struct buffer *b, const char *fmt, ...);
char *buffer_get (struct buffer *b); char *buffer_get (struct buffer *b);
_Bool buffer_read (const char *path, struct buffer *b);
void buffer_free (struct buffer *b); void buffer_free (struct buffer *b);
#endif #endif

2
tests/expr.pinky Normal file
View File

@ -0,0 +1,2 @@
# Evaluate a simple expression
2 + 3