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

Commit

Permalink
0.12.1
Browse files Browse the repository at this point in the history
Fixed exponent operation parsing
  • Loading branch information
PQCraft authored Mar 25, 2021
1 parent d815f4e commit 99591a1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 38 deletions.
39 changes: 29 additions & 10 deletions clibasic.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <readline/readline.h>
#include <readline/history.h>

char VER[] = "0.12";
char VER[] = "0.12.1";

#ifdef B32
char BVER[] = "32";
Expand Down Expand Up @@ -190,7 +190,7 @@ int main(int argc, char *argv[]) {
conbuf[0] = 0;
if (chkinProg) {inProg = true; chkinProg = false;}
if (!inProg) {
if (runfile) {if (textlock) {tcsetattr(0, TCSANOW, &restore); textlock = false;} cleanExit();}
if (runfile) {if (textlock) {tcsetattr(0, TCSANOW, &restore); textlock = false;} err = cerr; cleanExit();}
char *tmpstr = NULL;
int tmpt = getVal(prompt, pstr);
if (tmpt != 1) strcpy(pstr, "CLIBASIC> ");
Expand Down Expand Up @@ -346,7 +346,7 @@ void loadProg() {
if (tmpc == '"') inStr = !inStr;
if (tmpc == '\'' && !inStr) comment = true;
if (tmpc == '\n') comment = false;
if (tmpc == '\r') tmpc = ' ';
if (tmpc == '\r' || tmpc == '\t') tmpc = ' ';
if (!comment) {progbuf[j] = (char)tmpc; j++;}
}
if (j == 0) j = 1;
Expand Down Expand Up @@ -698,6 +698,7 @@ uint8_t getVal(char* tmpinbuf, char* outbuf) {
if (tmp[0][i] == '-' && !inStr && pct == 0) {if (!gvchkchar(tmp[0], i)) {return 0;} p2 = i; numAct = 1;}
}
inStr = false;
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) {
if (p3 == 0) {
t = getType(tmp[0]);
Expand Down Expand Up @@ -727,17 +728,27 @@ uint8_t getVal(char* tmpinbuf, char* outbuf) {
t = getType(tmp[3]);
if (t == 0) {cerr = 1; return 0;} else
if (t == 255) {t = getVar(tmp[3], tmp[3]); if (t == 0) {return 0;} if (t != 2) {cerr = 2; return 0;}}
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]);
sscanf(tmp[2], "%lf", &num1);
sscanf(tmp[3], "%lf", &num2);
if (debug) printf("num1: [%lf], num2: [%lf], num3: [%lf]\n", num1, num2, num3);
switch (numAct) {
case 0: num3 = num1 + num2; break;
case 1: num3 = num1 - num2; break;
case 2: num3 = num1 * num2; break;
case 3: if (num2 == 0) {cerr = 5; return 0;} num3 = num1 / num2; break;
case 4: num3 = pow(num1, num2); break;
case 4:
if (num1 == 0) {if (num2 == 0) {cerr = 5; return 0;} num3 = 0; break;}
if (num2 == 0) {num3 = 1; break;}
num3 = pow(num1, num2);
if (num1 < 0 && num3 > 0) {num3 *= -1;}
break;
}
if (debug) printf("num1: [%lf], num2: [%lf], num3: [%lf]\n", num1, num2, num3);
tmp[1][0] = 0;
sprintf(tmp[2], "%lf", num3);
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 (dt == 2) {
bool dp = false;
int i = 0;
Expand All @@ -763,8 +774,12 @@ uint8_t getVal(char* tmpinbuf, char* outbuf) {
while (tmp[1][i] != 0) {if (tmp[1][i] == '.') {dp = true;} i++;}
while (dp && tmp[1][strlen(tmp[1]) - 1] == '0') {tmp[1][strlen(tmp[1]) - 1] = 0;}
if (dp && tmp[1][strlen(tmp[1]) - 1] == '.') {tmp[1][strlen(tmp[1]) - 1] = 0;}
i = 0;
while (tmp[1][i] == '0' && tmp[1][i + 1] != 0) {i++;}
copyStrSnip(tmp[1], i, strlen(tmp[1]), outbuf);
} else {
copyStr(tmp[1], outbuf);
}
copyStr(tmp[1], outbuf);
if (outbuf[0] == 0 && dt != 1) {outbuf[0] = '0'; outbuf[1] = 0; return 2;}
if (debug) printf("output: getVal(\"%s\", \"%s\");\n", inbuf, outbuf);
return (uint8_t)dt;
Expand Down Expand Up @@ -914,8 +929,6 @@ uint8_t logictest(char* inbuf) {
if (t1 != t2) {cerr = 2; return -1;}
if (!qstrcmp(tmp[1], "=")) {
return (uint8_t)(bool)!qstrcmp(tmp[0], tmp[2]);
} else if (!qstrcmp(tmp[1], "!=")) {
return (uint8_t)(bool)qstrcmp(tmp[0], tmp[2]);
} else if (!qstrcmp(tmp[1], "<>")) {
return (uint8_t)(bool)qstrcmp(tmp[0], tmp[2]);
} else if (!qstrcmp(tmp[1], ">")) {
Expand Down Expand Up @@ -1005,14 +1018,14 @@ void runcmd() {
#include "commands.c"
if (debug) printf("cerr: [%d]\n", cerr);
cmderr:
if (cerr != 0) {
if (cerr) {
getCurPos();
if (curx != 1) printf("\n");
if (inProg) {printf("Error %d on line %d of %s: '%s': ", cerr, progLine, progFilename, cmd);}
else {printf("Error %d: ", cerr);}
switch (cerr) {
default:
printf("\b\b \b\b");
printf("\b\b \b");
break;
case 1:
printf("Syntax");
Expand All @@ -1027,7 +1040,7 @@ void runcmd() {
printf("Invalid variable name: '%s'", errstr);
break;
case 5:
printf("Divide by zero");
printf("Operation results in undefined");
break;
case 6:
printf("LOOP without DO");
Expand Down Expand Up @@ -1065,6 +1078,12 @@ void runcmd() {
case 127:
printf("Not a function: '%s'", errstr);
break;
case 253:
printf("Command not valid in command-line: '%s'", arg[0]);
break;
case 254:
printf("Command not valid in program: '%s'", arg[0]);
break;
case 255:
printf("Not a command: '%s'", arg[0]);
break;
Expand Down
53 changes: 40 additions & 13 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,71 @@ if (!qstrcmp(arg[0], "PRINT") || !qstrcmp(arg[0], "?")) {
}
if (!qstrcmp(arg[0], "COLOR")) {
cerr = 0;
int tmp = 0;
if (argct > 2 || argct < 1) {cerr = 3; goto cmderr;}
if (!solvearg(1)) goto cmderr;
if (argt[1] == 0) {} else
if (argt[1] != 2) {cerr = 2; goto cmderr;}
else {fgc = (uint8_t)atoi(arg[1]);}
else {
tmp = atoi(arg[1]);
if (tmp < 0 || tmp > 255) {cerr = 16; goto cmderr;}
fgc = (uint8_t)tmp;
printf("\e[38;5;%um", fgc);
}
if (argct > 1) {
if (!solvearg(2)) goto cmderr;
if (argt[2] == 0) {} else
if (argt[2] != 2) {cerr = 2; goto cmderr;}
else {bgc = (uint8_t)atoi(arg[2]);}
else {
tmp = atoi(arg[2]);
if (tmp < 0 || tmp > 255) {cerr = 16; goto cmderr;}
fgc = (uint8_t)tmp;
printf("\e[48;5;%um", bgc);
}
}
if (debug) printf("CMD[COLOR]: fgc: [%u], bgc: [%u]\n", fgc, bgc);
printf("\e[38;5;%um\e[48;5;%um", fgc, bgc);
fflush(stdout);
}
if (!qstrcmp(arg[0], "SET") || !qstrcmp(arg[0], "LET")) {
cerr = 0;
if (argct != 2) {cerr = 3; goto cmderr;}
solvearg(1);
if (!solvearg(2)) goto cmderr;
if (tmpargs[0][0] == 0 || tmpargs[0][0] == ' ') {cerr = 1; goto cmderr;}
if (argt[1] == 0 || argt[2] == 0) {cerr = 1; goto cmderr;}
int v = -1;
for (int i = 0; i < varmaxct; i++) {
if (varinuse[i] && !qstrcmp(arg[1], varname[i])) {v = i; break;}
}
if (argt[1] != argt[2] && v != -1) {cerr = 2; goto cmderr;}
if (getType(tmpargs[1]) != 255) {cerr = 3; goto cmderr;}
if (getType(tmpargs[1]) != 255) {
cerr = 4;
errstr = realloc(errstr, (strlen(tmpargs[1]) + 1) * sizeof(char));
copyStr(tmpargs[1], errstr);
goto cmderr;
}
if (!setVar(tmpargs[1], arg[2], argt[2])) goto cmderr;
}
if (!qstrcmp(arg[0], "LOCATE")) {
cerr = 0;
int tmp = 0;
if (argct > 2 || argct < 1) {cerr = 3; goto cmderr;}
if (!solvearg(1)) goto cmderr;
if (!solvearg(2)) goto cmderr;
if (argt[1] == 0) {} else
if (argt[1] != 2) {cerr = 2; goto cmderr;}
else {cury = atoi(arg[1]);}
else {
tmp = atoi(arg[1]);
if (tmp < 0) {cerr = 16; goto cmderr;}
curx = tmp;
}
if (argct > 1) {
if (!solvearg(2)) goto cmderr;
if (argt[2] == 0) {} else
if (argt[2] != 2) {cerr = 2; goto cmderr;}
else {curx = atoi(arg[2]);}
else {
tmp = atoi(arg[2]);
if (tmp < 0) {cerr = 16; goto cmderr;}
cury = tmp;
}
}
if (curx < 0) {getCurPos(); cerr = 16; goto cmderr;}
if (cury < 0) {getCurPos(); cerr = 16; goto cmderr;}
printf("\e[%d;%dH", cury, curx);
fflush(stdout);
}
Expand Down Expand Up @@ -141,20 +163,23 @@ if (!qstrcmp(arg[0], "RESETTIMER")) {
resetTimer();
}
if (!qstrcmp(arg[0], "$PROMPT")) {
if (inProg) {cerr = 254; goto cmderr;}
cerr = 0;
if (argct != 1) {cerr = 3; goto cmderr;}
if (!solvearg(1)) goto cmderr;
if (argt[1] != 1) {cerr = 2; goto cmderr;}
copyStr(tmpargs[1], prompt);
}
if (!qstrcmp(arg[0], "$SAVECMDHST")) {
if (inProg) {cerr = 254; goto cmderr;}
cerr = 0;
if (argct != 1) {cerr = 3; goto cmderr;}
if (!solvearg(1)) goto cmderr;
if (argt[1] != 1) {cerr = 2; goto cmderr;}
write_history(arg[1]);
}
if (!qstrcmp(arg[0], "$LOADCMDHST")) {
if (inProg) {cerr = 254; goto cmderr;}
cerr = 0;
if (argct != 1) {cerr = 3; goto cmderr;}
if (!solvearg(1)) goto cmderr;
Expand Down Expand Up @@ -251,13 +276,15 @@ if (!qstrcmp(arg[0], "$TXTUNLOCK")) {
if (textlock) tcsetattr(0, TCSANOW, &restore);
textlock = false;
}
if (!inProg && !qstrcmp(arg[0], "$DEBUGON")) {
if (!qstrcmp(arg[0], "$DEBUGON")) {
if (inProg) {cerr = 254; goto cmderr;}
cerr = 0;
if (argct != 0) {cerr = 3; goto cmderr;}
if (!debug) printf("Enabled debug mode.\n");
debug = true;
}
if (!inProg && !qstrcmp(arg[0], "$DEBUGOFF")) {
if (!qstrcmp(arg[0], "$DEBUGOFF")) {
if (inProg) {cerr = 254; goto cmderr;}
cerr = 0;
if (argct != 0) {cerr = 3; goto cmderr;}
if (debug) printf("Disabled debug mode.\n");
Expand Down
4 changes: 2 additions & 2 deletions functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ if (!qstrcmp(farg[0], "INKEY$")) {
enableRawMode();
int obp = -1;
int tmp = -1;
while (tmp == -1) {
outbuf[0] = 0;
while ((outbuf[0] == 27 && tmp != 0) || outbuf[0] == 0) {
obp++;
tmp = read(1, &outbuf[obp], 1);
if (tmp == 0) {outbuf[obp] = 0; break;}
Expand All @@ -141,7 +142,6 @@ if (!qstrcmp(farg[0], "INKEY$")) {
}
obp++;
outbuf[obp] = 0;
if (!textlock) tcsetattr(0, TCSANOW, &restore);
disableRawMode();
}
if (!qstrcmp(farg[0], "CURX")) {
Expand Down
20 changes: 7 additions & 13 deletions logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ if (!qstrcmp(tmp[0], "DO")) {
if (!qstrcmp(tmp[0], "DOWHILE")) {
if (dlstackp >= 255) {cerr = 12; goto lexit;}
dlstackp++;
if (dlstackp > 0) {
if (dldcmd[dlstackp - 1]) {dldcmd[dlstackp] = true; return true;}
}
if (itstackp > -1) {
if (itdcmd[itstackp]) {dldcmd[dlstackp] = true; return true;}
}
if (dlstackp > 0) {
if (dldcmd[dlstackp - 1]) {dldcmd[dlstackp] = true; return true;}
}
copyStrSnip(cmd, j + 1, strlen(cmd), tmp[1]);
uint8_t testval = logictest(tmp[1]);
if (testval != 1 && testval != 0) return true;
Expand All @@ -35,12 +35,12 @@ if (!qstrcmp(tmp[0], "DOWHILE")) {
}
if (!qstrcmp(tmp[0], "LOOP")) {
if (dlstackp <= -1) {cerr = 6; return true;}
if (itstackp > -1) {
if (itdcmd[itstackp]) return true;
}
if (dlstackp > -1) {
if (dldcmd[dlstackp]) {dlstackp--; return true;}
}
if (itstackp > -1) {
if (itdcmd[itstackp]) return true;
}
cp = dlstack[dlstackp];
progLine = dlpline[dlstackp];
lockpl = true;
Expand Down Expand Up @@ -71,7 +71,7 @@ if (!qstrcmp(tmp[0], "LOOPWHILE")) {
if (!qstrcmp(tmp[0], "IF")) {
if (itstackp >= 255) {cerr = 13; return true;}
itstackp++;
if (itstackp > 0) {
if (itstackp > -2) {
if (itdcmd[itstackp - 1]) {itdcmd[itstackp] = true; return true;}
}
if (dlstackp > -1) {
Expand All @@ -84,22 +84,16 @@ if (!qstrcmp(tmp[0], "IF")) {
return true;
}
if (!qstrcmp(tmp[0], "ELSE")) {
if (debug) printf("ELSE (1): itdcmd[%d]: [%d]\n", (int)itdcmd[itstackp], (int)itstackp);
if (itstackp <= -1) {cerr = 8; goto lexit;}
if (itstackp > 0) {
if (debug) printf("ELSE: checking itdcmd[itstackp - 1]: [%d]\n", (int)itdcmd[itstackp - 1]);
if (itdcmd[itstackp - 1]) return true;
if (debug) printf("ELSE: passed\n");
}
if (dlstackp > -1) {
if (debug) printf("ELSE: checking dldcmd[dlstackp]: [%d]\n", (int)dldcmd[dlstackp]);
if (dldcmd[dlstackp]) return true;
if (debug) printf("ELSE: passed\n");
}
if (didelse) {cerr = 11; return true;}
didelse = true;
itdcmd[itstackp] = !itdcmd[itstackp];
if (debug) printf("ELSE (2): itdcmd[%d]: [%d]\n", (int)itdcmd[itstackp], (int)itstackp);
return true;
}
if (!qstrcmp(tmp[0], "ENDIF")) {
Expand Down

0 comments on commit 99591a1

Please sign in to comment.