Skip to content

Commit

Permalink
Merge pull request #49 from algosup/dev
Browse files Browse the repository at this point in the history
Update main following dev last changes
  • Loading branch information
MaximeAlgosup authored Feb 20, 2024
2 parents 7e1b434 + 50fb990 commit aeebbab
Show file tree
Hide file tree
Showing 17 changed files with 1,639 additions and 1,735 deletions.
6 changes: 5 additions & 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.0.4
VERSION 0.1.1
LANGUAGES CXX C
)

Expand All @@ -25,8 +25,10 @@ add_executable(tests
src/parser.c
src/builder.c
src/debug.c
src/error.c
src/binExporter.c
test/test.cc
src/ast.c
googletest/googletest/src/gtest-all.cc
)

Expand Down Expand Up @@ -60,8 +62,10 @@ add_executable(2at2
src/parser.c
src/builder.c
src/debug.c
src/error.c
src/binExporter.c
src/2at2.c
src/ast.c
)

target_include_directories(2at2
Expand Down
73 changes: 46 additions & 27 deletions src/2at2.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "parser.h"
#include "builder.h"
#include "2at2.h"
#include "debug.h"
#include "error.h"

#define VERSION "0.0.3"
#define VERSION "0.1.1"

int main(int argc, char *argv[]) {
// Init error data history
asm_error_t *errData = initErrorFile("errors.log", argv[1]);

// ---------- Parse arguments ----------

// Parse program arguments and get flags
flags_t flags = parseArgs(argc, argv);

Expand All @@ -22,53 +30,64 @@ int main(int argc, char *argv[]) {
printVersion();
}

fprintf(stderr, "[\t5%%\t] Parsing arguments successfully\n");

// ---------- Init data ----------

// Init variables list struct
varList_t *varList = malloc(sizeof(varList_t));
varList->size = 10;
varList->list = malloc(sizeof(var_t) * varList->size);
// Init variables list
for(size_t i = 0; i < varList->size; i++){
varList->list[i].name = NULL;
}
varList_t *varList = createEmptyVarList();

// Init labels list struct
labelList_t *labelList = malloc(sizeof(labelList_t));
labelList->size = 10;
labelList->list = malloc(sizeof(label_t) * labelList->size);
for(size_t i = 0; i < labelList->size; i++){
labelList->list[i].name = NULL;
}
labelList_t *labelList = createEmptyLabelList();

// Init instructions list struct
instList_t *instList = malloc(sizeof(instList_t));
instList->head = NULL;
instList_t *instList = createEmptyInstList();

fprintf(stderr, "[\t10%%\t] Data initialized successfully\n");

// ---------- Parse ----------

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

if(flags.debug) {
printInstList(instList, "../others/parsing.log");
printVarList(varList, "../others/parsing.log");
printLabelList(labelList, "../others/parsing.log");
if(flags.debug){
printAst(instList);
}

fprintf(stderr, "[\t45%%\t] File parsed successfully\n");

// ---------- Build ----------

// run builder
build(instList, labelList, varList);
buildProgram(instList, varList, labelList, errData);

fprintf(stderr, "[\t70%%\t] File built successfully\n");

if(flags.debug) {
printInstList(instList, "../others/building.log");
printVarList(varList, "../others/building.log");
printLabelList(labelList, "../others/building.log");
if(flags.debug){
printAst(instList);
}

// ---------- Assemble ----------

// run exporter
// TODO: make the function export to binary

fprintf(stderr, "[\t95%%\t] File assembled successfully\n");

// ---------- Print error summary ----------

// Free memory
free(varList);
free(labelList);
free(instList);


fprintf(stderr, "[\t100%%\t] Work success\n");

printErrorSummary(errData);

// Free error data
free(errData);

exit(EXIT_SUCCESS);
}

Expand Down
225 changes: 225 additions & 0 deletions src/ast.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/*
this file is used to define the functions of the ast.h
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>

#include "ast.h"

char *getIntCode(enum interruptKind kind){
switch(kind){
case INT_EXIT:
return "0";
case INT_DRAW:
return "1";
case INT_OB1:
return "2";
case INT_OR:
return "3";
case INT_AND:
return "4";
case INT_XOR:
return "5";
case INT_LT:
return "6";
case INT_LTE:
return "7";
case INT_GT:
return "8";
case INT_GTE:
return "9";
case INT_EQ:
return "10";
case INT_NEQ:
return "11";
case INT_PUSHA:
return "12";
case INT_POPA:
return "13";
case INT_MOV_F_REG:
return "14";
case INT_ELSE:
return "15";
case INT_END:
return "16";
default:
// TODO: transform to error
exit(EXIT_FAILURE);
}
}

varList_t *createEmptyVarList(){
// Init variables list struct
varList_t *varList = malloc(sizeof(varList_t));
varList->size = 10;
varList->list = malloc(sizeof(var_t) * varList->size);
// Init variables list
for(size_t i = 0; i < varList->size; i++){
varList->list[i].name = NULL;
}
return varList;
}

bool addVar(varList_t *varList, char *name, char *value){
// check if the variable already exists
for(size_t i = 0; i < varList->size; i++){
if (varList->list[i].name == NULL){
continue;
}
if(strcmp(varList->list[i].name, name) == 0){
// TODO: add error variable already exists
return false;
}
}
// check if the list is full
bool isFull = true;
for(size_t i = 0; i < varList->size; i++){
if(varList->list[i].name == NULL){
isFull = false;
break;
}
}
if(isFull){
// double the size of the list
varList->size *= 2;
varList->list = realloc(varList->list, sizeof(var_t) * varList->size);
for(size_t i = varList->size / 2; i < varList->size; i++){
varList->list[i].name = NULL;
}
}
// add the variable
for(size_t i = 0; i < varList->size; i++){
if(varList->list[i].name == NULL){
// create id
varList->list[i].id = i;
varList->list[i].name = name;
varList->list[i].value = value;
return true;
}
}
// TODO: throw error
exit(EXIT_FAILURE);
}

int isVarExist(varList_t *varList, char *name){
for(size_t i = 0; i < varList->size; i++){
if(varList->list[i].name == NULL){
continue;
}
if(strcmp(varList->list[i].name, name) == 0){
return i;
}
}
return -1;
}

instList_t *createEmptyInstList(){
instList_t *instList = malloc(sizeof(instList_t));
instList->head = NULL;
return instList;
}

instNode_t *copyInstNode(instNode_t *node){
instNode_t *newNode = (instNode_t *)malloc(sizeof(instNode_t));
if(newNode == NULL){
// TODO: thorw memory alloc error
exit(EXIT_FAILURE);
}
newNode->id = node->id;
newNode->lineNb = node->lineNb;
newNode->op = node->op;
newNode->isInter = node->isInter;
newNode->inter = node->inter;
newNode->arg0 = node->arg0;
newNode->arg1 = node->arg1;
newNode->inputReg = node->inputReg;
newNode->next = node->next;
return newNode;
}

instNode_t *createEmptyInstNode() {
instNode_t *newNode = (instNode_t *)malloc(sizeof(instNode_t));
if (newNode == NULL) {
fprintf(stderr, "Memory allocation error\n");
exit(EXIT_FAILURE);
}
newNode->id = -1;
newNode->lineNb = -1;
newNode->isInter = false;
newNode->arg0 = NULL;
newNode->arg1 = NULL;
newNode->isBuilt = false;
newNode->next = NULL;

return newNode;
}


labelList_t *createEmptyLabelList(){
labelList_t *labelList = malloc(sizeof(labelList_t));
labelList->size = 10;
labelList->list = malloc(sizeof(label_t) * labelList->size);
for(size_t i = 0; i < labelList->size; i++){
labelList->list[i].name = NULL;
}
return labelList;
}

int addLabel(labelList_t *labelList, char *name, long nodeId){
// check if the label already exists
for(size_t i = 0; i < labelList->size; i++){
if (labelList->list[i].name == NULL){
continue;
}
if(strcmp(labelList->list[i].name, name) == 0){
// TODO: add error label already exists
return -1;
}
}

// check if the list is full
bool isFull = true;
for(size_t i = 0; i < labelList->size; i++){
if(labelList->list[i].name == NULL){
isFull = false;
break;
}
}
if(isFull){
// double the size of the list
labelList->size *= 2;
labelList->list = realloc(labelList->list, sizeof(label_t) * labelList->size);
for(size_t i = labelList->size / 2; i < labelList->size; i++){
labelList->list[i].name = NULL;
}
}

// add the label
for(size_t i = 0; i < labelList->size; i++){
if(labelList->list[i].name == NULL){
labelList->list[i].name = name;
labelList->list[i].nodeId = nodeId;
labelList->list[i].id = i;
return i;
}
}

// TODO: throw error
return -1;
}

int isLabelExist(labelList_t *labelList, char *name){
for(size_t i = 0; i < labelList->size; i++){
if(labelList->list[i].name == NULL){
continue;
}
if(strcmp(labelList->list[i].name, name) == 0){
return labelList->list[i].id;
}
}
return -1;
}
Loading

0 comments on commit aeebbab

Please sign in to comment.