added a placeholder lexer and token struct
This commit is contained in:
parent
a34348be60
commit
b0682bbde5
6
Makefile
6
Makefile
@ -5,7 +5,7 @@ RM=rm
|
||||
CFLAGS=-Wall -Werror -std=gnu99 -O0 -g
|
||||
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
|
||||
|
||||
all: $(FILES)
|
||||
@ -16,6 +16,10 @@ build/main.o: src/main.c
|
||||
@$(ECHO) "CC\t\t"$<
|
||||
@$(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
|
||||
@$(ECHO) "CC\t\t"$<
|
||||
@$(CC) $(CFLAGS) $< -c -o $@ $(LIBS)
|
||||
|
20
src/lexer.c
Normal file
20
src/lexer.c
Normal 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
14
src/lexer.h
Normal 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
|
13
src/main.c
13
src/main.c
@ -1,6 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "utils/buffer.h"
|
||||
|
||||
#include "lexer.h"
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@ -10,5 +14,14 @@ main (int argc, char **argv)
|
||||
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;
|
||||
}
|
||||
|
14
src/token.h
Normal file
14
src/token.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef __TOKEN_H
|
||||
#define __TOKEN_H
|
||||
|
||||
enum token_type
|
||||
{
|
||||
};
|
||||
|
||||
struct token
|
||||
{
|
||||
enum token_type type;
|
||||
char lexeme[4];
|
||||
};
|
||||
|
||||
#endif
|
@ -22,7 +22,7 @@ buffer_create ()
|
||||
{
|
||||
struct buffer b = { 0 };
|
||||
|
||||
b.capacity = 16;
|
||||
b.capacity = 4;
|
||||
b.buf = calloc (b.capacity, sizeof (char));
|
||||
if (!b.buf)
|
||||
{
|
||||
@ -77,6 +77,41 @@ buffer_get (struct buffer *b)
|
||||
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
|
||||
buffer_free (struct buffer *b)
|
||||
{
|
||||
|
@ -14,6 +14,7 @@ struct buffer buffer_create ();
|
||||
void buffer_append (const char *s, struct buffer *b);
|
||||
void buffer_printf (struct buffer *b, const char *fmt, ...);
|
||||
char *buffer_get (struct buffer *b);
|
||||
_Bool buffer_read (const char *path, struct buffer *b);
|
||||
void buffer_free (struct buffer *b);
|
||||
|
||||
#endif
|
||||
|
2
tests/expr.pinky
Normal file
2
tests/expr.pinky
Normal file
@ -0,0 +1,2 @@
|
||||
# Evaluate a simple expression
|
||||
2 + 3
|
Loading…
x
Reference in New Issue
Block a user