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

Commit

Permalink
0.23.5
Browse files Browse the repository at this point in the history
SET raises a syntax error if given an empty value
Added EXECA, EXECA(), and EXECA$()
Added REDIM
Added BREAK and CONTINUE
Moved examples to seperate repo
  • Loading branch information
PQCraft committed Oct 9, 2021
1 parent 3b83475 commit e603c30
Show file tree
Hide file tree
Showing 34 changed files with 233 additions and 430 deletions.
5 changes: 4 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "docs"]
path = docs
url = git@github.com:PQCraft/clibasic-docs.git
url = https://github.com/pqcraft/clibasic-docs
[submodule "examples"]
path = examples
url = git@github.com:PQCraft/clibasic-examples.git
105 changes: 78 additions & 27 deletions clibasic.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@

// Base defines

char VER[] = "0.23.4";
char VER[] = "0.23.5";

#if defined(__linux__)
char OSVER[] = "Linux";
Expand Down Expand Up @@ -220,6 +220,14 @@ int* minfnstackp = NULL;
char fnvar[CB_BUF_SIZE];
char forbuf[4][CB_BUF_SIZE];

typedef struct {
uint8_t type;
uint8_t block;
} cb_brkinfo;

cb_brkinfo brkinfo;
cb_brkinfo* oldbrkinfo = NULL;

char* errstr = NULL;

char conbuf[CB_BUF_SIZE];
Expand Down Expand Up @@ -640,6 +648,27 @@ static inline void readyTerm() {
#endif
}

static inline void clearGlobals() {
dlstackp = -1;
itstackp = -1;
fnstackp = -1;
memset(&brkinfo, 0, sizeof(brkinfo));
for (int i = 0; i < CB_PROG_LOGIC_MAX; ++i) {
memset(&dlstack[i], 0, sizeof(cb_jump));
dldcmd[i] = false;
memset(&fnstack[i], 0, sizeof(cb_jump));
fnstack[i].cp = -1;
fndcmd[i] = false;
fninfor[i] = false;
itdcmd[i] = false;
}
for (int i = 0; i < gotomaxct; ++i) {
nfree(gotodata[i].name);
}
nfree(gotodata);
gotomaxct = 0;
}

