Skip to content

Commit

Permalink
Merge pull request #70 from algosup/builder-improve
Browse files Browse the repository at this point in the history
Builder OK
  • Loading branch information
MistzSoftware authored Feb 22, 2024
2 parents 20f1610 + 294cdda commit d367dc5
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 93 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set(CMAKE_C_COMPILER "gcc")

project(
Interpreter-AT2
VERSION 0.4.0
VERSION 0.5.0
LANGUAGES CXX C
)

Expand Down
4 changes: 2 additions & 2 deletions src/2at2.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "error.h"
#include "binExporter.h"

#define VERSION "0.4.0"
#define VERSION "0.5.0"
#define BIN_NAME "bin.2at2"

int main(int argc, char *argv[]) {
Expand Down Expand Up @@ -56,7 +56,7 @@ int main(int argc, char *argv[]) {
// ---------- Parse ----------

// run parser
parseFile(instList, argv[1], varList, errData);
parseFile(instList, argv[1], varList, labelList, errData);

if(flags.debug){
printAst(instList, errData);
Expand Down
3 changes: 2 additions & 1 deletion src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <time.h>

#include "ast.h"
#include "stringPlus.h"


char *getIntCode(enum interruptKind kind, asm_error_t *errData){
Expand Down Expand Up @@ -98,7 +99,7 @@ bool addVar(varList_t *varList, char *name, char *value, long lineNb, asm_error_
// create id
varList->list[i].id = i;
varList->list[i].name = name;
varList->list[i].value = value;
varList->list[i].value = cleanString(value, errData);
return true;
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/binExporter.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ void exportToBin(instList_t *nodeList, char *filename, varList_t *varList, asm_e
} else {
// write the argument
if(node->arg0 != NULL){
fwrite(stringToBinary(node->arg0, errData), 1, sizeof(char)* 8, file);
fwrite(stringToBinary(node->arg0, errData), 1, sizeof(char)*8, file);
}
else if(node->arg1 != NULL){
fwrite(stringToBinary(node->arg1, errData), 1, sizeof(char)* 8, file);
fwrite(stringToBinary(node->arg1, errData), 1, sizeof(char)*8, file);
}
else {
fwrite("00000000", 1, 8, file);
fwrite("00000000", 1, sizeof(char)*8, file);
}
}

Expand Down Expand Up @@ -208,6 +208,9 @@ char *stringToBinary(char *s, asm_error_t *errData) {
return bin;
} else {
// Convert char to byte
if(strcmp(s, "\0")==0){
return "00000000";
}
char *bin = (char *)malloc(strlen(s) * 8 + 1);
if (bin == NULL) {
errorMemAlloc(errData);
Expand Down
17 changes: 13 additions & 4 deletions src/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ void buildNode(instNode_t *node, varList_t *varList, labelList_t *labeList, asm_
case OP_CALL:
buildCall(node, labeList, errData);
break;
case OP_INT:
node->isBuilt = true;
break;
case OP_B_XOR: case OP_DIV: case OP_ADD: case OP_SUB: case OP_MUL: case OP_R_SHIFT: case OP_L_SHIFT: case OP_B_AND: case OP_B_OR: case OP_B_NOT: case OP_MOD:
buildOperation(node, varList, errData);
break;
case OP_PUSH:
node->isBuilt = true;
break;
case OP_RET:
node->isBuilt = true;
break;
Expand All @@ -44,9 +53,9 @@ void buildNode(instNode_t *node, varList_t *varList, labelList_t *labeList, asm_
break;
case OP_LAB:
buildLabel(node, labeList, errData);
break;
case OP_B_XOR: case OP_DIV: case OP_ADD: case OP_SUB: case OP_MUL: case OP_R_SHIFT: case OP_L_SHIFT: case OP_B_AND: case OP_B_OR: case OP_B_NOT: case OP_MOD:
buildOperation(node, varList, errData);
break;
case OP_POP:
node->isBuilt = true;
break;
default:
unknowError("Operation code not found during build", errData);
Expand Down Expand Up @@ -146,7 +155,7 @@ void buildVar(instNode_t *node, varList_t *varList, asm_error_t *errData){

void buildLabel(instNode_t *node, labelList_t *labelList, asm_error_t *errData){
// try to add the label to the list
int labId = addLabel(labelList, node->arg0, node->id, node->lineNb, errData);
int labId = isLabelExist(labelList, node->arg0);
if(labId == -1){
errorLabelNotFound(node->lineNb, node->arg0, errData);
}
Expand Down
67 changes: 37 additions & 30 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void flagsSet(char *flag, flags_t *flags, asm_error_t *errData){
}
}

void parseFile(instList_t *nodeList, char *filename, varList_t *varList, asm_error_t *errData){
void parseFile(instList_t *nodeList, char *filename, varList_t *varList, labelList_t *labelList, asm_error_t *errData){
// check if the file exists
FILE *file = fopen(filename, "r");
if(file == NULL){
Expand All @@ -86,7 +86,7 @@ void parseFile(instList_t *nodeList, char *filename, varList_t *varList, asm_err
}

// parse the line
instNode_t *node = parseLine(line, nodeId, lineNb, varList, errData);
instNode_t *node = parseLine(line, nodeId, lineNb, varList, labelList, errData);
// continue if the line is empty
if(node == NULL){
++ lineNb;
Expand All @@ -108,7 +108,7 @@ void parseFile(instList_t *nodeList, char *filename, varList_t *varList, asm_err
fclose(file);
}

instNode_t *parseLine(char *line, long nodeId, long lineNb, varList_t *varList, asm_error_t *errData){
instNode_t *parseLine(char *line, long nodeId, long lineNb, varList_t *varList, labelList_t *labelList, asm_error_t *errData){
// check if the line is empty or a comment
if(line[0] == '\n' || strncmp(cleanString(line, errData), "//", 2) == 0){
return NULL;
Expand All @@ -133,7 +133,7 @@ instNode_t *parseLine(char *line, long nodeId, long lineNb, varList_t *varList,

bool isThatKind = false;
// Set the instruction
isThatKind = isOp(inst, newNode, varList, errData);
isThatKind = isOp(inst, newNode, varList, labelList, errData);
if(isThatKind){
return newNode;
}
Expand All @@ -143,7 +143,7 @@ instNode_t *parseLine(char *line, long nodeId, long lineNb, varList_t *varList,
return newNode;
}

bool isOp(char *inst, instNode_t *newNode, varList_t *varList, asm_error_t *errData){
bool isOp(char *inst, instNode_t *newNode, varList_t *varList, labelList_t *labelList, asm_error_t *errData){
if(strcmp(inst, "mov") == 0){
newNode->op = OP_MOV;
}
Expand All @@ -166,7 +166,6 @@ bool isOp(char *inst, instNode_t *newNode, varList_t *varList, asm_error_t *errD
if(newNode->arg1 == NULL){
errorNoArg(newNode->lineNb, errData);
}

}
else if(strcmp(inst, "pop") == 0){
newNode->op = OP_POP;
Expand Down Expand Up @@ -236,22 +235,20 @@ bool isOp(char *inst, instNode_t *newNode, varList_t *varList, asm_error_t *errD
}
else if(strcmp(inst, "inc") == 0 || strcmp(inst, "++") == 0){
newNode->op = OP_ADD;
// check if first argument is null
newNode->arg1 = "1";
if(newNode->arg1 == NULL){
errorNoArg(newNode->lineNb, errData);
}
newNode->arg1 = malloc(2);
strcpy(newNode->arg1, "1");

}
else if(strcmp(inst, "dec") == 0 || strcmp(inst, "--") == 0){
newNode->op = OP_SUB;
newNode->arg1 = "1";
// check if first argument is null
if(newNode->arg1 == NULL){
errorNoArg(newNode->lineNb, errData);
}
newNode->arg1 = malloc(2);
strcpy(newNode->arg1, "1");

}
else if(strcmp(inst, "lab") == 0){
newNode->op = OP_LAB;
// add label to labelList
addLabel(labelList, newNode->arg0, newNode->id, newNode->lineNb, errData);
}
else if(strcmp(inst, "var") == 0){
newNode->op = OP_VAR;
Expand All @@ -273,72 +270,82 @@ bool isOp(char *inst, instNode_t *newNode, varList_t *varList, asm_error_t *errD
else if(strcmp(inst, "ngr") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "0";
newNode->inter = INT_EXIT;
}
else if(strcmp(inst, "draw") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "1";
newNode->inter = INT_DRAW;
}
else if(strcmp(inst, "ob1") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "2";
newNode->inter = INT_OB1;
}
else if(strcmp(inst, "if_or") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "3";
newNode->inter = INT_OR;
}
else if(strcmp(inst, "if_and") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "4";
newNode->inter = INT_AND;
}
else if(strcmp(inst, "if_xor") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "5";
newNode->inter = INT_XOR;
}
else if(strcmp(inst, "if_lt") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "6";
newNode->inter = INT_LT;
}
else if(strcmp(inst, "if_lte") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "7";
newNode->inter = INT_LTE;
}
else if(strcmp(inst, "if_gt") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "8";
newNode->inter = INT_GT;
}
else if(strcmp(inst, "if_gte") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "9";
newNode->inter = INT_GTE;
}
else if(strcmp(inst, "if_eq") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "10";
newNode->inter = INT_EQ;
}
else if(strcmp(inst, "if_neq") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "11";
newNode->inter = INT_NEQ;
}
else if(strcmp(inst, "pusha") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "12";
newNode->inter = INT_PUSHA;
}
else if(strcmp(inst, "popa") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->arg0 = "13";
newNode->inter = INT_POPA;
}
else if(strcmp(inst, "else") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->inter = INT_ELSE;
}
else if(strcmp(inst, "end") == 0){
newNode->op = OP_INT;
newNode->isInter = true;
newNode->inter = INT_END;
}
else{
errorInstruction(inst, newNode->lineNb, errData);
Expand Down
11 changes: 8 additions & 3 deletions src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ void flagsSet(char *flag, flags_t *flags, asm_error_t *errData);
params:
nodeList: pointer to the instruction list
filename: name of the file to be parsed
varList: variable list
labelList: label list
errData: error history
*/
void parseFile(instList_t *nodeList, char *filename, varList_t *varList, asm_error_t *errData);
void parseFile(instList_t *nodeList, char *filename, varList_t *varList, labelList_t *labelList, asm_error_t *errData);

/*
Parse a line and return an instruction node
Expand All @@ -51,23 +54,25 @@ void parseFile(instList_t *nodeList, char *filename, varList_t *varList, asm_err
nodeId: id of the node
lineNb: number of the line
varList: variable list
labelList: label list
errData: error history
returns:
instNode_t: instruction node
*/
instNode_t *parseLine(char *line, long nodeId, long lineNb, varList_t *varList, asm_error_t *errData);
instNode_t *parseLine(char *line, long nodeId, long lineNb, varList_t *varList, labelList_t *labelList, asm_error_t *errData);

/*
Read the line and check if it is an operation
params:
char*: instruction to be checked
instNode_t*: pointer to the instruction node
varList_t: variable list
labelList_t: label list
error_t*: error history
returns:
bool: true if it is an operation
*/
bool isOp(char *inst, instNode_t *newNode, varList_t *varList, asm_error_t *errData);
bool isOp(char *inst, instNode_t *newNode, varList_t *varList, labelList_t *labelList, asm_error_t *errData);


/*
Expand Down
Loading

0 comments on commit d367dc5

Please sign in to comment.