back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/stack.c
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-03-31 21:05:36 +0300
committerscratko <m@scratko.xyz>2024-03-31 21:05:36 +0300
commit7d07ff825dc7cff7ee80b04346e0d2c49a91e190 (patch)
treef3bf4c72c22107b0d74143f09af39b0071106540 /stack.c
downloadarithmetic-expression-computator-7d07ff825dc7cff7ee80b04346e0d2c49a91e190.tar.gz
arithmetic-expression-computator-7d07ff825dc7cff7ee80b04346e0d2c49a91e190.tar.bz2
arithmetic-expression-computator-7d07ff825dc7cff7ee80b04346e0d2c49a91e190.zip
Initial commit
Diffstat (limited to 'stack.c')
-rw-r--r--stack.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/stack.c b/stack.c
new file mode 100644
index 0000000..8aee3d7
--- /dev/null
+++ b/stack.c
@@ -0,0 +1,68 @@
+#include "stack.h"
+#include "calls.h"
+
+#define QUANTITY_ADDRESSES 5
+
+enum address_control { get_address, set_address };
+void* heap_base;
+
+void init_stack(stack *st)
+{
+ *st = 0;
+}
+
+static void* address_management(void* address, enum address_control state)
+{
+ enum { null = 0 };
+ static void* available_addresses[QUANTITY_ADDRESSES];
+ int i;
+ if(state == set_address) {
+ for(i = 0; i < QUANTITY_ADDRESSES; ++i) {
+ if(!available_addresses[i]) {
+ available_addresses[i] = address;
+ return null;
+ }
+ }
+ return null;
+ }
+ for(i = 0; i < QUANTITY_ADDRESSES; ++i) {
+ if(available_addresses[i]) {
+ address = available_addresses[i];
+ available_addresses[i] = 0;
+ return address;
+ }
+ }
+ return null;
+}
+
+
+void push_stack(stack *st, long num)
+{
+ struct item *tmp;
+ tmp = address_management(0, get_address);
+ if(!tmp)
+ tmp = sys_alloc(sizeof(struct item));
+ if(!heap_base)
+ heap_base = tmp;
+ tmp->data = num;
+ tmp->next = *st;
+ *st = tmp;
+}
+
+long pop_stack(stack *st)
+{
+ long num = (*st)->data;
+ address_management(*st, set_address);
+ *st = (*st)->next;
+ return num;
+}
+
+int is_empty_stack(const stack *st)
+{
+ return *st ? 0 : 1;
+}
+
+long top_stack(const stack *st)
+{
+ return (*st)->data;
+}