Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
.vscode/
41 changes: 38 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
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
CC = g++
CFLAGS = -std=c++11 -Wall --pedantic-errors -g
SDIR = src
BDIR = bin
_OBJS = construct_debug.o construct_flags.o deconstruct.o reconstruct.o construct.o
OBJS = $(patsubst %,$(BDIR)/%,$(_OBJS))
PROG = construct

.PHONY: all clean

all: $(OBJS) $(BDIR)/$(PROG)

$(BDIR)/$(PROG): $(OBJS)
mkdir -p $(BDIR)
$(CC) $(OBJS) -o $(BDIR)/$(PROG)

$(BDIR)/construct.o: $(SDIR)/construct.cpp $(SDIR)/deconstruct.h $(SDIR)/reconstruct.h $(SDIR)/construct_flags.h $(SDIR)/construct_types.h
mkdir -p $(BDIR)
$(CC) -c $(SDIR)/construct.cpp -o $(BDIR)/construct.o $(CFLAGS)

$(BDIR)/construct_debug.o: $(SDIR)/construct_debug.cpp $(SDIR)/construct_debug.h $(SDIR)/construct_types.h $(SDIR)/reconstruct.h
mkdir -p $(BDIR)
$(CC) -c $(SDIR)/construct_debug.cpp -o $(BDIR)/construct_debug.o $(CFLAGS)

$(BDIR)/construct_flags.o: $(SDIR)/construct_flags.cpp $(SDIR)/construct_flags.h $(SDIR)/construct_types.h
mkdir -p $(BDIR)
$(CC) -c $(SDIR)/construct_flags.cpp -o $(BDIR)/construct_flags.o $(CFLAGS)

$(BDIR)/deconstruct.o: $(SDIR)/deconstruct.cpp $(SDIR)/deconstruct.h $(SDIR)/construct_types.h
mkdir -p $(BDIR)
$(CC) -c $(SDIR)/deconstruct.cpp -o $(BDIR)/deconstruct.o $(CFLAGS)

$(BDIR)/reconstruct.o: $(SDIR)/reconstruct.cpp $(SDIR)/reconstruct.h $(SDIR)/construct_types.h
mkdir -p $(BDIR)
$(CC) -c $(SDIR)/reconstruct.cpp -o $(BDIR)/reconstruct.o $(CFLAGS)

