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
25 changes: 0 additions & 25 deletions examples/factorial.asm

This file was deleted.

3 changes: 2 additions & 1 deletion examples/factorial.con
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function main():
!result rax
call printf(fmt, result)

syscall exit()
mov rax, 60
syscall

section .data
fmt: db "%d", 10, 0
33 changes: 0 additions & 33 deletions examples/strchr.asm

This file was deleted.

14 changes: 6 additions & 8 deletions examples/strchr.con
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@ section .text
function strchr(str, chr):
!ptrresult rax
!findchr sil
!end_of_str 0
!nullptr 0
mov ptrresult, nullptr
while byte[str] ne end_of_str:
mov ptrresult, 0
while byte[str] ne 0:
if byte[str] e findchr:
mov ptrresult, str
ret
inc str

function main():
!W_letter 87
call strchr(teststr, W_letter)
call strchr(teststr, 87)
!result rax
call printf(fmt, result)
call printf(fmt, teststr)

syscall exit()
mov rax, 60
syscall

section .data
teststr: db "Hello World!", 0
fmt: db "%p", 10, 0
teststr: db "Hello World!", 0

32 changes: 0 additions & 32 deletions examples/strlwr.asm

This file was deleted.

21 changes: 9 additions & 12 deletions examples/strlwr.con
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@ extern printf

section .text
function strlwr(str):
!A_letter 65
!Z_letter 90
!end_of_str 0
!A_to_a 32
while byte[str] ne end_of_str:
if byte[str] ge A_letter:
if byte[str] le Z_letter:
while byte[str] ne 0:
if byte[str] ge 65:
if byte[str] le 90:
!crntchr sil
mov crntchr, byte[str]
add crntchr, A_to_a
add crntchr, 32
mov byte[str], crntchr
inc str


function main():
call strlwr(teststr)
call printf(fmt, teststr)
call strlwr(teststring)
call printf(fmt, teststring)

syscall exit()
mov rax, 60
syscall

section .data
teststr: db "HeLlO WoRlD", 0
teststring db "HeLlO WoRlD", 0
fmt: db "%s", 10, 0

22 changes: 5 additions & 17 deletions src/construct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
#include <fstream>
#include <sstream>
#include "construct_types.h"
#include "deconstruct.h"
#include "reconstruct.h"
#include "construct_flags.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) {
Expand Down Expand Up @@ -40,23 +39,12 @@ int main(int argc, char** argv)
apply_ifs(tokens);
apply_whiles(tokens);
apply_funcalls(tokens);
apply_syscalls(tokens);
std::vector<con_macro*> empty_macros; // pointer to con_macros in tokens, not a copy
std::vector<con_macro> empty_macros;
apply_macros(tokens, empty_macros);
empty_macros.clear(); // remove the pointers to con_macro, not the con_macro objects themselves
linearize_tokens(tokens);

std::ofstream outfile;
outfile.open(outpath);
outfile << tokens_to_nasm(tokens);
outfile.close();

for (std::vector<con_token*>::reverse_iterator r_it = tokens.rbegin(); r_it != tokens.rend(); ++r_it) {
delete *r_it;
*r_it = nullptr;
}
tokens.clear();
glob_cmd = nullptr; // deleted in tokens vector
glob_tok = nullptr; // deleted in tokens vector
return 0;
}
16 changes: 5 additions & 11 deletions src/construct_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include "construct_types.h"
#include "reconstruct.h" // comparison_to_string()

