Implement OperandStack for Runtime

This commit is contained in:
Laborratte 5 2024-07-20 21:41:27 +02:00
parent 986c04053e
commit 920251e99f
Signed by: Laborratte5
GPG key ID: 3A30072E35202C02
3 changed files with 52 additions and 1 deletions

View file

@ -1,5 +1,10 @@
CC=gcc CC=gcc
CFLAGS=-I. 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) $(CC) -o $@ $^ $(CFLAGS)

29
runtime/OperandStack.c Normal file
View file

@ -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;
}

17
runtime/OperandStack.h Normal file
View file

@ -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