clean:
rm -rf $(BDIR)
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fmt: db "%s", 10, 0
- If statements: If statements, like while loops, take a single [conditional](#conditionals) statement
- Functions:
Functions are declared with the "function" keyword, a "ret" instruction is added to functions in post-processing, so functions will not flow into eachother.
- Function calls: Functions can be called with any number of arguments, independent of the function decleration.
- Function calls: Functions can be called with any number of arguments, independent of the function decleration.
If the amount of arguments used to call a function is more than its decleration states, they can be accessed like normal with their respective registers / stack address.
Construct function calls, like NASM, use the "call" keyword. Functions can still be called without parentheses or arguments, NASM-style.
- Macros: Construct macros can only be used in their respective scopes. Construct macros are declared with the '!' character and cannot contain whitespaces.
Expand All @@ -58,3 +58,4 @@ Neither side of the comparison can contains whitespaces.
### Required flags
- `-f (format)`: Can be either "elf64", "elf32", "elf16", "elf8" and decides the registers used for funcion calls.
- `-i (input file)`: Specifies the input file to be compiled (-i is not neccesary)
- `-o (output file)`: Specifies the output file to be created
2 changes: 1 addition & 1 deletion examples/factorial.con
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function factorial(num):
while i le num:
mul i
inc i

function main():
call factorial(3)
!result rax
Expand Down
3 changes: 1 addition & 2 deletions examples/strchr.con
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ extern printf
section .text
function strchr(str, chr):
!ptrresult rax
!findchr sil
mov ptrresult, 0
while byte[str] ne 0:
if byte[str] e findchr:
if byte[str] e chr:
mov ptrresult, str
ret
inc str
Expand Down
21 changes: 13 additions & 8 deletions src/construct.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#include "deconstruct.h"
#include "reconstruct.h"
#include "construct_flags.h"
#include<iostream>
#include<fstream>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include "construct_types.h"
#include "deconstruct.h" // parse_construct()
#include "reconstruct.h" // linearize_tokens()
#include "construct_flags.h" // handle_flags()

int main(int argc, char** argv) {
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, &path, &outpath) != 0) {
std::cout << "Some flag(s) not set" << std::endl;
return 0;
}
if(path.empty()) {
if (path.empty()) {
std::cout << "No input file specified" << std::endl;
return 0;
}
Expand Down
33 changes: 22 additions & 11 deletions src/construct_debug.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#include <string>
#include <vector>
#include <stdexcept>
#include "construct_debug.h"
#include "construct_types.h"
#include "reconstruct.h" // comparison_to_string()

std::string tokentype_to_string(CON_TOKENTYPE type) {
switch(type) {
std::string tokentype_to_string(CON_TOKENTYPE type)
{
switch (type) {
case SECTION:
return "section";
case TAG:
Expand All @@ -16,13 +22,16 @@ std::string tokentype_to_string(CON_TOKENTYPE type) {
return "cmd";
case MACRO:
return "macro";
case FUNCALL:
return "funcall";
}
return "unknown";
throw std::invalid_argument("Invalid token type: "+std::to_string(static_cast<int>(type)));
}

std::string token_to_string(con_token token) {
std::string token_to_string(con_token token)
{
std::string tokstring = "type: " + tokentype_to_string(token.tok_type);
switch(token.tok_type) {
switch (token.tok_type) {
case SECTION:
tokstring += ", name: " + token.tok_section->name;
break;
Expand All @@ -37,19 +46,19 @@ 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++) {
if(i != 0) {
for (size_t i = 0; i < token.tok_function->arguments.size(); i++) {
if (i != 0) {
tokstring += ", ";
}
tokstring += token.tok_function->arguments[i];
}
break;
case CMD:
if(!token.tok_cmd->arg1.empty() && !token.tok_cmd->arg2.empty()) {
if (!token.tok_cmd->arg1.empty() && !token.tok_cmd->arg2.empty()) {
tokstring += ", cmd: " + token.tok_cmd->command + " " + token.tok_cmd->arg1 + ", " + token.tok_cmd->arg2;
break;
}
if(!token.tok_cmd->arg1.empty()) {
if (!token.tok_cmd->arg1.empty()) {
tokstring += ", cmd: " + token.tok_cmd->command + " " + token.tok_cmd->arg1;
break;
}
Expand All @@ -58,10 +67,12 @@ std::string token_to_string(con_token token) {
case MACRO:
tokstring += ", macro: " + token.tok_macro->macro + ", value: " + token.tok_macro->value;
break;
default: // FUNCALL
break;
}
if(token.tokens.size() > 0) {
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: 6 additions & 3 deletions src/construct_debug.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include<iostream>
#include<string>
#ifndef CONSTRUCT_DEBUG_H_
#define CONSTRUCT_DEBUG_H_

#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 // CONSTRUCT_DEBUG_H_
34 changes: 20 additions & 14 deletions src/construct_flags.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,71 @@
#include <string>
#include <iostream>
#include "construct_flags.h"
#include "construct_types.h"

extern CON_BITWIDTH bitwidth;

using namespace std;

int set_bitwidth(char* argv) {
if(strcmp(argv, "elf64") == 0) {
int set_bitwidth(char* argv)
{
if (string(argv) == "elf64") {
bitwidth = BIT64;
return 0;
}
if(strcmp(argv, "elf32") == 0) {
if (string(argv) == "elf32") {
bitwidth = BIT32;
return 0;
}
if(strcmp(argv, "elf16") == 0) {
if (string(argv) == "elf16") {
bitwidth = BIT16;
return 0;
}
if(strcmp(argv, "elf8") == 0) {
if (string(argv) == "elf8") {
bitwidth = BIT8;
return 0;
}
cout << "\"" << argv << "\" not a supported format" << endl;
return -1;
}

int handle_flags(int argc, char** argv, string* path, string* outpath) {
int handle_flags(int argc, char** argv, string* path, string* outpath)
{
bool bitwidth_set = false;
bool path_set = false;
bool outpath_set = false;
for(int i = 1; i < argc; i++) {
if(strcmp(argv[i], "-f") == 0 && set_bitwidth(argv[i+1]) == 0) {
for (int i = 1; i < argc; i++) {
if (string(argv[i]) == "-f" && set_bitwidth(argv[i+1]) == 0) {
bitwidth_set = true;
i++;
continue;
}
if(strcmp(argv[i], "-i") == 0) {
if (string(argv[i]) == "-i") {
path_set = true;
i++;
(*path) = argv[i];
continue;
}
if(strcmp(argv[i], "-o") == 0) {
if (string(argv[i]) == "-o") {
outpath_set = true;
i++;
(*outpath) = argv[i];
continue;
}
if(path != NULL) {
if (path != NULL) {
path_set = true;
(*path) = argv[i];
}
}
if(!bitwidth_set) {
if (!bitwidth_set) {
cout << "flag -f (format) not set" << endl;
return -1;
}
if(!path_set) {
if (!path_set) {
cout << "flag -i (input file) not set" << endl;
return -1;
}
if(!outpath_set) {
if (!outpath_set) {
cout << "flag -o (output file) not set" << endl;
return -1;
}
Expand Down
10 changes: 6 additions & 4 deletions src/construct_flags.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include<string>
#include<cstring>
#include<iostream>
#include "reconstruct.h"
#ifndef CONSTRUCT_FLAGS_H_
#define CONSTRUCT_FLAGS_H_

#include <string>

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

#endif // CONSTRUCT_FLAGS_H_
12 changes: 6 additions & 6 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
#ifndef CONSTRUCT_TYPES_H_
#define CONSTRUCT_TYPES_H_

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

enum CON_BITWIDTH {
BIT8,
Expand Down Expand Up @@ -86,7 +86,7 @@ struct con_cmd {

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

#endif
#endif // CONSTRUCT_TYPES_H_
Loading