This repository has been archived on 2025-11-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
ovm/asm/main.cpp
Aryadev Chavali d368a49f56 Implemented a function to read a file in full
Uses std::optional in case file doesn't exist.
2024-04-14 17:10:23 +06:30

57 lines
1.2 KiB
C++

/* Copyright (C) 2024 Aryadev Chavali
* You may distribute and modify this code under the terms of the
* GPLv2 license. You should have received a copy of the GPLv2
* license with this file. If not, please write to:
* aryadev@aryadevchavali.com.
* Created: 2024-04-14
* Author: Aryadev Chavali
* Description: Entrypoint for assembly program
*/
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <optional>
#include <string>
#include <lib/inst.h>
#include "./lexer.hpp"
using std::pair, std::string, std::string_view, std::vector;
std::optional<string> read_file(const char *filename)
{
FILE *fp = fopen(filename, "rb");
if (fp)
{
string contents;
fseek(fp, 0, SEEK_END);
contents.resize(ftell(fp));
rewind(fp);
fread(&contents[0], 1, contents.size(), fp);
fclose(fp);
return contents;
}
else
return std::nullopt;
}
void usage(const char *program_name, FILE *fp)
{
fprintf(fp,
"Usage: %s FILE OUT-FILE\n"
"\tFILE: Source code to compile\n"
"\tOUT-FILE: Name of file to store bytecode\n",
program_name);
}
int main(int argc, const char *argv[])
{
usage(argv[1], stdout);
return 0;
}