Skip to content

Commit

Permalink
Support file reading
Browse files Browse the repository at this point in the history
  • Loading branch information
LeotendoDev committed Mar 20, 2022
1 parent bbc65cf commit 6cad725
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gcc src/hasm.c src/token.c -I headers
gcc src/hasm.c src/token.c src/file_buffer.c -I headers -o hasm
6 changes: 1 addition & 5 deletions examples/test.hasm
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
%include "std.hasm"

main:
msg: string "Hello, World!"
print msg
123
Binary file added hasm
Binary file not shown.
5 changes: 5 additions & 0 deletions headers/file_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#define BUFFER_CHUNK_CAP 255

void read_filesrc(char* src_path);
6 changes: 5 additions & 1 deletion headers/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ char s_char_token[9] = {
'(', ')', '{', '}', '[', ']', ':', ';', '=',
};

static void
static void expect_token_next(char token_id)
{
if (token_id > 9)
return;
}
24 changes: 24 additions & 0 deletions src/file_buffer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdbool.h>

#include "file_buffer.h"

void read_filesrc(char* src_path)
{
bool exists = access(src_path, F_OK) == 0;
if (!exists) {
fprintf(stderr, "Source does not exist!");
return;
}

FILE* src_file = fopen(src_path, "r");
char buffer[BUFFER_CHUNK_CAP];

fgets(buffer, BUFFER_CHUNK_CAP, (FILE*)src_file);

fclose(src_file);

printf("%s\n", buffer);
}
6 changes: 5 additions & 1 deletion src/hasm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <stdio.h>

int main(int argc, char** argv) {
#include "file_buffer.h"

int main(int argc, char** argv)
{
read_filesrc("examples/test.hasm");
return 0;
}
4 changes: 4 additions & 0 deletions src/hasm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <stdint.h>

static uint64_t c_source_letter = 0;

0 comments on commit 6cad725

Please sign in to comment.