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

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since f465f0e was ac4dad2, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

shorten experimental code

  • Property mode set to 100644
File size: 1003 bytes
Line 
1#include <stdlib>
2#include "cfa-stack.h"
3
4forall( otype T ) {
5 struct node {
6 T value;
7 node(T) * next;
8 };
9
10 void ?{}( stack(T) & s ) { (s.head){ 0 }; }
11
12 void ?{}( stack(T) & s, stack(T) t ) {
13 node(T) ** cr = &s.head;
14 for ( node(T) * nx = t.head; nx; nx = nx->next ) {
15 *cr = alloc();
16 ((*cr)->value){ nx->value };
17 cr = &(*cr)->next;
18 }
19 *cr = 0;
20 }
21
22 void ^?{}( stack(T) & s) { clear( s ); }
23
24 void clear( stack(T) & s ) with( s ) {
25 for ( node(T) * nx = head; nx; ) {
26 node(T) * cr = nx;
27 nx = cr->next;
28 ^(*cr){};
29 free(cr);
30 }
31 head = 0;
32 }
33
34 stack(T) ?=?( stack(T) & s, stack(T) t ) {
35 if ( s.head == t.head ) return s;
36 clear( s );
37 s{ t };
38 return s;
39 }
40
41 _Bool empty( const stack(T) & s ) { return s.head == 0; }
42
43 void push( stack(T) & s, T value ) with( s ) {
44 node(T) * n = alloc();
45 (*n){ value, head };
46 head = n;
47 }
48
49 T pop( stack(T) & s ) with( s ) {
50 node(T) * n = head;
51 head = n->next;
52 T v = n->value;
53 ^(*n){};
54 free( n );
55 return v;
56 }
57}
Note: See TracBrowser for help on using the repository browser.