source: doc/generic_types/evaluation/cfa-stack.c @ a381b46

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since a381b46 was a381b46, checked in by Aaron Moss <a3moss@…>, 5 years ago

Minor cleanup, also filled in benchmark source appendix

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[309be81]1#include <stdlib>
2#include "cfa-stack.h"
3
4forall(otype T) struct stack_node {
5        T value;
6        stack_node(T)* next;
7};
8
[a381b46]9forall(otype T) void ?{}(stack(T)* s) { (&s->head){ 0 }; }
[d919f47]10
[122aecd]11forall(otype T) void ?{}(stack(T)* s, stack(T) t) {
12        stack_node(T)** crnt = &s->head;
13        stack_node(T)* next = t.head;
[309be81]14        while ( next ) {
[3fb7f5e]15                *crnt = ((stack_node(T)*)malloc()){ next->value }; /***/
[122aecd]16                stack_node(T)* acrnt = *crnt;
17                crnt = &acrnt->next;
18                next = next->next;
[309be81]19        }
[122aecd]20        *crnt = 0;
21}
22
23forall(otype T) stack(T) ?=?(stack(T)* s, stack(T) t) {
24        if ( s->head == t.head ) return *s;
25        clear(s);
26        s{ t };
27        return *s;
28}
29
[a381b46]30forall(otype T) void ^?{}(stack(T)* s) { clear(s); }
[309be81]31
[a381b46]32forall(otype T) _Bool empty(const stack(T)* s) { return s->head == 0; }
[309be81]33
34forall(otype T) void push(stack(T)* s, T value) {
[3fb7f5e]35        s->head = ((stack_node(T)*)malloc()){ value, s->head }; /***/
[309be81]36}
37
38forall(otype T) T pop(stack(T)* s) {
39        stack_node(T)* n = s->head;
40        s->head = n->next;
41        T x = n->value;
[65cb413]42        ^n{};
43        free(n);
[309be81]44        return x;
45}
[122aecd]46
47forall(otype T) void clear(stack(T)* s) {
48        stack_node(T)* next = s->head;
49        while ( next ) {
50                stack_node(T)* crnt = next;
51                next = crnt->next;
52                delete(crnt);
53        }
[d919f47]54        s->head = 0;
[122aecd]55}
Note: See TracBrowser for help on using the repository browser.