From 920251e99f937239556981b170dc100a8a1d0b12 Mon Sep 17 00:00:00 2001 From: Laborratte5 Date: Sat, 20 Jul 2024 21:41:27 +0200 Subject: [PATCH] Implement OperandStack for Runtime --- runtime/Makefile | 7 ++++++- runtime/OperandStack.c | 29 +++++++++++++++++++++++++++++ runtime/OperandStack.h | 17 +++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 runtime/OperandStack.c create mode 100644 runtime/OperandStack.h diff --git a/runtime/Makefile b/runtime/Makefile index ee84e75..f39f16c 100644 --- a/runtime/Makefile +++ b/runtime/Makefile @@ -1,5 +1,10 @@ CC=gcc CFLAGS=-I. +DEPS=OperandStack.h +OBJ=OperandStack.o salruntime.o -salruntime: salruntime.c +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +salruntime: $(OBJ) $(CC) -o $@ $^ $(CFLAGS) diff --git a/runtime/OperandStack.c b/runtime/OperandStack.c new file mode 100644 index 0000000..e414450 --- /dev/null +++ b/runtime/OperandStack.c @@ -0,0 +1,29 @@ +#include "OperandStack.h" + +int OStackSize(OperandStack* stack) { + return stack->top; +} + +int OStackPush(OperandStack* stack, int operand) { + if (OStackSize(stack) >= TYPE_STACK_SIZE) { + // Stack overflow + return -1; + } + + stack->operands[stack->top] = operand; + stack->top++; + + return 0; +} + +int OStackPop(OperandStack* stack, int* result) { + if (OStackSize(stack) == 0) { + // Stack underflow + return -1; + } + + *result = stack->operands[stack->top - 1]; + stack->top--; + + return 0; +} diff --git a/runtime/OperandStack.h b/runtime/OperandStack.h new file mode 100644 index 0000000..7e1770e --- /dev/null +++ b/runtime/OperandStack.h @@ -0,0 +1,17 @@ +#ifndef OPERAND_STACK +#define OPERAND_STACK + +#define TYPE_STACK_SIZE 32 + +typedef struct OperandStack { + int top; + int operands[TYPE_STACK_SIZE]; +} OperandStack; + +int OStackSize(OperandStack* stack); + +int OStackPush(OperandStack* stack, int operand); + +int OStackPop(OperandStack* stack, int* result); + +#endif