std::string tokentype_to_string(CON_TOKENTYPE type)
{
std::string tokentype_to_string(CON_TOKENTYPE type) {
switch (type) {
case SECTION:
return "section";
Expand All @@ -17,21 +16,18 @@ std::string tokentype_to_string(CON_TOKENTYPE type)
case IF:
return "if";
case FUNCTION:
return "function";
return "func";
case CMD:
return "cmd";
case MACRO:
return "macro";
case FUNCALL:
return "funcall";
case SYSCALL:
return "syscall";
}
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) {
case SECTION:
Expand All @@ -41,12 +37,10 @@ std::string token_to_string(con_token token)
tokstring += ", name: " + token.tok_tag->name;
break;
case WHILE:
tokstring += ", condition: " + token.tok_while->condition.arg1 + " "
+ comparison_to_string(token.tok_while->condition.op) + " " + token.tok_while->condition.arg2;
tokstring += ", condition: " + token.tok_while->condition.arg1 + " " + comparison_to_string(token.tok_while->condition.op) + " " + token.tok_while->condition.arg2;
break;
case IF:
tokstring += ", condition: " + token.tok_if->condition.arg1 + " "
+ comparison_to_string(token.tok_if->condition.op) + " " + token.tok_if->condition.arg2;
tokstring += ", condition: " + token.tok_if->condition.arg1 + " " + comparison_to_string(token.tok_if->condition.op) + " " + token.tok_if->condition.arg2;
break;
case FUNCTION:
tokstring += ", function: " + token.tok_function->name + ", arguments: ";
Expand Down
6 changes: 2 additions & 4 deletions src/construct_flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ extern CON_BITWIDTH bitwidth;

using namespace std;

int set_bitwidth(char* argv)
{
int set_bitwidth(char* argv) {
if (string(argv) == "elf8") {
bitwidth = BIT8;
return 0;
Expand All @@ -29,8 +28,7 @@ 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, string* path, string* outpath) {
bool bitwidth_set = false;
bool path_set = false;
bool outpath_set = false;
Expand Down
72 changes: 16 additions & 56 deletions src/construct_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,22 @@ enum CON_TOKENTYPE {
FUNCTION,
CMD,
MACRO,
FUNCALL,
SYSCALL
FUNCALL
};


struct con_token {
CON_TOKENTYPE tok_type;
int indentation;
struct con_section* tok_section;
struct con_tag* tok_tag;
struct con_while* tok_while;
struct con_if* tok_if;
struct con_function* tok_function;
struct con_cmd* tok_cmd;
struct con_macro* tok_macro;
struct con_funcall* tok_funcall;
std::vector<con_token*> tokens; // Only non-empty for if, while and function tokens
};

struct _con_condition {
Expand All @@ -38,7 +52,6 @@ struct _con_condition {
std::string arg2;
};


struct con_section {
std::string name;
};
Expand Down Expand Up @@ -76,57 +89,4 @@ struct con_funcall {
std::vector<std::string> arguments;
};

struct con_syscall {
uint16_t number;
std::vector<std::string> arguments;
};


struct con_token {
CON_TOKENTYPE tok_type;
int indentation;
con_section* tok_section = nullptr;
con_tag* tok_tag = nullptr;
con_while* tok_while = nullptr;
con_if* tok_if = nullptr;
con_function* tok_function = nullptr;
con_cmd* tok_cmd = nullptr;
con_macro* tok_macro = nullptr;
con_funcall* tok_funcall = nullptr;
con_syscall* tok_syscall = nullptr;
std::vector<con_token*> tokens; // relevant to "if", "while", "function" and "syscall" tokens

~con_token() {
switch (tok_type) {
case SECTION:
if (tok_section != nullptr) delete tok_section;
break;
case TAG:
if (tok_tag != nullptr) delete tok_tag;
break;
case WHILE:
if (tok_while != nullptr) delete tok_while;
break;
case IF:
if (tok_if != nullptr) delete tok_if;
break;
case FUNCTION:
if (tok_function != nullptr) delete tok_function;
break;
case CMD:
if (tok_cmd != nullptr) delete tok_cmd;
break;
case MACRO:
if (tok_macro != nullptr) delete tok_macro;
break;
case FUNCALL:
if (tok_funcall != nullptr) delete tok_funcall;
break;
case SYSCALL:
if (tok_syscall != nullptr) delete tok_syscall;
break;
}
}
};

#endif // CONSTRUCT_TYPES_H_
Loading