diff options
author | Rasmus Luha <rasmus.luha@gmail.com> | 2022-09-21 14:16:01 +0300 |
---|---|---|
committer | Rasmus Luha <rasmus.luha@gmail.com> | 2022-09-21 14:16:01 +0300 |
commit | c34cd59be89fb26e63fe959b98afabb4f24ba5bd (patch) | |
tree | 60531983cb0bfef27ab91cb3be41ebd40b324c9f |
init commit
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | Makefile | 25 | ||||
-rw-r--r-- | inc/bfi.h | 17 | ||||
-rw-r--r-- | inc/mem.h | 19 | ||||
-rw-r--r-- | inc/stack.h | 10 | ||||
-rw-r--r-- | notes.txt | 6 | ||||
-rw-r--r-- | src/bfi.c | 6 | ||||
-rw-r--r-- | src/main.c | 45 | ||||
-rw-r--r-- | src/mem.c | 60 | ||||
-rw-r--r-- | src/stack.c | 61 |
10 files changed, 255 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..51d9359 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +obj/ +lib/ +GPATH +GRTAGS +GTAGS +.dir-locals.el diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..67ded2b --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +CFLAGS := -Iinc -g -Og -Wall + +# Lähtekood +SRC := $(wildcard src/*.c) +OBJ := $(patsubst src/%.c, obj/%.o, $(SRC)) + +all: lib/interpret + +lib/interpret: $(OBJ) | lib + gcc $(CFLAGS) $(OBJ) -o lib/interpret + +obj/%.o: src/%.c | obj + gcc -c $(CFLAGS) -o $@ $< + +# Dirs +lib: + mkdir lib + +obj: + mkdir obj + +.PHONY: clean +clean: + rm -r obj lib + diff --git a/inc/bfi.h b/inc/bfi.h new file mode 100644 index 0000000..7f3dbce --- /dev/null +++ b/inc/bfi.h @@ -0,0 +1,17 @@ +#ifndef __BFI_H__ +#define __BFI_H__ + +enum instructions_e{ + BF_RIGHT = '>', + BF_LEFT = '<', + BF_INCREASE = '+', + BF_DECREASE = '-', + BF_READ = ',', + BF_PRINT = '.', + BF_START_LOOP = '[', + BF_END_LOOP = ']', + BF_DEBUG = '#'}; + +void intepret(char *program); + +#endif diff --git a/inc/mem.h b/inc/mem.h new file mode 100644 index 0000000..50a0a17 --- /dev/null +++ b/inc/mem.h @@ -0,0 +1,19 @@ +#ifndef __MEM_H__ +#define __MEM_H__ + +#define MEM_SIZE 30000 + +struct mem_st { + char arr[MEM_SIZE]; + int pos; +}; + +int mem_inc(); +int mem_dec(); +int mem_left(); +int mem_right(); +int mem_get(); +int mem_set(char v); +void mem_printDebug(); + +#endif diff --git a/inc/stack.h b/inc/stack.h new file mode 100644 index 0000000..60d41b8 --- /dev/null +++ b/inc/stack.h @@ -0,0 +1,10 @@ +#ifndef __STACK_H__ +#define __STACK_H__ + +void stackPush(int val); +int stackPop(void); +int stackIsEmpty(void); +int stackPeek(void); +void stackPrint(void); + +#endif diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..02f9c0b --- /dev/null +++ b/notes.txt @@ -0,0 +1,6 @@ +What are exactly object files? +#ifdef ja #ifndef check over +what are enums +https://www.gnu.org/software/make/manual/html_node/Syntax-of-Functions.html#Syntax-of-Functions +.PHONY? + diff --git a/src/bfi.c b/src/bfi.c new file mode 100644 index 0000000..0171ede --- /dev/null +++ b/src/bfi.c @@ -0,0 +1,6 @@ +#include "bfi.h" + +void intepret(char *program){ + +} + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..ec1de31 --- /dev/null +++ b/src/main.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include "stack.h" + +int main() { + int x, y, z; + + while (1) { + scanf("%d", &x); + + // 1. Kui x==0 siis print stack. + if (x == 0){ + printf("\nPrindin stacki\n"); + stackPrint(); + break; + } + + // 2. Kui empty Push. + else if (stackIsEmpty()) + stackPush(x); + + // 3. Kui x pos. ja peeked pos: push(x). + // 4. kui x pos. aga peeked neg., y=pop() ja push(x+y). + else if (x >= 0){ + y = stackPeek(); + if (y >= 0) + stackPush(x); + + else{ + z = stackPop(); + if (x+z != 0) + stackPush(x+z); + } + } + + // 5. Kui x on neg, siis pop() + else if (x < 0) { + z = stackPop(); + if (x+z != 0) { + stackPush(x+z); + } + } + + } // While loop End + return 0; +} diff --git a/src/mem.c b/src/mem.c new file mode 100644 index 0000000..3c605a5 --- /dev/null +++ b/src/mem.c @@ -0,0 +1,60 @@ +#include <stdio.h> +#include "mem.h" + +struct mem_st mem = { .pos = 0}; + +int mem_inc(){ + mem.arr[mem.pos]++; + return mem.arr[mem.pos]; +} + +int mem_dec(){ + mem.arr[mem.pos]--; + return mem.arr[mem.pos]; +} + +int mem_left(){ + if (mem.pos <= 0) { + mem.pos = MEM_SIZE-1; + } + else { + mem.pos--; + } + return mem.pos; +} + +int mem_right(){ + if (mem.pos >= MEM_SIZE-1) { + mem.pos = 0; + } + else { + mem.pos++; + } + return mem.pos; + +} + +int mem_get(){ + return mem.arr[mem.pos]; +} + +int mem_set(char v){ + mem.arr[mem.pos] = v; + return v; +} + +void mem_printDebug(){ + if (mem.pos+9 >= MEM_SIZE) + printf("index: %d [%d .. %d]: ", mem.pos, mem.pos, mem.pos-MEM_SIZE+9); + printf("index: %d [%d .. %d]: ", mem.pos, mem.pos, mem.pos+9); + + for (int i = 0; i < 10; ++i) { + if (mem.pos+i >= MEM_SIZE) { + printf("%d ", mem.arr[mem.pos-MEM_SIZE+i]); + } + else { + printf("%d ", mem.arr[mem.pos+i]); + } + } + +} diff --git a/src/stack.c b/src/stack.c new file mode 100644 index 0000000..2fe3488 --- /dev/null +++ b/src/stack.c @@ -0,0 +1,61 @@ +#include "stack.h" +#include <stdio.h> + +#define STACK_SIZE 100 + +struct stack_st { + int len; + int arr[STACK_SIZE]; +}; + +struct stack_st stack = { .len = 0, .arr = {0}}; + +// Functions + +void stackPush(int val){ + if (stack.len >= STACK_SIZE){ + printf("Stack overrflow\n"); + return; + } + stack.arr[stack.len] = val; + stack.len++; + return; +} + + +int stackPop(void){ + if (stackIsEmpty()){ + return 0; + } + int stackPopped = stack.arr[--stack.len]; + return stackPopped; +} + + +int stackIsEmpty(void) { + return !(stack.len > 0); +} + + +int stackPeek(){ + // To avoid funking up the stack.len + if (stackIsEmpty()) return 0; + else if (stack.len == 1) return stack.arr[1]; + else{ + int x = stackPop(); + stackPush(x); + return x; + } +} + + +void stackPrint(){ + while(1){ + if(stackIsEmpty()) + break; + + int x = stackPop(); + printf("%d\n", x); + } + return; +} |