Skip to content

Commit

Permalink
Rename data types. Fix bug with CallExpression.
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-is-coding committed May 6, 2023
1 parent 4c9f6b6 commit e25b2ac
Show file tree
Hide file tree
Showing 16 changed files with 316 additions and 257 deletions.
32 changes: 16 additions & 16 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#include "main.h"

static bool check(Array *, bool);
static bool isLang(string);
static bool isFile(string);
static Lang parseLang(string);
static void init(int, string *, Array *);
static boolean check(Array *, boolean);
static boolean isLang(char *);
static boolean isFile(char *);
static Lang parseLang(char *);
static void init(int, char *[], Array *);

static struct Args {
Lang lang;
Mode mode;
bool set;
string file;
boolean set;
char *file;
} args;

void init(int argc, string *argv, Array *errs) {
void init(int argc, char *argv[], Array *errs) {
args.lang = L_UNKN;
args.mode = INTERPRET;
args.set = false;
args.file = NULL;
for (small i = 1; i < argc; i++) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--compile") == 0) {
if (args.set && args.mode == INTERPRET)
pushArray(errs, clierror(3, "\x1b[96m--interpret", " and \x1b[96m--compile", " are mutually exclusive"));
Expand All @@ -40,15 +40,15 @@ void init(int argc, string *argv, Array *errs) {
if (args.mode == INTERPRET && !args.file) pushArray(errs, clierror(1, "REPL interpret mode not available yet"));
}

int main(int argc, string *argv) {
int main(int argc, char *argv[]) {
Array *errors = newArray(1);
init(argc, argv, errors);

if (check(errors, false)) return -1;

FILE *fp = fopen(args.file, "rb");
if (fp == NULL) {
string e = utfcat("file \x1b[97m", args.file);
char *e = utfcat("file \x1b[97m", args.file);
pushArray(errors, clierror(2, e, "\x1b[0;91m does not exist"));
free(e);
}
Expand All @@ -58,7 +58,7 @@ int main(int argc, string *argv) {
size_t fsize = ftell(fp);
rewind(fp);

string contents = malloc(fsize+1);
char *contents = malloc(fsize+1);

if (contents == NULL) pushArray(errors, clierror(1, "could not allocate enough space for file"));
if (check(errors, false)) return -1;
Expand Down Expand Up @@ -86,7 +86,7 @@ int main(int argc, string *argv) {
free(p);
}

bool check(Array *errors, bool drop) {
boolean check(Array *errors, boolean drop) {
if (errors->l) {
printErrors(errors);
if (drop) freeArray(errors);
Expand All @@ -95,7 +95,7 @@ bool check(Array *errors, bool drop) {
return 0;
}

bool isLang(string arg) {
boolean isLang(char *arg) {
for (size_t i = 0; i < strlen(arg); i++) {
if (
!((arg[i] >= 'a' && arg[i] <= 'z')
Expand All @@ -106,7 +106,7 @@ bool isLang(string arg) {
return true;
}

bool isFile(string arg) {
boolean isFile(char *arg) {
for (size_t i = 0; i < strlen(arg); i++) {
if (!(
(arg[i] >= 'a' && arg[i] <= 'z')
Expand All @@ -121,7 +121,7 @@ bool isFile(string arg) {
return true;
}

Lang parseLang(string arg) {
Lang parseLang(char *arg) {
if (strcasecmp(arg, "js") == 0 || strcasecmp(arg, "javascript") == 0) {
return L_JS;
} else if (strcasecmp(arg, "asm") == 0 || strcasecmp(arg, "assembly") == 0) {
Expand Down
5 changes: 4 additions & 1 deletion main.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ typedef enum __attribute__((__packed__))Modes {
DEBUG
} Mode;

extern int main(int, string *);
// declare strcasecmp for C99 compatibility
int strcasecmp(const char *s1, const char *s2);

extern int main(int, char *[]);
12 changes: 6 additions & 6 deletions src/compilers/c.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "c.h"

static void compile(Typed *v, small indent, FILE *fp);
static void compile(Typed *v, int indent, FILE *fp);
static void root_compile(Program *p, FILE *fp);
static void tab(small i, FILE *fp);
static void tab(int i, FILE *fp);
static char *getBinaryOperator(BinaryOperator);

void tab(small i, FILE *fp) {
for (small j = 0; j < i; j++) {
void tab(int i, FILE *fp) {
for (int j = 0; j < i; j++) {
fputs("\t", fp);
}
}
Expand All @@ -30,14 +30,14 @@ char *getBinaryOperator(BinaryOperator oper) {
}
}

void compile(Typed *v, small indent, FILE *fp) {
void compile(Typed *v, int indent, FILE *fp) {
switch(v->type) {
case BINARY_EXPRESSION: {
BinaryExpression *expr = (BinaryExpression *)v;
// tab(indent);
compile((Typed *)expr->left, indent+1, fp);
fputc(' ', fp);
fputs(getBinaryOperator(expr->operator), fp);
fputs(getBinaryOperator(expr->oper), fp);
fputc(' ', fp);
compile((Typed *)expr->right, indent+1, fp);
// printf(";\n");
Expand Down
14 changes: 7 additions & 7 deletions src/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ CLIError *clierror(int argc, ...) {
va_list v0;
va_list v1;
va_start(v0, argc);
for (small i = 1; i <= argc; i++) {
s += strlen(va_arg(v0, string));
for (int i = 1; i <= argc; i++) {
s += strlen(va_arg(v0, char *));
}; va_end(v0);
string e = malloc(s+1);
char *e = malloc(s+1);
strcpy(e, "");
va_start(v1, argc);
for (small i = 1; i <= argc; i++) {
strcat(e, va_arg(v1, string));
for (int i = 1; i <= argc; i++) {
strcat(e, va_arg(v1, char *));
}; va_end(v1);
CLIError *err = malloc(sizeof(CLIError));
err->type = CLIERROR;
err->data = e;
return err;
}

Error *error(string name, string file, string data, Token *token) {
Error *error(char *name, char *file, char *data, Token *token) {
Error *err = malloc(sizeof(Error));
string n = malloc(strlen("Error") + strlen(name) + 1);
char *n = malloc(strlen("Error") + strlen(name) + 1);
strcpy(n, name);
strcat(n, "Error");
err->type = ERROR;
Expand Down
10 changes: 5 additions & 5 deletions src/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ typedef enum __attribute__((__packed__)) ErrorTypes {

typedef struct Error {
ErrorType type;
string file;
string name;
string data;
char *file;
char *name;
char *data;
Token *token;
} Error;
typedef struct CLIError {
ErrorType type;
string data;
char *data;
} CLIError;
typedef struct ErrorLike {
ErrorType type;
} ErrorLike;

extern CLIError *clierror(int arg_count, ...);
extern Error *error(string error_name, string file, string data, Token* token);
extern Error *error(char *error_name, char *file, char *data, Token* token);

#endif
Loading

0 comments on commit e25b2ac

Please sign in to comment.