Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
0.8.2
Browse files Browse the repository at this point in the history
Added negative number support
Added more functions
Added more commands
Tweaked the --version text
  • Loading branch information
PQCraft authored Mar 3, 2021
1 parent 67fdf3a commit 6c4d158
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 26 deletions.
53 changes: 42 additions & 11 deletions clibasic.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ int runcmd();
int copyStr(char* str1, char* str2);
void copyStrSnip(char* str1, int i, int j, char* str2);
uint8_t getVal(char* inbuf, char* outbuf);
int read_history (const char *filename);
int write_history (const char *filename);

int main(int argc, char *argv[]) {
signal(SIGINT, cleanExit);
Expand All @@ -73,7 +75,7 @@ int main(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v")) {
printf("Command Line Interface BASIC version %s\n", VER);
printf("Copyright (C) PQCraft 2021\n");
printf("Copyright (C) 2021 PQCraft\n");
exit = true;
}
if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
Expand Down Expand Up @@ -217,6 +219,7 @@ uint8_t getType(char* str) {
if (str[0] == '"') {if (str[strlen(str) - 1] != '"') {return 0;} return 1/*STR*/;}
bool p = false;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == '-') {} else
if ((str[i] < '0' || str[i] > '9') && str[i] != '.') {return 255 /*VAR*/;} else
if (str[i] == '.') {if (p) {return 0 /*ERR*/;} p = true;}
}
Expand Down Expand Up @@ -287,10 +290,11 @@ uint8_t getFunc(char* inbuf, char* outbuf) {

uint8_t getVar(char* vn, char* varout) {
if (debug) printf("getVar: vn: {%s}\n", vn);
if (vn[0] == '\0') return 0;
if (vn[strlen(vn) - 1] == ')') {
return getFunc(vn, varout);
}
if (vn[0] == '\0') return 0;
for (int i = 0; vn[i] != '\0'; i++) {if (vn[i] >= 'a' && vn[i] <= 'z') vn[i] = vn[i] - 32;}
int v = -1;
if (debug) printf("getVar: v: [%d]\n", v);
for (int i = 0; i < varmaxct; i++) {
Expand All @@ -310,6 +314,7 @@ uint8_t getVar(char* vn, char* varout) {
}

void setVar(char* vn, char* val, uint8_t t) {
for (int i = 0; vn[i] != '\0'; i++) {if (vn[i] >= 'a' && vn[i] <= 'z') vn[i] = vn[i] - 32;}
if (debug) printf("setVar: vn: {%s}\n", vn);
if (debug) printf("setVar: val: {%s}\n", val);
int v = -1;
Expand Down Expand Up @@ -346,6 +351,23 @@ void setVar(char* vn, char* val, uint8_t t) {
return;
}

bool gvchkchar(char* tmp, int i) {
if (isSpChar(tmp, i + 1)) {
if (tmp[i + 1] == '-') {
if (isSpChar(tmp, i + 2)) {
cerr = 1; return false;
}
} else {
cerr = 1; return false;
}
} else {
if (isSpChar(tmp, i - 1)) {
cerr = 1; return false;
}
}
return true;
}

uint8_t getVal(char* tmpinbuf, char* outbuf) {
if (tmpinbuf[0] == '\0') {return 255;}
char inbuf[32768];
Expand All @@ -360,8 +382,10 @@ uint8_t getVal(char* tmpinbuf, char* outbuf) {
double num2 = 0;
double num3 = 0;
int numAct;
if (inbuf[0] == '"') dt = 1;
if (debug) printf("checking for syntax error\n");
if (isSpChar(inbuf, 0) || isSpChar(inbuf, strlen(inbuf) - 1)) {cerr = 1; return 0;}
if ((isSpChar(inbuf, 0) && inbuf[0] != '-') || isSpChar(inbuf, strlen(inbuf) - 1)) {cerr = 1; return 0;}
if (inbuf[0] != '"' && (inbuf[0] == '-' && isSpChar(inbuf, 1))) {cerr = 1; return 0;}
if (debug) printf("no syntax error detected\n");
int tmpct = 0;
tmp[0][0] = 0; tmp[1][0] = 0; tmp[2][0] = 0; tmp[3][0] = '"'; tmp[3][1] = 0;
Expand Down Expand Up @@ -427,23 +451,28 @@ uint8_t getVal(char* tmpinbuf, char* outbuf) {
if (t == 2) {
copyStr(inbuf, tmp[0]);
int p1, p2, p3;
bool inStr = false;
while (true) {
numAct = 0;
p1 = 0, p2 = 0, p3 = 0;
if (debug) printf("checking for exp\n");
for (int i = 0; tmp[0][i] != '\0' && p2 == 0; i++) {
if (tmp[0][i] == '^') {p2 = i; numAct = 4;}
if (tmp[0][i] == '"') inStr = !inStr;
if (tmp[0][i] == '^' && !inStr) {if (!gvchkchar(tmp[0], i)) {return 0;} p2 = i; numAct = 4;}
}
if (debug) printf("checking for mlt/dvd\n");
for (int i = 0; tmp[0][i] != '\0' && p2 == 0; i++) {
if (tmp[0][i] == '*') {p2 = i; numAct = 2;}
if (tmp[0][i] == '/') {p2 = i; numAct = 3;}
if (tmp[0][i] == '"') inStr = !inStr;
if (tmp[0][i] == '*' && !inStr) {if (!gvchkchar(tmp[0], i)) {return 0;} p2 = i; numAct = 2;}
if (tmp[0][i] == '/' && !inStr) {if (!gvchkchar(tmp[0], i)) {return 0;} p2 = i; numAct = 3;}
}
if (debug) printf("checking for add/sub\n");
for (int i = 0; tmp[0][i] != '\0' && p2 == 0; i++) {
if (tmp[0][i] == '+') {p2 = i; numAct = 0;}
if (tmp[0][i] == '-') {p2 = i; numAct = 1;}
if (tmp[0][i] == '"') inStr = !inStr;
if (tmp[0][i] == '+' && !inStr) {if (!gvchkchar(tmp[0], i)) {return 0;} p2 = i; numAct = 0;}
if (tmp[0][i] == '-' && !inStr) {if (!gvchkchar(tmp[0], i)) {return 0;} p2 = i; numAct = 1;}
}
inStr = false;
if (debug) printf("getVal: p1: [%d], p2: [%d], p3: [%d]\n", p1, p2, p3);
if (debug) printf("getVal: tmp[0]: {%s}, tmp[1]: {%s}, tmp[2]: {%s}, tmp[3]: {%s}\n", tmp[0], tmp[1], tmp[2], tmp[3]);
if (p2 == 0) {
Expand All @@ -457,10 +486,12 @@ uint8_t getVal(char* tmpinbuf, char* outbuf) {
}
tmp[1][0] = 0; tmp[2][0] = 0; tmp[3][0] = 0;
for (int i = p2 - 1; i > 0; i--) {
if (isSpChar(tmp[0], i)) {p1 = i; break;}
if (tmp[0][i] == '"') inStr = !inStr;
if (isSpChar(tmp[0], i) && !inStr) {p1 = i; break;}
}
for (int i = p2 + 1; true; i++) {
if (isSpChar(tmp[0], i) || tmp[0][i] == '\0') {p3 = i; break;}
if (tmp[0][i] == '"') inStr = !inStr;
if ((isSpChar(tmp[0], i) && i != p2 + 1 && !inStr) || tmp[0][i] == '\0') {p3 = i; break;}
}
copyStrSnip(tmp[0], p1, p2, tmp[2]);
t = getType(tmp[2]);
Expand Down Expand Up @@ -489,7 +520,7 @@ uint8_t getVal(char* tmpinbuf, char* outbuf) {
if (tmp[2][strlen(tmp[2]) - 1] == '.') {tmp[2][strlen(tmp[2]) - 1] = 0;}
if (debug) printf("getVal: tmp[0]: {%s}, tmp[1]: {%s}, tmp[2]: {%s}, tmp[3]: {%s}\n", tmp[0], tmp[1], tmp[2], tmp[3]);
copyStrSnip(tmp[0], p3, strlen(tmp[0]), tmp[3]);
if (p1 != 0) copyStrSnip(tmp[0], 0, p1 + 1, tmp[1]);
if (p1 != 0) copyStrSnip(tmp[0], 0, p1, tmp[1]);
copyStrApnd(tmp[2], tmp[1]);
copyStrApnd(tmp[3], tmp[1]);
copyStr(tmp[1], tmp[0]);
Expand Down
14 changes: 14 additions & 0 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,17 @@ if (!strcmp(arg[0], "$PROMPT")) {
copyStr(tmpargs[1], prompt);
goto cmderr;
}
if (!strcmp(arg[0], "$SAVE_CMD_HST")) {
cerr = 0;
if (argct != 1) {cerr = 3; goto cmderr;}
if (argt[1] != 1) {cerr = 2; goto cmderr;}
write_history(arg[1]);
goto cmderr;
}
if (!strcmp(arg[0], "$LOAD_CMD_HST")) {
cerr = 0;
if (argct != 1) {cerr = 3; goto cmderr;}
if (argt[1] != 1) {cerr = 2; goto cmderr;}
read_history(arg[1]);
goto cmderr;
}
55 changes: 40 additions & 15 deletions functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,47 @@ if (!strcmp(farg[0], "RND") || !strcmp(farg[0], "RAND")) {
sprintf(outbuf, "%lf", randNum(min, max));
goto fexit;
}
if (!strcmp(farg[0], "RNDINT") || !strcmp(farg[0], "RANDINT")) {
if (!strcmp(farg[0], "INT")) {
cerr = 0;
ftype = 2;
double min = 0;
double max;
if (fargct == 1) {
if (fargt[1] != 2) {cerr = 2; goto fexit;}
sscanf(farg[1], "%lf", &max);
} else if (fargct == 2) {
if (fargt[1] + fargt[2] != 4) {cerr = 2; goto fexit;}
sscanf(farg[1], "%lf", &min);
sscanf(farg[2], "%lf", &max);
} else {
cerr = 3;
goto fexit;
}
sprintf(outbuf, "%d", (int)round(randNum(min, max)));
if (fargct != 1) {cerr = 3; goto fexit;}
if (fargt[1] != 2) {cerr = 2; goto fexit;}
double dbl;
sscanf(farg[1], "%lf", &dbl);
sprintf(outbuf, "%d", (int)dbl);
int i;
for (i = 0; outbuf[i] != '.' && outbuf[i] != '\0'; i++) {}
outbuf[i] = 0;
goto fexit;
}
if (!strcmp(farg[0], "CINT")) {
cerr = 0;
ftype = 2;
if (fargct != 1) {cerr = 3; goto fexit;}
if (fargt[1] != 2) {cerr = 2; goto fexit;}
double dbl;
sscanf(farg[1], "%lf", &dbl);
dbl = round(dbl);
sprintf(outbuf, "%d", (int)dbl);
goto fexit;
}
if (!strcmp(farg[0], "STR")) {
cerr = 0;
ftype = 1;
if (fargct != 1) {cerr = 3; goto fexit;}
if (fargt[1] != 2) {cerr = 2; goto fexit;}
copyStr(farg[1], outbuf);
goto fexit;
}
if (!strcmp(farg[0], "VAL")) {
cerr = 0;
ftype = 2;
if (fargct != 1) {cerr = 3; goto fexit;}
if (fargt[1] != 1) {cerr = 2; goto fexit;}
double dbl;
sscanf(farg[1], "%lf", &dbl);
sprintf(outbuf, "%lf", dbl);
ftype = getVal(outbuf, outbuf);
if (ftype == 1) {cerr = 2; goto fexit;}
goto fexit;
}

0 comments on commit 6c4d158

Please sign in to comment.