Reading the source file
Before the lexer can find return and 42, it needs the bytes from the source file. We will read those bytes into memory and write them back out to check that we received the whole file.
The source buffer
A Source holds the path, the address of the buffer, and its length. The path comes from the command line and remains available while main runs. The buffer has its own lifetime. The reader allocates it, and the compiler keeps it until no stage needs the bytes.
flowchart TB
accTitle: Source fields and their lifetimes
accDescr: Source borrows the path from command-line arguments and owns the allocated byte buffer. Its length field records how many bytes are in the buffer.
source["Source"]
source -->|path| path["Borrowed from arguments"]
source -->|bytes| buffer["Allocated by the reader"]
source -->|length| length["Number of bytes"]
#ifndef AVAC_H
#define AVAC_H
#include <stdbool.h>
#include <stddef.h>
typedef struct Source {
const char *path;
unsigned char *bytes;
size_t length;
} Source;
bool source_read(const char *path, Source *source);
void source_free(Source *source);
#endif
Read and release the file
For a regular file, we can seek to the end, get its length, and allocate that many bytes. An empty file needs no allocation. The buffer can contain zero bytes, so every caller uses the explicit length. See why the length matters.
source_read fills in the result after a complete read. If opening the file, measuring it, or reading it fails, the function reports the error and releases any resources it has acquired.
#include "avac.h"
#include <stdio.h>
#include <stdlib.h>
bool source_read(const char *path, Source *source) {
FILE *file = fopen(path, "rb");
if (file == NULL) {
perror(path);
return false;
}
if (fseek(file, 0, SEEK_END) != 0) {
perror(path);
fclose(file);
return false;
}
long file_size = ftell(file);
if (file_size < 0) {
perror(path);
fclose(file);
return false;
}
rewind(file);
size_t length = (size_t)file_size;
unsigned char *bytes = NULL;
if (length > 0) {
bytes = malloc(length);
if (bytes == NULL) {
fprintf(stderr, "%s: error: not enough memory\n", path);
fclose(file);
return false;
}
if (fread(bytes, 1, length, file) != length) {
fprintf(stderr, "%s: error: could not read complete source\n", path);
free(bytes);
fclose(file);
return false;
}
}
fclose(file);
source->path = path;
source->bytes = bytes;
source->length = length;
return true;
}
void source_free(Source *source) {
free(source->bytes);
*source = (Source){0};
}
Write the bytes back
The command avac dump source writes the buffer to standard output and releases it. We pass 1 as the item size to fwrite, so its return value can be compared directly with the byte count.
#include "avac.h"
#include <stdio.h>
#include <string.h>
static int dump_source(const char *path) {
Source source = {0};
if (!source_read(path, &source)) {
return 1;
}
if (source.length > 0 &&
fwrite(source.bytes, 1, source.length, stdout) != source.length) {
fprintf(stderr, "%s: error: could not write complete source\n", path);
source_free(&source);
return 1;
}
source_free(&source);
return 0;
}
int main(int argc, char **argv) {
if (argc != 4 || strcmp(argv[1], "dump") != 0 ||
strcmp(argv[2], "source") != 0) {
fputs("Usage: avac dump source INPUT.ava\n", stderr);
return 2;
}
return dump_source(argv[3]);
}
Check the result
Code for this lesson includes the complete source files and build instructions.
Save the three C files above and this test input in the same directory.
fn main() -> i32 {
return 42
}
Build the reader and compare its output with the original file. A successful cmp prints nothing and exits with status 0.
cc -std=c17 -Wall -Wextra -Wpedantic -Werror avac.c source.c -o avac
./avac dump source return-42.ava > source-copy.ava
cmp return-42.ava source-copy.ava
Try an empty file and a missing path. The empty file should produce zero bytes and succeed. The missing file should report an error and return status 1.