Skip to content

Commit

Permalink
Strings are here! Typing is a thing?
Browse files Browse the repository at this point in the history
Also there are now things for the github action to compare to
Also a small start for `draw`
  • Loading branch information
Stvff committed Mar 6, 2022
1 parent be47109 commit 062ee5f
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 30 deletions.
9 changes: 1 addition & 8 deletions .github/arbasm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ rules:
# This file is made for Arbitrary Assembly, it is adapted from the NASM assembly file from micro itself

## Instructions
- statement: "\\b(?i)(set|dset|dget|rev|sel|inc|dec|add|sub|mul|div|mod|cmp|ucmp|rot|shf|print|len|dset|dget)(?-i)\\b"
- statement: "\\b(?i)(set|dset|dget|rev|sel|inc|dec|add|sub|mul|div|mod|cmp|ucmp|rot|shf|print|sprint|draw|len|dset|dget)(?-i)\\b"
- statement: "\\b(?i)(push|pop|peek|flip|ret)(?-i)\\b"

## Registers
Expand All @@ -30,13 +30,6 @@ rules:
rules:
- constant.specialChar: "\\\\."

- constant.string:
start: "'"
end: "'"
skip: "\\\\."
rules:
- constant.specialChar: "\\\\."

- comment:
start: ";"
end: "$"
Expand Down
59 changes: 44 additions & 15 deletions aasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ enum registers {gr1, gr2, gr3, gr4, ir, flag, inplen, endian, stacsz, mstptr, rs
enum instructs {endprog=1, slashn, wincrap, space, tab, semicolon, h, set, dset, dget, rev, sel,
inc, dec, add, sub, mul, divi, modu,
cmp, ucmp, rot, shf, len,
print, push, pop, peek, flip, ret,
print, sprint, fprint, draw,
push, pop, peek, flip, ret,
SCR, SAVE, LOAD, Ce, Cg, Cs};

const int TheMaximumLengthOfTheThings = 10;
Expand All @@ -36,7 +37,8 @@ char registerstring[][10] = { "gr1", "gr2", "gr3", "gr4", "ir",
char instructstring[][10] = { "\\", "\n", "\r", " ", " ", ";", "h", "set", "dset", "dget", "rev", "sel",
"inc", "dec", "add", "sub", "mul", "div", "mod",
"cmp", "ucmp", "rot", "shf", "len",
"print", "push", "pop", "peek", "flip", "ret",
"print", "sprint", "fprint", "draw",
"push", "pop", "peek", "flip", "ret",
"SCR", "SAVE", "LOAD", "Ce", "Cg", "Cs", "\0end" };

int qregamount = 4;
Expand All @@ -47,6 +49,8 @@ enum quaternioninstructs {qset=1, qnget, qnset, qadd, qsub, qmul, qsmul};
char quatregistring[][10] = { "qr1", "qr2", "qr3", "qr4", "\0end" };
char quatinstructstring[][10] = { "qset", "qnget", "qadd", "qsub", "qmul", "qsmul", "\0end" };

enum typeenum {number, quater, string, image};

typedef struct FileInfo {
FILE *fp;
int rdpos;
Expand Down Expand Up @@ -75,19 +79,21 @@ int strlook(char string[], char source[][TheMaximumLengthOfTheThings], int offse
return 0;
}

void functionswitch(int instruction, num_t* args[], bool wasquat, qua_t* qargs[]){
void functionswitch(int instruction, num_t* args[], int types[], qua_t* qargs[]){
clock_t begin_time = clock();
bool doprint = !ScriptingMode;
int rettype = types[0];
num_t* printptr = args[0];
qua_t* qprintptr = qargs[0];
num_t dummy;
initnum(&dummy, 15, 0, 0);
switch (instruction){
case set:
if(wasquat)
if(types[0]==quater)
copyquat(qargs[0], qargs[1], 0);
else
copynum(args[0], args[1], 0);
if(types[1]==string) rettype = string;
break;
case dset:
args[0]->nump[numtoint(args[1], false) % args[0]->len] = args[2]->nump[0];
Expand All @@ -112,19 +118,19 @@ void functionswitch(int instruction, num_t* args[], bool wasquat, qua_t* qargs[]
incnum(args[0], true);
break;
case add:
if(wasquat)
if(types[0]==quater)
sumquat(qargs[0], qargs[0], qargs[1], false);
else
sumnum(args[0], args[0], args[1], false);
break;
case sub:
if(wasquat)
if(types[0]==quater)
sumquat(qargs[0], qargs[0], qargs[1], true);
else
sumnum(args[0], args[0], args[1], true);
break;
case mul:
if(wasquat)
if(types[0]==quater)
multquat(qargs[0], qargs[0], qargs[1]);
else
multnum(args[0], args[0], args[1]);
Expand Down Expand Up @@ -152,6 +158,11 @@ void functionswitch(int instruction, num_t* args[], bool wasquat, qua_t* qargs[]
case print:
doprint = true;
break;
case sprint:
doprint = false;
rettype = string;
printstrnum(args[0]);
break;
case push:
stackptr--;
if(stackptr < 0){
Expand Down Expand Up @@ -229,9 +240,19 @@ void functionswitch(int instruction, num_t* args[], bool wasquat, qua_t* qargs[]
break;
}

if(doprint && !wasquat) printnum(printptr, bigEndian);
if(doprint && wasquat) printquat(qprintptr, bigEndian);

if(doprint) switch (rettype){
case number:
printnum(printptr, bigEndian);
break;
case quater:
printquat(qprintptr, bigEndian);
break;
case string:
printstrnum(printptr);
printf("\n");
break;
};

inttonum(&regs[tme], (int)((double)(clock() - begin_time)/CLOCKS_PER_SEC));
free(dummy.nump);
}
Expand Down Expand Up @@ -382,17 +403,20 @@ bool dothing(file_t file){
qua_t* tmpqptr[3];
qua_t tmpq[3];

int types[3];

for(int i = 0; i < argam; i++){
initnum(&tmp[i], 1, 0, 0);
tmp[i].nump[0] = 0;
tmpptr[i] = &tmp[i];
initquat(&tmpq[i]);
tmpqptr[i] = &tmpq[i];

types[i] = number;
}

int argn = 0;
int diminq = 0;
bool wasquat = false;
int loInputentry = 0;
char entry;
bool therewasnosemi = true;
Expand All @@ -407,7 +431,7 @@ bool dothing(file_t file){

if(argn < argam){
if(entry == ','){ argn++; diminq = 0;}
if(entry == '+'){ diminq++; wasquat = true;}
if(entry == '+'){ diminq++; types[argn] = quater;}
if( (entry >= 'a' && entry <= 'z') || (entry >= 'A' && entry <= 'Z') ){

int id;
Expand All @@ -426,7 +450,7 @@ bool dothing(file_t file){
printf("Register at argument %d on line %d is not a (quaternion) register.\n", argn + 1, file.linenr);
goto donothingsafe;
} else {
wasquat = true;
types[argn] = quater;
tmpqptr[argn] = &qregs[id - 1];
tmpptr[argn] = &qregs[id - 1].q[diminq%4];
// copynum(tmpptr[argn], &tmpqptr[argn]->q[diminq%4], false);
Expand All @@ -439,6 +463,11 @@ bool dothing(file_t file){
tmpptr[argn] = &tmpqptr[argn]->q[diminq%4];
// copynum(&tmpqptr[argn]->q[diminq%4], tmpptr[argn], false);

} else if (entry == '"'){

loInputentry = strtostrnum(tmpptr[argn], userInput, i+1);
types[argn] = string;

}
}
}
Expand All @@ -448,7 +477,7 @@ bool dothing(file_t file){
}
// for(int i = 0; i < argam; i++) printquat(tmpqptr[i], bigEndian);

functionswitch(instruction, tmpptr, wasquat, tmpqptr);
functionswitch(instruction, tmpptr, types, tmpqptr);

donothingsafe:
for(int i = 0; i < argam; i++){
Expand Down Expand Up @@ -482,7 +511,7 @@ void flushuserInput(){
}

int main(int argc, char* argv[]){
initnumarray(regamount, regs, 21, 0, 0);
initnumarray(regamount, regs, 7, 0, 0);
initquatarray(qregamount, qregs);
stackptr = stackSize;
stack = (num_t*) malloc(stackSize * sizeof(num_t));
Expand Down
49 changes: 46 additions & 3 deletions arbnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ uint8_t* initnum(num_t* number, unsigned int length, int sign, int dim){
number->sign = sign;
number->dim = dim;
number->len = length;
number->nump = (uint8_t*) malloc(number->len*sizeof(uint32_t));
number->nump = (uint8_t*) malloc(number->len*sizeof(uint8_t));
if(number->nump == NULL){
printf("Malloc error on initnum.\n");
}
Expand Down Expand Up @@ -50,7 +50,7 @@ void copynum(num_t* des, num_t* src, int keeplen){
}
}

void printnum(num_t* number, int isbigend){
void printnum(num_t* number, int isbigend){//add 2 to isbigend to make it not newline
bool donewline = true;
if(isbigend > 1){ donewline = false; isbigend -= 2;}

Expand Down Expand Up @@ -93,7 +93,7 @@ int inpstrtonum(num_t* number, char str[], int offset, int isbigend){
}
free(number->nump);
number->len = j;
number->nump = (uint8_t*) malloc(j*sizeof(uint32_t));
number->nump = (uint8_t*) malloc(j*sizeof(uint8_t));
if(number->nump == NULL) printf("There is a malloc problem on inpstrtonum.\n");

int signage = 0;
Expand Down Expand Up @@ -129,6 +129,49 @@ int inpstrtonum(num_t* number, char str[], int offset, int isbigend){
return i + signage - offset;
}

int strtostrnum(num_t* number, char str[], int offset){
int i = offset;
int j = 0;
int escapes = 0;
while(str[i] != '"' && str[i] != '\0'){
if(str[i] == '\\') escapes++;
else j++;
i++;
}
free(number->nump);
number->len = j;
number->nump = (uint8_t*) malloc(j*sizeof(uint8_t));
if(number->nump == NULL) printf("There is a malloc problem on strtostrnum.\n");

number->sign = 0;
number->dim = 0;

j = 0;
for(int n = offset; n < i; n++){
if(str[n] == '\\'){
switch (str[n+1]){
case '0':
number->nump[j] = '\0';
break;
case 'n':
number->nump[j] = '\n';
break;
case 'r':
number->nump[j] = '\r';
break;
}
n++;
} else number->nump[j] = str[n];
j++;
}
return i + 1;
}

void printstrnum(num_t* number){
for(unsigned int i = 0; i < number->len; i++)
printf("%c", number->nump[i]);
}

void savenum(FILE* fp, num_t* num){//File needs to be opened already!
fwrite(num, 4, 3, fp);
fwrite(num->nump, 1, num->len, fp);
Expand Down
6 changes: 6 additions & 0 deletions arbnum_quats.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,10 @@ void multquat(qua_t* res, qua_t* arg1, qua_t* arg2){
freequat(&dummy1);
freequat(&dummy2);
}

/*void divquat(qua_t* res, qua_t* mod, qua_t* num, num_t* den){
}*/

#endif
9 changes: 5 additions & 4 deletions scripts/bigtest/bigtest.aa
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
; These are tests
print 11111111111111111111111111111111
; These are tests, its rigorousness is dubious, but
; I'm just throwing stuff at it to see how it'll handle
print "\n##################REGISTERS\n"
SCR registers.aa
print 22222222222222222222222222222222
print "\n##################INPUTS\n"
SCR inputs.aa
print 33333333333333333333333333333333
print "\n##################END OF TEST\n"
62 changes: 62 additions & 0 deletions scripts/bigtest/expectedoutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

##################REGISTERS

gr1: 0 0 0 0 0 0 0
gr2: 0 0 0 0 0 0 0
gr3: 0 0 0 0 0 0 0
gr4: 0 0 0 0 0 0 0
ir: 0 0 0 0 0 0 0
flag: 0 0 0 0 0 0 0
inplen: 6 5 2
endian: 0
stacsz: 0 2
mstptr: 0 2
rstptr: 0 2
time: 0
loop: 0
qr1: 0 + 0 i + 0 j + 0 k
qr2: 0 + 0 i + 0 j + 0 k
qr3: 0 + 0 i + 0 j + 0 k
qr4: 0 + 0 i + 0 j + 0 k

##################INPUTS

endian is 0
123456789:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
978 654 321:
9 7 8 6 5 4 3 2 1
9 7 8 6 5 4 3 2 1
123 -:
1 2 3 -
1 2 3 -
123-:
1 2 3 -
3 2 1 -

endian is 1
123456789:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
978 654 321:
9 7 8 6 5 4 3 2 1
9 7 8 6 5 4 3 2 1
123 -:
1 2 3 -
1 2 3 -
123-:
1 2 3 -
3 2 1 -

endian is 0
1 + 2i + 3j + 4k:
1 + 2 i + 3 j + 4 k
4+3i+2j+1k:
4 + 3 i + 2 j + 1 k
3 - +2+ 4k + 1-:
3 - + 2 + 4 k + 1 -

##################END OF TEST

Script completed.
Loading

0 comments on commit 062ee5f

Please sign in to comment.