interpreteur à tester pour pointeurs et fonctions

This commit is contained in:
Leonie Gallois 2021-04-29 13:12:45 +02:00
parent 2733800f4d
commit 78c21f45ee
3 changed files with 51 additions and 8 deletions

11
Interpreteur/input.txt Normal file
View file

@ -0,0 +1,11 @@
AFC 0 1
AFC 10 20
ADD 0 0 10
AFC 1 8888
SUP 2 0 1
JMF 2 7
PRI 1
PRI 0
CALL
COPR
RET

14
Interpreteur/makefile Executable file
View file

@ -0,0 +1,14 @@
SRCC:= ./src/*.c
all: interpreter
interpreter: ./src/interpreter.y ./src/interpreter.l ./src/instructions.c
yacc -d ./src/interpreter.y
lex ./src/interpreter.l
gcc lex.yy.c y.tab.c ./src/instructions.c -Isrc -o interpreter
run: interpreter
./interpreter < input.txt
clean:
rm -f lex.yy.c interpreter y.tab.h y.tab.c *.o

View file

@ -39,6 +39,14 @@ void push(int arg){
}
}
void print_stack(int tab[], int taille){
for (int i=0; i<taille; i++){
printf("tab[%d]=%d\n", i,tab[i]);
}
}
int exec(int ip);
int valid_memory_addr(int address);
@ -127,25 +135,32 @@ int exec(int ip) {
switch (ins) {
case ADD:
printf("ADD @%d = @%d[%d] + @%d[%d]\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
memory[arg1] = memory[arg2] + memory[arg3]; break;
memory[arg1] = memory[arg2] + memory[arg3];
break;
case MUL:
printf("MUL @%d = @%d[%d] * @%d[%d]\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
memory[arg1] = memory[arg2] * memory[arg3]; break;
memory[arg1] = memory[arg2] * memory[arg3];
break;
case SOU:
printf("SOU @%d = @%d[%d] - @%d[%d]\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
memory[arg1] = memory[arg2] - memory[arg3]; break;
memory[arg1] = memory[arg2] - memory[arg3];
break;
case DIV:
printf("DIV @%d = @%d[%d] / @%d[%d]\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
memory[arg1] = memory[arg2] / memory[arg3]; break;
memory[arg1] = memory[arg2] / memory[arg3];
break;
case COP:
printf("COP @%d = @%d[%d]\n", arg1, arg2, memory[arg2]);
memory[arg1] = memory[arg2]; break;
memory[arg1] = memory[arg2];
break;
case AFC:
printf("AFC @%d = %d\n", arg1, arg2);
memory[arg1] = arg2; break;
memory[arg1] = arg2;
break;
case JMP:
printf("JMP to %d\n", arg1);
next_ip = arg1; break;
next_ip = arg1;
break;
case JMF:
printf("JMF cond@%d[%d] to %d\n", arg1, memory[arg1], arg2);
if (memory[arg1] == 0) next_ip = arg2;
@ -196,6 +211,9 @@ int exec(int ip) {
default:
fprintf(stderr, "ERROR run : unknown inst.\n");
}
printf("memory_fonc\n");
print_stack(memory_fonc, INDEX_memory_fonc);
printf("memory\n");
print_stack(memory, 11);
return next_ip;
}