#include #include "cfa-stack.h" forall(otype T) struct stack_node { T value; stack_node(T) * next; }; forall(otype T) void ?{}( stack(T) & s ) { (s.head){ 0 }; } forall(otype T) void ?{}( stack(T) & s, stack(T) t ) { stack_node(T) ** crnt = &s.head; for ( stack_node(T) * next = t.head; next; next = next->next ) { *crnt = alloc(); ((*crnt)->value){ next->value }; crnt = &(*crnt)->next; } *crnt = 0; } forall(otype T) stack(T) ?=?( stack(T) & s, stack(T) t ) { if ( s.head == t.head ) return s; clear( s ); s{ t }; return s; } forall(otype T) void ^?{}( stack(T) & s) { clear( s ); } forall(otype T) _Bool empty( const stack(T) & s ) { return s.head == 0; } forall(otype T) void push( stack(T) & s, T value ) with( s ) { stack_node(T)* n = alloc(); (*n){ value, head }; head = n; } forall(otype T) T pop( stack(T) & s ) with( s ) { stack_node(T) * n = head; head = n->next; T x = n->value; ^(*n){}; free( n ); return x; } forall(otype T) void clear( stack(T) & s ) with( s ) { for ( stack_node(T) * next = head; next; ) { stack_node(T) * crnt = next; next = crnt->next; ^(*crnt){}; free(crnt); } head = 0; }