source: doc/papers/general/evaluation/cfa-stack.c @ dd05e12

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since dd05e12 was 986dd36, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Update CFA evaluation code

  • Property mode set to 100644
File size: 1.3 KB
Line 
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
9forall(otype T) void ?{}(stack(T)& s) { (s.head){ 0 }; }
10
11forall(otype T) void ?{}(stack(T)& s, stack(T) t) {
12        stack_node(T)** crnt = &s.head;
13        for ( stack_node(T)* next = t.head; next; next = next->next ) {
14                // *crnt = &(*(stack_node(T)*)malloc()){ next->value }; /***/
15                stack_node(T)* new_node = ((stack_node(T)*)malloc());
16                (*new_node){ next->value }; /***/
17                *crnt = new_node;
18
19                stack_node(T)* acrnt = *crnt;
20                crnt = &acrnt->next;
21        }
22        *crnt = 0;
23}
24
25forall(otype T) stack(T) ?=?(stack(T)& s, stack(T) t) {
26        if ( s.head == t.head ) return s;
27        clear(s);
28        s{ t };
29        return s;
30}
31
32forall(otype T) void ^?{}(stack(T)& s) { clear(s); }
33
34forall(otype T) _Bool empty(const stack(T)& s) { return s.head == 0; }
35
36forall(otype T) void push(stack(T)& s, T value) {
37        // s.head = &(*(stack_node(T)*)malloc()){ value, s.head }; /***/
38        stack_node(T)* new_node = ((stack_node(T)*)malloc());
39        (*new_node){ value, s.head }; /***/
40        s.head = new_node;
41}
42
43forall(otype T) T pop(stack(T)& s) {
44        stack_node(T)* n = s.head;
45        s.head = n->next;
46        T x = n->value;
47        ^n{};
48        free(n);
49        return x;
50}
51
52forall(otype T) void clear(stack(T)& s) {
53    for ( stack_node(T)* next = s.head; next; ) {
54                stack_node(T)* crnt = next;
55                next = crnt->next;
56                delete(crnt);
57        }
58        s.head = 0;
59}
Note: See TracBrowser for help on using the repository browser.