Skip to content

Commit

Permalink
Starting implementation of Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-is-coding committed May 4, 2024
1 parent 31a6b37 commit 0646a7b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
65 changes: 32 additions & 33 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ typedef struct Position {
} Position;

typedef struct Token {
Type type;
Type type;
Position pos;
} Token;

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -247,24 +247,23 @@ 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 {
Array *errors;
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);
Expand All @@ -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
17 changes: 17 additions & 0 deletions src/util/reader.c
Original file line number Diff line number Diff line change
@@ -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);
}
}
21 changes: 21 additions & 0 deletions src/util/reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef HADRON_READER_H
#define HADRON_READER_H

#define CHUNK_SIZE 1024

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

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

0 comments on commit 0646a7b

Please sign in to comment.