diff --git a/v0/src/buffer.c b/v0/buffer.c similarity index 100% rename from v0/src/buffer.c rename to v0/buffer.c diff --git a/v0/buffer.h b/v0/buffer.h new file mode 100644 index 0000000..9b5c8e3 --- /dev/null +++ b/v0/buffer.h @@ -0,0 +1,50 @@ +/** + * An interface that wraps files and strings. + * Allows stream–like reading from it. + */ +#ifndef BUFFER_H +#define BUFFER_H + +/** + * An interface to a source of textual data. + */ +typedef struct Buffer Buffer; + +/** + * Opens a file. + * + * @param path The path to the file. + * @returns The newly–opened buffer. + */ +Buffer* buffer_open_file(const char* path); + +/** + * Opens a string. + * + * The string is not copied, and must not be free'd until the + * buffer itself has been closed. + * + * @param string The contents stored in the buffer. + * @returns A newly–opened buffer that reads from the string. + */ +Buffer* buffer_open_string(const char* string); + +/** + * Closes the buffer. + * + * @param buffer The buffer to close. + */ +void buffer_close(Buffer* buffer); + +/** + * Reads a single character from the buffer. + * + * If there are no more characters in the buffer, + * this returns `-1`. + * + * @param buffer The buffer to read from. + * @returns the next character in the buffer. + */ +char buffer_read(Buffer* buffer); + +#endif diff --git a/v0/main.c b/v0/main.c new file mode 100644 index 0000000..932a6c7 --- /dev/null +++ b/v0/main.c @@ -0,0 +1,5 @@ +#include + +int main(int argc, char** argv) { + puts("Hello, world"); +} \ No newline at end of file diff --git a/v0/tests/test.c b/v0/test.c similarity index 100% rename from v0/tests/test.c rename to v0/test.c diff --git a/v0/tests/test.h b/v0/test.h similarity index 100% rename from v0/tests/test.h rename to v0/test.h diff --git a/v0/tests/test_buffer.c b/v0/test_buffer.c similarity index 89% rename from v0/tests/test_buffer.c rename to v0/test_buffer.c index 9644be5..69fa7d2 100644 --- a/v0/tests/test_buffer.c +++ b/v0/test_buffer.c @@ -1,5 +1,5 @@ #include "test.h" -#include "../src/buffer.h" +#include "buffer.h" #include // @copilot add test for reading from a file. @@ -27,7 +27,7 @@ static void test_buffer_string_eof_after_content(void) { } static void test_buffer_file_reads_chars(void) { - Buffer* buf = buffer_open_file("v0/tests/test_buffer.txt"); + Buffer* buf = buffer_open_file("v0/test_buffer.txt"); if (buf == NULL) fail("could not open file"); if (buffer_read(buf) != 'a') fail("expected 'a'"); if (buffer_read(buf) != 'b') fail("expected 'b'"); @@ -38,6 +38,6 @@ static void test_buffer_file_reads_chars(void) { } static void test_buffer_file_open_fail(void) { - Buffer* buf = buffer_open_file("v0/tests/does_not_exist.txt"); + Buffer* buf = buffer_open_file("v0/does_not_exist.txt"); if (buf != NULL) fail("expected NULL for non-existent file"); } diff --git a/v0/tests/test_buffer.txt b/v0/test_buffer.txt similarity index 100% rename from v0/tests/test_buffer.txt rename to v0/test_buffer.txt diff --git a/v0/token.h b/v0/token.h new file mode 100644 index 0000000..e4a5002 --- /dev/null +++ b/v0/token.h @@ -0,0 +1,49 @@ +/** + * Contains the interface for reading tokens from a file. + */ +#ifndef TOKEN_H +#define TOKEN_H + +typedef enum { + // Keywords + TOKEN_MODULE, + TOKEN_IMPORT, + TOKEN_SEMICOLON, + + // Symbols + TOKEN_PARENT_OPEN, + TOKEN_PARENT_CLOSE, + TOKEN_BRACKET_OPEN, + TOKEN_BRACKET_CLOSE, + TOKEN_COMMA, + + // Primitives + TOKEN_VOID, + + // Variable + TOKEN_IDENTIFIER, +} Token; + +typedef struct TokenStream TokenStream; + +/** + * Opens a file and returns a TokenStream for it. + * @param path The path to the file. + * @returns A handle to the TokenStream. + */ +TokenStream* tokenstream_open(const char* path); + +/** + * Closes a TokenStream. + * @param ts The TokenStream to close. + */ +void tokenstream_close(TokenStream* ts); + +/** + * Gets the next token from the TokenStream. + * @param ts The TokenStream to read from. + * @returns The next token read. + */ +Token tokenstream_next(TokenStream* ts); + +#endif \ No newline at end of file