Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Thomas de Bock

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
main:
mkdir -p bin/
g++ src/construct.cpp src/deconstruct.cpp src/construct_debug.cpp src/reconstruct.cpp src/construct_flags.cpp -o bin/construct
g++ src/construct.cpp src/deconstruct.cpp src/construct_debug.cpp src/reconstruct.cpp src/construct_flags.cpp src/construct_error.cpp -o bin/construct
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Using the naming of the conditional jump instructions, construct supports the fo
- `g: greater`
- `le: less or equal`
- `ge: greater or equal`

Neither side of the comparison can contains whitespaces.

# Use
Expand Down
14 changes: 6 additions & 8 deletions src/construct.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
#include "deconstruct.h"
#include "reconstruct.h"
#include "construct_flags.h"
#include<iostream>
#include<fstream>
#include <iostream>
#include <fstream>

int main(int argc, char** argv) {
std::string path;
std::string outpath;
if(handle_flags(argc, argv, &path, &outpath) != 0) {
if(handle_flags(argc, argv) != 0) {
std::cout << "Some flag(s) not set" << std::endl;
return 0;
}
if(path.empty()) {
if(inputfile.empty()) {
std::cout << "No input file specified" << std::endl;
return 0;
}

std::ifstream inpfile(path);
std::ifstream inpfile(inputfile);
std::stringstream buffer;
buffer << inpfile.rdbuf();
std::vector<con_token*> tokens = parse_construct(buffer.str());
Expand All @@ -39,7 +37,7 @@ int main(int argc, char** argv) {
linearize_tokens(tokens);

std::ofstream outfile;
outfile.open(outpath);
outfile.open(outputfile);
outfile << tokens_to_nasm(tokens);
outfile.close();
}
4 changes: 2 additions & 2 deletions src/construct_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ std::string token_to_string(con_token token) {
break;
case FUNCTION:
tokstring += ", function: " + token.tok_function->name + ", arguments: ";
for(int i = 0; i < token.tok_function->arguments.size(); i++) {
for(size_t i = 0; i < token.tok_function->arguments.size(); i++) {
if(i != 0) {
tokstring += ", ";
}
Expand All @@ -61,7 +61,7 @@ std::string token_to_string(con_token token) {
}
if(token.tokens.size() > 0) {
tokstring += ", tokens: {\n";
for(int i = 0; i < token.tokens.size(); i++) {
for(size_t i = 0; i < token.tokens.size(); i++) {
tokstring += token_to_string(*token.tokens[i]) + "\n";
}
tokstring += "}";
Expand Down
9 changes: 7 additions & 2 deletions src/construct_debug.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include<iostream>
#include<string>
#ifndef CON_DEBUG_H
#define CON_DEBUG_H

#include <iostream>
#include <string>
#include "construct_types.h"
#include "reconstruct.h"

std::string tokentype_to_string(CON_TOKENTYPE type);
std::string token_to_string(con_token token);

#endif
20 changes: 20 additions & 0 deletions src/construct_error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "construct_error.h"
#include "construct_flags.h"
#include <string>

const char* parse_error_strings[2] = {"Failed to parse if-statement", "Failed to parse while-loop"};

void throw_parse_error(linedata* linedata, PARSE_ERROR error_type, size_t tok_index) {
std::cerr << inputfile << ": " << parse_error_strings[error_type] << "\n"
<< linedata->line_num << " | ";
size_t space_count = 3 + std::to_string(linedata->line_num).size();
for(size_t i = 0; i < linedata->line_split->size(); i++) {
std::cerr << (*linedata->line_split)[i] << " ";
if(i >= tok_index) {
continue;
}
space_count += (*linedata->line_split)[i].size() + 1;
}
std::cerr << "\n" << std::string(space_count, ' ') << "^\n";
exit(EXIT_FAILURE);
}
16 changes: 16 additions & 0 deletions src/construct_error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef CON_ERROR_H
#define CON_ERROR_H

#include "construct_types.h"
#include "construct_flags.h"
#include <cstdlib>

enum PARSE_ERROR {
IF_ERROR,
WHILE_ERROR
};

void throw_parse_error(linedata* linedata, PARSE_ERROR error_type, size_t tok_index);


#endif
40 changes: 33 additions & 7 deletions src/construct_flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

using namespace std;

CON_BITWIDTH bitwidth = BIT64;
std::string inputfile;
std::string outputfile;

int set_bitwidth(char* argv) {
if(strcmp(argv, "elf64") == 0) {
bitwidth = BIT64;
Expand All @@ -24,11 +28,12 @@ int set_bitwidth(char* argv) {
return -1;
}

int handle_flags(int argc, char** argv, string* path, string* outpath) {
int handle_flags(int argc, char** argv) {
bool bitwidth_set = false;
bool path_set = false;
bool outpath_set = false;
for(int i = 1; i < argc; i++) {
bool help_set = false;
for(size_t i = 1; i < argc; i++) {
if(strcmp(argv[i], "-f") == 0 && set_bitwidth(argv[i+1]) == 0) {
bitwidth_set = true;
i++;
Expand All @@ -37,19 +42,40 @@ int handle_flags(int argc, char** argv, string* path, string* outpath) {
if(strcmp(argv[i], "-i") == 0) {
path_set = true;
i++;
(*path) = argv[i];
inputfile = argv[i];
continue;
}
if(strcmp(argv[i], "-o") == 0) {
outpath_set = true;
i++;
(*outpath) = argv[i];
outputfile = argv[i];
continue;
}
if(path != NULL) {
path_set = true;
(*path) = argv[i];
if(strcmp(argv[i], "-h") == 0) {
help_set = true;
continue;
}

path_set = true;
inputfile = argv[i];
}
if(!bitwidth_set && !path_set && !outpath_set) {
help_set = true;
}
if(help_set) {
cout <<
"Construct (version 1.1.0)\n"
"An abstraction over x86 assembly providing useful shortcuts and syntax.\n\n"

"Usage:\n"
"-i Input file. Should have valid construct syntax.\n"
"-f Output format. Can be either elf64, elf32, elf16, or elf8.\n"
"-o Output file. Specifies where to put the resulting NASM assembly file.\n\n"

"2024 Thomas de Bock\n"
"MIT LICENSE\n"
"https://github.com/Thomas-de-Bock/construct/\n";
return -1;
}
if(!bitwidth_set) {
cout << "flag -f (format) not set" << endl;
Expand Down
19 changes: 14 additions & 5 deletions src/construct_flags.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#include<string>
#include<cstring>
#include<iostream>
#include "reconstruct.h"
#ifndef CON_FLAGS_H
#define CON_FLAGS_H

#include <string>
#include <cstring>
#include <iostream>
#include "construct_types.h"

extern CON_BITWIDTH bitwidth;
extern std::string inputfile;
extern std::string outputfile;

int set_bitwidth(char* argv);
int handle_flags(int argc, char** argv, std::string* path, std::string* outpath);
int handle_flags(int argc, char** argv);

#endif
18 changes: 13 additions & 5 deletions src/construct_types.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef CON_TYPES_H
#define CON_TYPES_H

#include<string>
#include<vector>
#include <string>
#include <vector>

enum CON_BITWIDTH {
BIT8,
Expand All @@ -17,7 +17,8 @@ enum CON_COMPARISON {
L,
G,
LE,
GE
GE,
COMPARISON_ERROR
};

enum CON_TOKENTYPE {
Expand All @@ -28,7 +29,7 @@ enum CON_TOKENTYPE {
FUNCTION,
CMD,
MACRO,
FUNCALL
FUNCALL,
};


Expand Down Expand Up @@ -86,7 +87,14 @@ struct con_cmd {

struct con_funcall {
std::string funcname;
std:: vector<std::string> arguments;
std::vector<std::string> arguments;
};

struct linedata {
std::string* line;
std::vector<std::string>* line_split;
std::string* filename;
size_t line_num;
};

#endif
Loading