diff --git a/src/types.h b/src/types.h index 621889c..bcf8310 100644 --- a/src/types.h +++ b/src/types.h @@ -112,7 +112,7 @@ typedef struct Position { } Position; typedef struct Token { - Type type; + Type type; Position pos; } Token; @@ -140,70 +140,70 @@ typedef struct Typed { typedef struct Identifier { AST_Type type; - char *name; + char *name; } Identifier; typedef struct TypedIdentifier { AST_Type type; - char *name; - char *kind; + char *name; + char *kind; } TypedIdentifier; typedef struct Literal { AST_Type type; - char *value; + char *value; } Literal; typedef struct StringLiteral { AST_Type type; - char *value; + char *value; } StringLiteral; typedef struct Program { AST_Type type; - Array *body; + Array *body; } Program; typedef struct ImportSpecifier { - AST_Type type; + AST_Type type; Identifier *name; Identifier *local; } ImportSpecifier; typedef struct ImportDeclaration { - AST_Type type; + AST_Type type; StringLiteral *source; - Array *specifiers; + Array *specifiers; } ImportDeclaration; typedef struct FunctionDeclaration { - AST_Type type; - boolean async; + AST_Type type; + boolean async; Identifier *name; - Array *params; - Array *body; + Array *params; + Array *body; } FunctionDeclaration; typedef struct ClassDeclaration { - AST_Type type; + AST_Type type; Identifier *name; - Array *body; + Array *body; } ClassDeclaration; typedef struct CallExpression { - AST_Type type; + AST_Type type; Identifier *callee; - Array *params; + Array *params; } CallExpression; typedef struct ExpressionStatement { AST_Type type; - Typed *expr; + Typed *expr; } ExpressionStatement; typedef struct ReturnStatement { AST_Type type; - Typed *expr; + Typed *expr; } ReturnStatement; typedef enum AssignmentOperators { @@ -223,10 +223,10 @@ typedef enum AssignmentOperators { } AssignmentOperator; typedef struct AssignmentExpression { - AST_Type type; + AST_Type type; AssignmentOperator oper; - Typed *left; - Typed *right; + Typed left; + Typed right; } AssignmentExpression; typedef enum BinaryOperators { @@ -247,10 +247,10 @@ typedef enum BinaryOperators { } BinaryOperator; typedef struct BinaryExpression { - AST_Type type; + AST_Type type; BinaryOperator oper; - Typed *left; - Typed *right; + Typed *left; + Typed *right; } BinaryExpression; typedef struct Result { @@ -258,13 +258,12 @@ typedef struct Result { void *data; } Result; -extern char *getTokenContent(char *code, Token *); -extern void freeProgram(Program *); -extern Program *initProgram(int); -extern FunctionDeclaration *initFunctionDeclaration( - boolean async, Identifier *name, Array *params, Array *body); +extern char *getTokenContent(char *code, Token *); +extern void freeProgram(Program *); +extern Program *initProgram(int); +extern FunctionDeclaration *initFunctionDeclaration(boolean async, Identifier *name, Array *params, Array *body); extern ClassDeclaration *initClassDeclaration(Identifier *name, Array *body); -extern ImportSpecifier *initImportSpecifier( +extern ImportSpecifier *initImportSpecifier( Identifier *name, Identifier *local_name); extern ImportDeclaration *initImportDeclaration( StringLiteral *, Array *import_specifier_array); @@ -278,6 +277,6 @@ extern CallExpression *initCallExpression(Identifier *callee, Array *params); extern BinaryExpression *initBinaryExpression( BinaryOperator oper, Typed *left, Typed *right); extern ExpressionStatement *initExpressionStatement(Typed *expr); -extern ReturnStatement *initReturnStatement(Typed *expr); +extern ReturnStatement *initReturnStatement(Typed *expr); #endif diff --git a/src/util/reader.c b/src/util/reader.c new file mode 100644 index 0000000..25229c5 --- /dev/null +++ b/src/util/reader.c @@ -0,0 +1,17 @@ +#include "reader.h" + +// todo return errors instead of exiting + +Reader *newReader(const char *filename) { + Reader *reader = malloc(sizeof(Reader)); + if (reader == NULL) { + fprintf(stderr, "Could not allocate memory for reader"); + exit(EXIT_FAILURE); + } + reader->fp = fopen(filename, "rb"); + if (reader->fp == NULL) { + fprintf(stderr, "Could not open file"); + free(reader); + exit(EXIT_FAILURE); + } +} diff --git a/src/util/reader.h b/src/util/reader.h new file mode 100644 index 0000000..ea68474 --- /dev/null +++ b/src/util/reader.h @@ -0,0 +1,21 @@ +#ifndef HADRON_READER_H +#define HADRON_READER_H + +#define CHUNK_SIZE 1024 + +#include +#include +#include + +typedef struct Reader { + FILE *fp; + char buffer[CHUNK_SIZE]; + size_t buffer_size; + size_t buffer_pos; +} Reader; + +Reader *newReader(const char *filename); +void freeReader(Reader *reader); +size_t readChunk(Reader *reader); + +#endif \ No newline at end of file