|
| 1 | +/* |
| 2 | +#include "stdio.h" |
| 3 | +#include "util.c" |
| 4 | +
|
| 5 | +#define width 80 |
| 6 | +#define height 18 |
| 7 | +*/ |
| 8 | + |
| 9 | +int iregisters[10]; |
| 10 | +char cregisters[10]; |
| 11 | +int cmp1, cmp2; |
| 12 | + |
| 13 | +void exec_seti(char*); |
| 14 | +void exec_setc(char*); |
| 15 | +void exec_printi(char*); |
| 16 | +void exec_printc(char*); |
| 17 | +void exec_addi(char*); |
| 18 | +void exec_multi(char*); |
| 19 | +void exec_null(char*); |
| 20 | +void exec_randi(char*); |
| 21 | +void exec_inc(char*); |
| 22 | +void exec_dec(char*); |
| 23 | +void exec_setcmp1(char*); |
| 24 | +void exec_setcmp2(char*); |
| 25 | +void exec_status(char*); |
| 26 | + |
| 27 | +char *execCommandList[] = { "seti", "setc", "printi", "printc", "addi", "multi", "randi", "inc", "dec", "setcmp1", "setcmp2", "status" , ""}; |
| 28 | +void (*execFunctionList[])(char*) = {exec_seti, exec_setc, exec_printi, exec_printc, exec_addi, exec_multi, exec_randi, exec_inc, exec_dec, exec_setcmp1, exec_setcmp2, exec_status, exec_null}; |
| 29 | + |
| 30 | +void sh_exec(char* params){ |
| 31 | + terminalMode = INTERPRETER; |
| 32 | + char line[width+1]; |
| 33 | + line[width] = 0; |
| 34 | + int index = 0; |
| 35 | + for(index = 0; index < 10; index++){ |
| 36 | + iregisters[index] = 0; |
| 37 | + cregisters[index] = 0; |
| 38 | + } |
| 39 | + |
| 40 | + //iterate through the buffer |
| 41 | + int lineNum; |
| 42 | + for(lineNum = 0; lineNum < height; lineNum++){ |
| 43 | + // read line, removing null chars along the way: |
| 44 | + int i = 0; |
| 45 | + index = lineNum * width; |
| 46 | + // lines must not have the first char blank |
| 47 | + // also don't read comments |
| 48 | + if(fileBuffer[index] == ' ' || fileBuffer[index] == 0 || fileBuffer[index] == '#') |
| 49 | + continue; |
| 50 | + memFill(line,0,width); |
| 51 | + while(fileBuffer[index] != ';' && i < 80){ |
| 52 | + if(fileBuffer[index] != 0){ |
| 53 | + line[i] = fileBuffer[index]; |
| 54 | + i++; |
| 55 | + } |
| 56 | + index++; |
| 57 | + } |
| 58 | + if(strEquals(line,"")) continue; |
| 59 | + // now `line` contains the command (function + args) to be interpreted |
| 60 | + // isolate out the function part by splitting on the first space |
| 61 | + int j = 0; |
| 62 | + while(line[j] != ' ' && j < width) j++; |
| 63 | + char function[j+1]; |
| 64 | + char params[width-j]; |
| 65 | + memCopy(line, function, j); |
| 66 | + function[j] = 0; |
| 67 | + memCopy((line + j+1), params, (width-j)); |
| 68 | + // macros: |
| 69 | + if(strEquals(function, "jeq")){ |
| 70 | + if(cmp1 == cmp2) lineNum = strToInt(params) - 1; |
| 71 | + continue; |
| 72 | + } |
| 73 | + if(strEquals(function, "jne")){ |
| 74 | + if(cmp1 != cmp2) lineNum = strToInt(params) - 1; |
| 75 | + continue; |
| 76 | + } |
| 77 | + if(strEquals(function, "jge")){ |
| 78 | + if(cmp1 >= cmp2) lineNum = strToInt(params) - 1; |
| 79 | + continue; |
| 80 | + } |
| 81 | + if(strEquals(function, "jle")){ |
| 82 | + if(cmp1 <= cmp2) lineNum = strToInt(params) - 1; |
| 83 | + continue; |
| 84 | + } |
| 85 | + if(strEquals(function, "jlt")){ |
| 86 | + if(cmp1 < cmp2) lineNum = strToInt(params) - 1; |
| 87 | + continue; |
| 88 | + } |
| 89 | + if(strEquals(function, "jgt")){ |
| 90 | + if(cmp1 > cmp2) lineNum = strToInt(params) - 1; |
| 91 | + continue; |
| 92 | + } |
| 93 | + j = 0; |
| 94 | + while(!strEquals(execCommandList[j],function) && !strEquals(execCommandList[j],"")) j++; |
| 95 | + if(!strEquals(execCommandList[j],"")) (*execFunctionList[j])(params); |
| 96 | + else { |
| 97 | + ttprint("err: no such function: `"); |
| 98 | + ttprint(function); |
| 99 | + ttprint("` of length:"); |
| 100 | + ttprintIntln(strLen(function)); |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + terminalMode = TERMINAL; |
| 105 | + return; |
| 106 | +} |
| 107 | + |
| 108 | +// `seti registerIndex value` |
| 109 | +void exec_seti(char* params){ |
| 110 | + int index = params[0] - '0'; |
| 111 | + iregisters[index] = strToInt( (params + 2) ); |
| 112 | +} |
| 113 | + |
| 114 | +// `setc registerIndex value` |
| 115 | +void exec_setc(char* params){ |
| 116 | + int index = params[0] - '0'; |
| 117 | + cregisters[index] = params[2]; |
| 118 | +} |
| 119 | + |
| 120 | +// `printi registerIndex` |
| 121 | +void exec_printi(char* params){ |
| 122 | + //printf("%d\n", iregisters[params[0] - '0']); |
| 123 | + ttprintIntln(iregisters[params[0] - '0']); |
| 124 | +} |
| 125 | + |
| 126 | +// `printc registerIndex` |
| 127 | +void exec_printc(char* params){ |
| 128 | + //printf("%c\n", cregisters[params[0] - '0']); |
| 129 | + ttprintChar(cregisters[params[0] - '0']); |
| 130 | + ttprintChar('\n'); |
| 131 | +} |
| 132 | + |
| 133 | +// `addi operand operand destination |
| 134 | +void exec_addi(char* params){ |
| 135 | + char a = params[0] - '0'; |
| 136 | + char b = params[2] - '0'; |
| 137 | + char c = params[4] - '0'; |
| 138 | + iregisters[c] = iregisters[a] + iregisters[b]; |
| 139 | +} |
| 140 | + |
| 141 | +// `multi operand operand destination |
| 142 | +void exec_multi(char* params){ |
| 143 | + char a = params[0] - '0'; |
| 144 | + char b = params[2] - '0'; |
| 145 | + char c = params[4] - '0'; |
| 146 | + iregisters[c] = iregisters[a] * iregisters[b]; |
| 147 | +} |
| 148 | + |
| 149 | +// `randi limit destination |
| 150 | +void exec_randi(char* params){ |
| 151 | + int i; |
| 152 | + int limit = 10; |
| 153 | + char index = 0; |
| 154 | + for(i = 0; i < strLen(params); i++){ |
| 155 | + if(params[i] == ' '){ |
| 156 | + params[i] = 0; |
| 157 | + limit = strToInt(params); |
| 158 | + params[i] = ' '; |
| 159 | + index = params[i+1] - '0'; |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + iregisters[index] = rand(limit); |
| 164 | +} |
| 165 | + |
| 166 | +void exec_inc(char* params){ |
| 167 | + iregisters[params[0] - '0'] += 1; |
| 168 | +} |
| 169 | + |
| 170 | +void exec_dec(char* params){ |
| 171 | + iregisters[params[0] - '0'] -= 1; |
| 172 | +} |
| 173 | + |
| 174 | +void exec_setcmp1(char* params){ |
| 175 | + cmp1 = iregisters[params[0] - '0']; |
| 176 | +} |
| 177 | +void exec_setcmp2(char* params){ |
| 178 | + cmp2 = iregisters[params[0] - '0']; |
| 179 | +} |
| 180 | + |
| 181 | +void exec_status(char* params){ |
| 182 | + int i; |
| 183 | + ttprint("ireg:"); |
| 184 | + for(i = 0; i < 10; i++){ |
| 185 | + ttprintInt(iregisters[i]); |
| 186 | + ttprintChar(','); |
| 187 | + } |
| 188 | + ttprintln(""); |
| 189 | + ttprint("creg:"); |
| 190 | + for(i = 0; i < 10; i++){ |
| 191 | + ttprintChar(cregisters[i]); |
| 192 | + ttprintChar(','); |
| 193 | + } |
| 194 | + ttprintln(""); |
| 195 | + ttprint("cmp:"); |
| 196 | + ttprintInt(cmp1); |
| 197 | + ttprintChar(','); |
| 198 | + ttprintInt(cmp2); |
| 199 | + ttprintln(""); |
| 200 | +} |
| 201 | + |
| 202 | + |
| 203 | +void exec_null(char * params){ |
| 204 | + |
| 205 | +} |
| 206 | + |
0 commit comments