summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bfi.c25
-rw-r--r--src/mem.c35
-rw-r--r--src/stack.c99
-rw-r--r--src/translator.c0
4 files changed, 125 insertions, 34 deletions
diff --git a/src/bfi.c b/src/bfi.c
index cc13b11..03b32c1 100644
--- a/src/bfi.c
+++ b/src/bfi.c
@@ -11,6 +11,9 @@
#endif
void intepret(char *program){
+
+ struct stack_st stack= { .len = 0, .size = 0, .arr = NULL};
+
int i = 0;
int length = 0;
while (program[i++] != '\0')
@@ -18,8 +21,6 @@ void intepret(char *program){
i = 0;
while (i < length){
- //printf("i = %d\n",i);
- //printf("%c\n", program[i]);
switch(program[i]) {
@@ -69,9 +70,14 @@ void intepret(char *program){
case BF_START_LOOP: {
- stackPush(i);
+ stackPush(&stack, i);
int memVal = mem_get();
+ //printf("StartLoop! i = %d\n",i);
+ //printf("memVal: %d\n", mem_get());
+ //printf("stackVal: %d\n", stackPeek(&stack));
+ //printf("%c\n\n", program[i]);
+
//Skiping loop
if (memVal == 0){
int helpCounter = 1;
@@ -82,19 +88,24 @@ void intepret(char *program){
if (program[i] == ']')
helpCounter--;
}
- stackPop();
+ stackPop(&stack);
}
break;
}
case BF_END_LOOP: {
+ //printf("Endloop! i = %d\n",i);
+ //printf("memVal: %d\n", mem_get());
+ //printf("stackVal: %d\n", stackPeek(&stack));
+ //printf("%c\n\n", program[i]);
+
if (mem_get() != 0){
- i = stackPop();
- stackPush(i);
+ i = stackPop(&stack);
+ stackPush(&stack, i);
}
else
- stackPop();
+ stackPop(&stack);
break;
}
diff --git a/src/mem.c b/src/mem.c
index 798fa1b..9b43c9d 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <string.h>
#include "mem.h"
struct mem_st mem = { .pos = 0, .arr={0}};
@@ -62,3 +63,37 @@ void mem_printDebug(){
}
printf("\n");
}
+
+
+
+/*
+// Praks 10-11 Added
+
+void mem_add(int amount){
+ mem.arr[mem.pos] += amount;
+}
+
+mem_int(){return mem_add(-1);};
+mem_dec(){return mem_add(1);};
+
+int mem_move(int amount) {
+ int new_pos = mem.pos + amount;
+ if (amount < 0) {
+ //NB, siin võib olla probleem
+ new_pos += (MEM_SIZE -1);
+ }
+ else if (new_pos >= MEM_SIZE){
+ new_pos %= MEM_SIZE;
+ }
+ mem.pos = new_pos;
+ return mem_pos;
+}
+
+void mem_init() {
+ memset(mem.arr, 0, MEM_SIZE * sizeof(char));
+ //for (int i = 0; i < MEM_SIZE; ++i) {
+ // mem.arr[i] = 0;
+ //}
+}
+
+*/
diff --git a/src/stack.c b/src/stack.c
index d29d1a7..afdf243 100644
--- a/src/stack.c
+++ b/src/stack.c
@@ -1,55 +1,100 @@
-#include "stack.h"
#include <stdio.h>
+#include <stdlib.h>
+#include "stack.h"
-#define STACK_SIZE 100
+void stackPush(struct stack_st* s, int val){
-struct stack_st stack = { .len = 0, .arr = {0}};
+ if (s->len == 0 || s->arr == NULL){
-// Functions
+ // Peame küsima mälu
+ s->size = 1;
+ //printf("yeet: %d\n", *(s->arr));
+ s->arr = (int*) malloc(s->size * sizeof(int));
-void stackPush(int val){
- if (stack.len >= STACK_SIZE){
- printf("Stack overrflow\n");
- return;
+ // Kontrollime, et mälu allokeermisel ei tekinud probleeme
+ if (s->arr == NULL) {
+ printf("Mälu otsas\n");
+ return;
+ }
+ s->arr[s->len++] = val;
+
+ } // s->len == 0 end
+
+ else if (s->len == s->size){
+ int tmp_size = 2 * s->size;
+ int* tmp_arr = realloc(s->arr, tmp_size);
+
+ // Kontrollime, kas saime mälu juurde
+ if (tmp_arr == NULL) {
+ printf("Mäluala suurendamine Erroris\n");
+ return;
+ }
+
+ s->size = tmp_size;
+ s->arr = tmp_arr;
+
+ s->arr[s->len] = val;
+ s->len++;
+ return;
+ }
+ else {
+ s->arr[s->len++] = val;
}
- stack.arr[stack.len] = val;
- stack.len++;
- return;
}
-int stackPop(void){
- if (stackIsEmpty()){
- return 0;
+int stackPop(struct stack_st *s){
+ int x = s->arr[--s->len];
+ int tmp_size = s->size / 2;
+
+ // Kui vaja s-> mälu suurust kahandada
+ if (s->len < tmp_size){
+ int* tmp_arr = realloc(s->arr, tmp_size);
+
+ if (!tmp_arr){
+ printf("No good, malu otsas");
+ return 0;
+ }
+
+ s->size = tmp_size;
+ s->arr = tmp_arr;
}
- int stackPopped = stack.arr[--stack.len];
- return stackPopped;
+
+ if (s->len == 0)
+ stackFree(s);
+
+ return x;
+}
+
+void stackFree(struct stack_st *s){
+ free(s->arr);
+ s->arr = NULL;
}
-int stackIsEmpty(void) {
- return !(stack.len > 0);
+int stackIsEmpty(struct stack_st *s) {
+ return !(s->len > 0);
}
-int stackPeek(){
- // To avoid funking up the stack.len
- if (stackIsEmpty()) return 0;
- else if (stack.len == 1) return stack.arr[1];
+int stackPeek(struct stack_st *s){
+ // To avoid funking up the s->len
+ if (stackIsEmpty(s)) return 0;
+ else if (s->len == 1) return s->arr[1];
else{
- int x = stackPop();
- stackPush(x);
+ int x = stackPop(s);
+ stackPush(s, x);
return x;
}
}
-void stackPrint(){
+void stackPrint(struct stack_st *s){
while(1){
- if(stackIsEmpty())
+ if(stackIsEmpty(s))
break;
- int x = stackPop();
+ int x = stackPop(s);
printf("%d\n", x);
}
return;
diff --git a/src/translator.c b/src/translator.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/translator.c