Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions compiler/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CC := clang++
CFLAGS := -Wall
CFLAGS += -std=c++11
CFLAGS += -O2
LFLAGS :=

ODIR := .OBJ

MAIN_SOURCE_DEPS := main.cpp cli.h common.h entire_file.h

all: binaries

$(ODIR):
@mkdir $(ODIR)

binaries: bytecode-cc

MAIN_OBJECT_DEPS := $(ODIR)/main.o

bytecode-cc: $(ODIR) $(MAIN_OBJECT_DEPS)
$(CC) $(CFLAGS) $(MAIN_OBJECT_DEPS) -o bytecode-cc $(LFLAGS)

$(ODIR)/main.o: $(ODIR) $(MAIN_SOURCE_DEPS)
$(CC) -c $(CFLAGS) main.cpp -o $(ODIR)/main.o

.PHONY: clean
clean:
rm -rf .OBJ bytecode-cc
103 changes: 103 additions & 0 deletions compiler/cli.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#ifndef _CLI_H_
#define _CLI_H_

#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cerrno>
#include <string>
#include <vector>

struct Cli {
std::string input;
std::string output;

void print_debug() {
printf("Cli {\n");
printf("\tinput: %s\n", this->input.c_str());
printf("\toutput: %s\n", this->output.c_str());
printf("}\n");
}

static void help() {
printf("Usage: Cli [OPTIONS]\n"
"\n"
"Options:\n"
" -h, --help\n"
" -i, --input <INPUT>\n"
" -o, --output <OUTPUT>\n"
);
exit(0);
}

static bool is_option(char* arg) {
static const char* valid_options[] = {
"-i",
"--input",
"-o",
"--output",
};

for (size_t i = 0; i != 4; ++i) {
if (strcmp(arg, valid_options[i]) == 0) {
return true;
}
}

return false;
}

static Cli parse (int argc, char *args[]) {
--argc;
++args;

const char* mandatory_field_names[] = { "input", "output", };
bool mandatory_fields_seen[sizeof(mandatory_field_names)/sizeof(mandatory_field_names[0])] = { false };

Cli res = {};
for (int i = 0; i != argc; ++i, ++args) {
char *arg = args[0];
if (strcmp("-h", arg) == 0 || strcmp("--help", arg) == 0) {
Cli::help();
} else if (strcmp(arg, "-i") == 0 || strcmp(arg, "--input") == 0) {
++args;
++i;
if (i == argc) {
printf("Expected value for option '%s' but no value was provided", arg);
exit(1);
}
std::string arg_res = args[0];
res.input = arg_res;
mandatory_fields_seen[0] = true;
} else if (strcmp(arg, "-o") == 0 || strcmp(arg, "--output") == 0) {
++args;
++i;
if (i == argc) {
printf("Expected value for option '%s' but no value was provided", arg);
exit(1);
}
std::string arg_res = args[0];
res.output = arg_res;
mandatory_fields_seen[1] = true;
} else {
printf("Unknown option '%s'\n", arg);
exit(1);
}
}

bool not_seen_any = false;
for (size_t i = 0; i != sizeof(mandatory_field_names)/sizeof(mandatory_field_names[0]); ++i) {
if (!mandatory_fields_seen[i]) {
printf("--%s was required but it was not provided\n", mandatory_field_names[i]);
not_seen_any = true;
}
}
if (not_seen_any) {
exit(1);
}
return res;
}
};

#endif // _CLI_H_
7 changes: 7 additions & 0 deletions compiler/cli.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[main]
struct Cli {
#[short, long]
input: string,
#[short, long]
output: string,
}
14 changes: 14 additions & 0 deletions compiler/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Created by George Liontos on 20/4/24.
//

#ifndef COMPILER_COMMON_H
#define COMPILER_COMMON_H

#include <stdlib.h>

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))

#define MALLOC(size, type) ((type*) malloc((size) * sizeof(type)))

#endif //COMPILER_COMMON_H
59 changes: 59 additions & 0 deletions compiler/entire_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Created by George Liontos on 20/4/24.
//

#ifndef CPP_ENTIRE_FILE_H
#define CPP_ENTIRE_FILE_H

#include <stdio.h>
#include <stddef.h>
#include "common.h"

struct EntireFile {
char* contents;
size_t num_bytes;
};

EntireFile read_entire_file_into_memory(const char* path) {
EntireFile res = {0};
FILE* f = fopen(path, "rb");
size_t num_bytes_read = 0U;

if (f == nullptr) {
fprintf(stderr, "Could not open '%s' for reading\n", path);
return res;
}

if (fseek(f, 0, SEEK_END) != 0) {
goto err;
}

res.num_bytes = ftell(f);
if (res.num_bytes == -1L) {
goto err;
}

if (fseek(f, 0, SEEK_SET) != 0) {
goto err;
}

res.contents = MALLOC(res.num_bytes + 1, char);
num_bytes_read = fread(res.contents, sizeof(char), res.num_bytes, f);
if (num_bytes_read != res.num_bytes) {
goto err;
}

fclose(f);
return res;

err:
fclose(f);
if (res.contents) {
free(res.contents);
}
res.contents = nullptr;
res.num_bytes = 0;
return res;
}

#endif //CPP_ENTIRE_FILE_H
3 changes: 3 additions & 0 deletions compiler/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONST 1
CONST 7
ADD
Binary file added compiler/example.out
Binary file not shown.
Loading