int main(int argc, char** argv) {
#if defined(GUI_CHECK) && defined(__unix__)
if (!isatty(STDERR_FILENO) && !isatty(STDIN_FILENO) && !isatty(STDOUT_FILENO)) {
Expand Down Expand Up @@ -924,29 +953,14 @@ int main(int argc, char** argv) {
cerr = 0;
initBaseMem();
resetTimer();
clearGlobals();
while (!pexit) {
fchkint:;
cp = 0;
if (chkinProg) {inProg = true; chkinProg = false;}
if (!inProg && !runc) {
if (runfile) cleanExit();
dlstackp = -1;
itstackp = -1;
fnstackp = -1;
for (int i = 0; i < CB_PROG_LOGIC_MAX; ++i) {
memset(&dlstack[i], 0, sizeof(cb_jump));
dldcmd[i] = false;
memset(&fnstack[i], 0, sizeof(cb_jump));
fnstack[i].cp = -1;
fndcmd[i] = false;
fninfor[i] = false;
itdcmd[i] = false;
}
for (int i = 0; i < gotomaxct; ++i) {
nfree(gotodata[i].name);
}
nfree(gotodata);
gotomaxct = 0;
clearGlobals();
char* tmpstr = NULL;
int tmpt = getVal(prompt, pstr);
if (tmpt != 1) strcpy(pstr, "CLIBASIC> ");
Expand Down Expand Up @@ -1195,6 +1209,7 @@ void unloadProg() {
progLine = proglinebuf[progindex];
didelse = olddidelse[progindex];
didelseif = olddidelseif[progindex];
brkinfo = oldbrkinfo[progindex];
dlstackp = mindlstackp[progindex];
itstackp = minitstackp[progindex];
fnstackp = minfnstackp[progindex];
Expand All @@ -1206,6 +1221,7 @@ void unloadProg() {
minfnstackp = (int*)realloc(minfnstackp, progindex * sizeof(int));
olddidelse = (bool*)realloc(olddidelse, progindex * sizeof(bool));
olddidelseif = (bool*)realloc(olddidelseif, progindex * sizeof(bool));
oldbrkinfo = (cb_brkinfo*)realloc(oldbrkinfo, progindex * sizeof(bool));
proggotodata = (cb_goto**)realloc(proggotodata, progindex * sizeof(cb_goto*));
proggotomaxct = (int*)realloc(proggotomaxct, progindex * sizeof(int));
progindex--;
Expand Down Expand Up @@ -1282,6 +1298,7 @@ bool loadProg(char* filename) {
minitstackp = (int*)realloc(minitstackp, progindex * sizeof(int));
olddidelse = (bool*)realloc(olddidelse, progindex * sizeof(bool));
olddidelseif = (bool*)realloc(olddidelseif, progindex * sizeof(bool));
oldbrkinfo = (cb_brkinfo*)realloc(oldbrkinfo, progindex * sizeof(bool));
minfnstackp = (int*)realloc(minfnstackp, progindex * sizeof(int));
proggotodata = (cb_goto**)realloc(proggotodata, progindex * sizeof(cb_goto*));
proggotomaxct = (int*)realloc(proggotomaxct, progindex * sizeof(int));
Expand All @@ -1292,6 +1309,8 @@ bool loadProg(char* filename) {
mindlstackp[progindex] = dlstackp;
minitstackp[progindex] = itstackp;
olddidelse[progindex] = didelse;
olddidelseif[progindex] = didelseif;
oldbrkinfo[progindex] = brkinfo;
minfnstackp[progindex] = fnstackp;
proggotodata[progindex] = gotodata;
proggotomaxct[progindex] = gotomaxct;
Expand All @@ -1302,6 +1321,7 @@ bool loadProg(char* filename) {
progLine = 1;
didelse = false;
didelseif = false;
memset(&brkinfo, 0, sizeof(brkinfo));
#ifdef _WIN_NO_VT
getCurPos();
int tmpx = curx, tmpy = cury;
Expand Down Expand Up @@ -1712,13 +1732,15 @@ uint16_t getFuncIndex = 0;
char* getFunc_gftmp[2] = {NULL, NULL};

uint8_t getFunc(char* inbuf, char* outbuf) {
char** tmpfargs;
char** farg;
uint8_t* fargt;
int* flen;
int fargct;
int ftmpct = 0;
int ftype = 0;
char* gftmp[2];
bool skipfargsolve = false;
if (getFuncIndex) {
gftmp[0] = malloc(CB_BUF_SIZE);
gftmp[1] = malloc(CB_BUF_SIZE);
Expand All @@ -1735,6 +1757,7 @@ uint8_t getFunc(char* inbuf, char* outbuf) {
int32_t j = strlen(inbuf) - 1;
copyStrSnip(inbuf, i + 1, j, gftmp[0]);
fargct = getArgCt(gftmp[0]);
tmpfargs = malloc((fargct + 1) * sizeof(char*));
farg = malloc((fargct + 1) * sizeof(char*));
flen = malloc((fargct + 1) * sizeof(int));
fargt = malloc((fargct + 1) * sizeof(uint8_t));
Expand All @@ -1746,6 +1769,7 @@ uint8_t getFunc(char* inbuf, char* outbuf) {
flen[0] = i;
farg[0] = (char*)malloc(flen[0] + 1);
copyStrTo(inbuf, i, farg[0]);
tmpfargs[0] = farg[0];
if (!strcmp(farg[0], "~") || !strcmp(farg[0], "_TEST")) {
ftype = 2;
if (fargct != 1) {cerr = 3; goto fexit;}
Expand All @@ -1756,30 +1780,54 @@ uint8_t getFunc(char* inbuf, char* outbuf) {
outbuf[0] = '0' + ret;
outbuf[1] = 0;
goto fexit;
} else if (!strcmp(farg[0], "EXECA") || !strcmp(farg[0], "EXECA$")) {
skipfargsolve = true;
}
} else {
if (getArg(j - 1, gftmp[0], gftmp[1]) == -1) {outbuf[0] = 0; goto fexit;}
fargt[j] = getVal(gftmp[1], gftmp[1]);
if (fargt[j] == 0) goto fnoerrscan;
if (fargt[j] == 255) fargt[j] = 0;
flen[j] = strlen(gftmp[1]);
farg[j] = (char*)malloc(flen[j] + 1);
copyStr(gftmp[1], farg[j]);
tmpfargs[j] = malloc(CB_BUF_SIZE);
int32_t tmpsize;
if ((tmpsize = getArg(j - 1, gftmp[0], tmpfargs[j])) == -1) {outbuf[0] = 0; goto fexit;}
tmpfargs[j] = realloc(tmpfargs[j], tmpsize + 1);
if (skipfargsolve) {
flen[j] = tmpsize;
} else {
fargt[j] = getVal(tmpfargs[j], gftmp[1]);
if (fargt[j] == 0) goto fnoerrscan;
if (fargt[j] == 255) fargt[j] = 0;
flen[j] = strlen(gftmp[1]);
farg[j] = (char*)malloc(flen[j] + 1);
copyStr(gftmp[1], farg[j]);
}
ftmpct++;
}
}
}
/*
for (int j = 0; j <= ftmpct; ++j) {
printf("farg[%d]: {%s}\n", j, farg[j]);
printf("tmpfargs[%d]: {%s}\n", j, tmpfargs[j]);
}
*/
outbuf[0] = 0;
cerr = 127;
chkCmdPtr = farg[0];
#include "functions.c"
fexit:;
if (cerr > 124 && cerr < 128) seterrstr(farg[0]);
fnoerrscan:;
for (int j = 0; j <= ftmpct; ++j) {
free(farg[j]);
free(farg[0]);
if (skipfargsolve) {
for (int j = 1; j <= ftmpct; ++j) {
free(tmpfargs[j]);
}
} else {
for (int j = 1; j <= ftmpct; ++j) {
free(farg[j]);
free(tmpfargs[j]);
}
}
free(farg);
free(tmpfargs);
free(flen);
free(fargt);
--getFuncIndex;
Expand Down Expand Up @@ -2914,6 +2962,9 @@ static inline void printError(int error) {
case 29:
fputs("Label is not defined", stdout);
break;
case 30:
fputs("Not in DO/FOR block", stdout);
break;
case 125:
printf("Function only valid in program: '%s'", errstr);
break;
Expand Down
79 changes: 72 additions & 7 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ if (chkCmd(2, "SET", "LET")) {
if (argct != 2) {cerr = 3; goto cmderr;}
cerr = 0;
if (!solvearg(2)) goto cmderr;
if (!tmpargs[1][0] || !argt[2]) {cerr = 1; goto cmderr;}
if (!setVar(tmpargs[1], arg[2], argt[2], -1)) goto cmderr;
goto noerr;
}
Expand All @@ -48,13 +49,44 @@ if (chkCmd(1, "DIM")) {
asize)) goto cmderr;
goto noerr;
}
if (chkCmd(1, "REDIM")) {
if (argct < 2) {cerr = 3; goto cmderr;}
cerr = 0;
if (!solvearg(2)) goto cmderr;
if (argt[2] != 2) {cerr = 2; goto cmderr;}
int v = -1;
for (register int i = 0; i < varmaxct; ++i) {
if (vardata[i].inuse && !strcmp(tmpargs[1], vardata[i].name)) {v = i; break;}
}
if (v == -1 || vardata[v].size == -1) {cerr = 23; seterrstr(tmpargs[1]); goto cmderr;}
int32_t s = atoi(arg[2]);
if (s == vardata[v].size) {goto noerr;}
int32_t os = vardata[v].size;
if (s < 0) {cerr = 16; goto cmderr;}
vardata[v].size = s;
char** newdata = (char**)malloc((s + 1) * sizeof(char*));
int32_t i = 0;
for (; i <= s && i <= os; ++i) {
newdata[i] = vardata[v].data[i];
}
for (; i <= s; ++i) {
newdata[i] = malloc(1);
newdata[i][0] = 0;
}
for (i = s + 1; i <= os; ++i) {
free(vardata[v].data[i]);
}
free(vardata[v].data);
vardata[v].data = newdata;
goto noerr;
}
if (chkCmd(1, "FILL")) {
if (argct < 1 || argct > 2) {cerr = 3; goto cmderr;}
if (getType(tmpargs[1]) != 255) {cerr = 4; seterrstr(tmpargs[1]); goto cmderr;}
upCase(tmpargs[1]);
int v = -1;
for (register int i = 0; i < varmaxct; ++i) {
if (!strcmp(tmpargs[1], vardata[i].name)) {v = i; break;}
if (vardata[i].inuse && !strcmp(tmpargs[1], vardata[i].name)) {v = i; break;}
}
if (v == -1 || vardata[v].size == -1) {cerr = 23; seterrstr(tmpargs[1]); goto cmderr;}
for (int i = 0; i <= vardata[v].size; ++i) {
Expand Down Expand Up @@ -149,6 +181,19 @@ if (chkCmd(3, "%", "GOTO", "GO")) {
lockpl = true;
goto noerr;
}
if (chkCmd(2, "CONTINUE", "BREAK")) {
if (argct) {cerr = 3; goto cmderr;}
if (brkinfo.block == 1) {
dldcmd[dlstackp] = !dldcmd[dlstackp];
} else if (brkinfo.block == 2) {
fndcmd[fnstackp] = !fndcmd[fnstackp];
} else {
cerr = 30;
goto cmderr;
}
brkinfo.type = 1 + !strcmp(arg[0], "BREAK");
goto noerr;
}
if (chkCmd(1, "COLOR")) {
if (argct > 2 || argct < 1) {cerr = 3; goto cmderr;}
cerr = 0;
Expand Down Expand Up @@ -436,11 +481,28 @@ if (chkCmd(2, "$", "SH")) {
cerr = 0;
goto noerr;
}
if (chkCmd(1, "EXEC")) {
if (chkCmd(2, "EXEC", "EXECA")) {
if (argct < 1) {cerr = 3; goto cmderr;}
cerr = 0;
if (!solvearg(1)) goto cmderr;
if (argt[1] != 1) {cerr = 2; goto cmderr;}
bool execa = false;
char** tmparg = NULL;
int tmpargct = 0;
if (!strcmp(arg[0], "EXECA")) {
execa = true;
int v = -1;
for (register int i = 0; i < varmaxct; ++i) {
if (vardata[i].inuse && !strcmp(tmpargs[1], vardata[i].name)) {v = i; break;}
}
if (v == -1 || vardata[v].size == -1) {cerr = 23; seterrstr(tmpargs[1]); goto cmderr;}
if (vardata[v].type != 1) {cerr = 2; goto cmderr;}
tmparg = arg;
tmpargct = argct;
arg = vardata[v].data - 1;
argct = vardata[v].size + 1;
} else {
if (!solvearg(1)) goto cmderr;
if (argt[1] != 1) {cerr = 2; goto cmderr;}
}
#ifndef _WIN_NO_VT
if (sh_clearAttrib) fputs("\e[0m", stdout);
#else
Expand All @@ -452,7 +514,7 @@ if (chkCmd(1, "EXEC")) {
runargs[0] = arg[1];
int argno = 1;
for (; argno < argct; ++argno) {
if (!solvearg(argno + 1)) {free(runargs); goto cmderr;}
if (!execa) if (!solvearg(argno + 1)) {free(runargs); goto cmderr;}
runargs[argno] = arg[argno + 1];
}
runargs[argno] = NULL;
Expand Down Expand Up @@ -485,7 +547,7 @@ if (chkCmd(1, "EXEC")) {
tmpcmd[0] = 0;
bool winecho = false;
for (int argno = 1; argno <= argct; ++argno) {
if (argno > 1) if (!solvearg(argno)) {free(tmpcmd); goto cmderr;};
if (!execa) if (argno > 1) if (!solvearg(argno)) {free(tmpcmd); goto cmderr;};
strApndChar(tmpcmd, ' ');
bool nq = winArgNeedsQuotes(arg[argno]);
if (argno == 1) {
Expand All @@ -511,6 +573,10 @@ if (chkCmd(1, "EXEC")) {
}
free(tmpcmd);
#endif
if (execa) {
argct = tmpargct;
arg = tmparg;
}
if (sh_restoreAttrib) updateTxtAttrib();
if (cerr) goto cmderr;
goto noerr;
Expand Down Expand Up @@ -730,7 +796,6 @@ if (chkCmd(1, "_RESETTITLE")) {
goto noerr;
}
if (chkCmd(1, "_TITLE")) {
if (inProg) {cerr = 254; goto cmderr;}
if (argct != 1) {cerr = 3; goto cmderr;}
cerr = 0;
if (!solvearg(1)) goto cmderr;
Expand Down
7 changes: 7 additions & 0 deletions commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ git commit -m "Updated docs to $ver"
git push
cd ..

echo $'\e[1m'"Updating examples submodule..."$'\e[0m'
cd examples/
git add *
git commit -m "Updated examples to $ver"
git push
cd ..

echo $'\e[1m'"Adding files:" !(clibasic|clibasic.exe|*.dll|*.zip|*.bas) $'\e[0m'
git add !(clibasic|clibasic.exe|*.dll|*.zip|*.bas) || exit 1

Expand Down
2 changes: 1 addition & 1 deletion docs
Submodule docs updated 1 files
+ manual.odt
Loading

0 comments on commit e603c30

Please sign in to comment.