njvm/reg.c

49 lines
1.0 KiB
C
Executable File

//
// Created by Nils on 29.10.2023.
//
#ifndef REG
#define REG
#include <stdio.h>
#include <stdlib.h>
struct reg {
int* size;
int* current;
unsigned int *stack;
};
void printStackR(struct reg stack) {
printf("Regurn reg\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
for (int i = *stack.current-1; i > 0; --i) {
printf("||%i||\n", stack.stack[i]);
}
}
void pushR(struct reg s, unsigned int value) {
if (*s.current >= *s.size) {
printf("Stack Overflow\n");
exit(EXIT_FAILURE);
}
s.stack[*s.current] = value;
*s.current=*s.current + 1;
}
unsigned int popR(struct reg s) {
if (*s.current == 0) {
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
}
*s.current = *s.current -1;
return s.stack[*s.current];
}
unsigned int peekR(struct reg s, int steps) {
if (*s.current - steps < 0) {
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
}
return s.stack[*s.current - steps];
}
#endif