Changeset ac4dad2


Ignore:
Timestamp:
Apr 28, 2018, 9:20:28 AM (6 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, with_gc
Children:
6926a6d
Parents:
aa5fdac
Message:

shorten experimental code

Location:
doc/papers/general/evaluation
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/general/evaluation/c-bench.c

    raa5fdac rac4dad2  
    55#include "c-stack.h"
    66
    7 char* new_char( char c ) {
    8         char* q = malloc(sizeof(char)); /***/
     7char * new_char( char c ) {
     8        char* q = malloc( sizeof(char) ); /***/
    99        *q = c;
    1010        return q;
    1111}
    1212
    13 short* new_short( short s ) {
    14         short* q = malloc(sizeof(short)); /***/
     13short * new_short( short s ) {
     14        short* q = malloc( sizeof(short) ); /***/
    1515        *q = s;
    1616        return q;
     
    1818
    1919int* new_int( int i ) {
    20         int* q = malloc(sizeof(int)); /***/
     20        int* q = malloc( sizeof(int) ); /***/
    2121        *q = i;
    2222        return q;
    2323}
    2424
    25 void* copy_char( const void* p ) { return new_char( *(const char*)p ); } /***/
    26 void* copy_short( const void* p ) { return new_short( *(const short*)p ); } /***/
    27 void* copy_int( const void* p ) { return new_int( *(const int*)p ); } /***/
    28 void* copy_pair_short_char( const void* p ) { return copy_pair( p, copy_short, copy_char ); } /***/
    29 void free_pair_short_char( void* p ) { free_pair( p, free, free ); } /***/
     25void * copy_char( const void * p ) { return new_char( *(const char*)p ); } /***/
     26void * copy_short( const void * p ) { return new_short( *(const short*)p ); } /***/
     27void * copy_int( const void * p ) { return new_int( *(const int*)p ); } /***/
     28void * copy_pair_short_char( const void * p ) { return copy_pair( p, copy_short, copy_char ); } /***/
     29void free_pair_short_char( void * p ) { free_pair( p, free, free ); } /***/
    3030
    3131int cmp_char( const void* a, const void* b ) { /***/
     
    3737}
    3838
    39 int main(int argc, char** argv) {
     39int main(int argc, char * argv[] ) {
    4040        int maxi = 0, vali = 42;
    4141        struct stack si = new_stack(), ti;
  • doc/papers/general/evaluation/c-pair.c

    raa5fdac rac4dad2  
    22#include "c-pair.h"
    33
    4 struct pair* new_pair(void* first, void* second) {
    5         struct pair* p = malloc(sizeof(struct pair)); /***/
    6         *p = (struct pair){ first, second }; /***/
     4pair * new_pair( void * first, void * second ) {
     5        pair * p = malloc( sizeof(pair) ); /***/
     6        *p = (pair){ first, second }; /***/
    77        return p;
    88}
    99
    10 struct pair* copy_pair(const struct pair* src,
    11                 void* (*copy_first)(const void*), void* (*copy_second)(const void*)) {
     10pair * copy_pair( const pair * src,
     11                void * (* copy_first)(const void* ), void * (* copy_second)(const void *)) {
    1212        return new_pair( copy_first(src->first), copy_second(src->second) );
    1313}
    1414
    15 void free_pair(struct pair* p, void (*free_first)(void*), void (*free_second)(void*)) {
    16         free_first(p->first);
    17         free_second(p->second);
    18         free(p);
     15void free_pair( pair * p, void (* free_first)(void *), void (* free_second)(void *)) {
     16        free_first( p->first );
     17        free_second( p->second );
     18        free( p );
    1919}
    2020
    21 int cmp_pair(const struct pair* a, const struct pair* b,
    22                 int (*cmp_first)(const void*, const void*), int (*cmp_second)(const void*, const void*)) {
    23         int c = cmp_first(a->first, b->first);
    24         if ( c == 0 ) c = cmp_second(a->second, b->second);
     21int cmp_pair( const pair * a, const pair * b,
     22                int (* cmp_first)(const void *, const void *), int (* cmp_second)(const void *, const void *)) {
     23        int c = cmp_first( a->first, b->first );
     24        if ( c == 0 ) c = cmp_second( a->second, b->second );
    2525        return c;
    2626}
  • doc/papers/general/evaluation/c-pair.h

    raa5fdac rac4dad2  
    11#pragma once
    22
    3 struct pair {
    4         void* first;
    5         void* second;
    6 };
     3typedef struct pair {
     4        void * first;
     5        void * second;
     6} pair;
    77
    8 struct pair* new_pair(void* first, void* second);
     8pair * new_pair( void * first, void * second );
    99
    10 struct pair* copy_pair(const struct pair* src,
    11         void* (*copy_first)(const void*), void* (*copy_second)(const void*));
     10pair * copy_pair( const pair * src,
     11        void * (* copy_first)(const void *), void * (* copy_second)(const void *));
    1212
    13 void free_pair(struct pair* p, void (*free_first)(void*), void (*free_second)(void*));
     13void free_pair( pair * p, void (* free_first)(void *), void (* free_second)(void *));
    1414
    15 int cmp_pair(const struct pair* a, const struct pair* b,
    16         int (*cmp_first)(const void*, const void*), int (*cmp_second)(const void*, const void*));
     15int cmp_pair( const pair * a, const pair * b,
     16        int (* cmp_first)(const void *, const void *), int (* cmp_second)(const void *, const void *));
  • doc/papers/general/evaluation/c-print.c

    raa5fdac rac4dad2  
    44#include "c-print.h"
    55
    6 void print_string(FILE* out, const char* x) { fprintf(out, "%s", x); }
     6void print_string( FILE * out, const char * x ) { fprintf( out, "%s", x ); }
    77
    8 void print_bool(FILE* out, _Bool x) { fprintf(out, "%s", x ? "true" : "false"); }
     8void print_bool( FILE * out, _Bool x ) { fprintf( out, "%s", x ? "true" : "false" ); }
    99
    10 void print_char(FILE* out, char x) {
    11         if ( 0x20 <= x && x <= 0x7E ) { fprintf(out, "'%c'", x); }
    12         else { fprintf(out, "'\\%x'", x); }
     10void print_char( FILE * out, char x ) {
     11        if ( 0x20 <= x && x <= 0x7E ) { fprintf( out, "'%c'", x ); }
     12        else { fprintf( out, "'\\%x'", x ); }
    1313}
    1414
    15 void print_int(FILE* out, int x) { fprintf(out, "%d", x); }
     15void print_int( FILE * out, int x ) { fprintf( out, "%d", x ); }
    1616
    17 void print_fmt(FILE* out, char fmt, void* p) {
     17void print_fmt( FILE * out, char fmt, void * p ) {
    1818        switch( fmt ) {
    19         case 's': print_string(out, (const char*)p); break; /***/
    20         case 'b': print_bool(out, *(_Bool*)p); break; /***/
    21         case 'c': print_char(out, *(char*)p); break; /***/
    22         case 'd': print_int(out, *(int*)p); break; /***/
     19        case 's': print_string( out, (const char*)p ); break; /***/
     20        case 'b': print_bool( out, *(_Bool*)p ); break; /***/
     21        case 'c': print_char( out, *(char*)p ); break; /***/
     22        case 'd': print_int( out, *(int*)p ); break; /***/
    2323        }
    2424}
    2525
    26 void print(FILE* out, const char* fmt, ...) {
     26void print( FILE * out, const char * fmt, ... ) {
    2727        va_list args;
    2828        va_start(args, fmt);
    29         for (const char* it = fmt; *it; ++it) {
     29        for ( const char * it = fmt; *it; ++it ) {
    3030                switch( *it ) {
    31                 case 's': print_string(out, va_arg(args, const char*)); break; /***/
    32                 case 'b': print_bool(out, va_arg(args, int)); break; /***/
    33                 case 'c': print_char(out, va_arg(args, int)); break; /***/
    34                 case 'd': print_int(out, va_arg(args, int)); break; /***/
     31                case 's': print_string( out, va_arg( args, const char * ) ); break; /***/
     32                case 'b': print_bool( out, va_arg( args, int ) ); break; /***/
     33                case 'c': print_char( out, va_arg( args, int ) ); break; /***/
     34                case 'd': print_int( out, va_arg( args, int ) ); break; /***/
    3535                case 'p': {
    36                         const struct pair x = va_arg(args, const struct pair); /***/
    37                         fprintf(out, "[");
    38                         print_fmt(out, *++it, x.first); /***/
    39                         fprintf(out, ", ");
    40                         print_fmt(out, *++it, x.second); /***/
    41                         fprintf(out, "]");
     36                        const struct pair x = va_arg( args, const struct pair ); /***/
     37                        fprintf( out, "[" );
     38                        print_fmt( out, *++it, x.first ); /***/
     39                        fprintf( out, ", " );
     40                        print_fmt( out, *++it, x.second ); /***/
     41                        fprintf( out, "]" );
    4242                        break;
    4343                }
    4444                }
    4545        }
    46         va_end(args);
     46        va_end( args );
    4747}
  • doc/papers/general/evaluation/c-print.h

    raa5fdac rac4dad2  
    22#include <stdio.h>
    33
    4 void print_string(FILE* out, const char* x);
    5 void print_bool(FILE* out, _Bool x);
    6 void print_char(FILE* out, char x);
    7 void print_int(FILE* out, int x);
     4void print_string( FILE * out, const char * x );
     5void print_bool( FILE * out, _Bool x );
     6void print_char( FILE * out, char x );
     7void print_int( FILE * out, int x );
    88
    9 void print(FILE* out, const char* fmt, ...);
     9void print( FILE * out, const char * fmt, ... );
  • doc/papers/general/evaluation/c-stack.c

    raa5fdac rac4dad2  
    22#include "c-stack.h"
    33
    4 struct stack_node {
     4typedef struct node {
    55        void * value;
    6         struct stack_node * next;
    7 };
     6        struct node * next;
     7} node;
    88
    9 void clear_stack( struct stack * s, void (*free_el)( void * ) ) {
    10         for ( struct stack_node * next = s->head; next; ) {
    11                 struct stack_node * crnt = next;
    12                 next = crnt->next;
    13                 free_el( crnt->value );
    14                 free( crnt );
     9void copy_stack( stack * s, const stack * t, void * (*copy)( const void * ) ) {
     10        node ** cr = &s->head;
     11        for ( node * nx = t->head; nx; nx = nx->next ) {
     12                *cr = malloc( sizeof(node) ); /***/
     13                (*cr)->value = copy( nx->value );
     14                cr = &(*cr)->next;
     15        }
     16        *cr = NULL;
     17}
     18
     19void clear_stack( stack * s, void (* free_el)( void * ) ) {
     20        for ( node * nx = s->head; nx; ) {
     21                node * cr = nx;
     22                nx = cr->next;
     23                free_el( cr->value );
     24                free( cr );
    1525        }
    1626        s->head = NULL;
    1727}
    1828
    19 struct stack new_stack() { return (struct stack){ NULL }; /***/ }
     29stack new_stack() { return (stack){ NULL }; /***/ }
    2030
    21 void copy_stack( struct stack * s, const struct stack * t, void * (*copy)( const void * ) ) {
    22         struct stack_node ** crnt = &s->head;
    23         for ( struct stack_node * next = t->head; next; next = next->next ) {
    24                 *crnt = malloc( sizeof(struct stack_node) ); /***/
    25                 (*crnt)->value = copy( next->value );
    26                 crnt = &(*crnt)->next;
    27         }
    28         *crnt = NULL;
    29 }
    30 struct stack * assign_stack( struct stack * s, const struct stack * t,
     31stack * assign_stack( stack * s, const stack * t,
    3132                void * (*copy_el)( const void * ), void (*free_el)( void * ) ) {
    3233        if ( s->head == t->head ) return s;
     
    3637}
    3738
    38 _Bool stack_empty( const struct stack * s ) { return s->head == NULL; }
     39_Bool stack_empty( const stack * s ) { return s->head == NULL; }
    3940
    40 void push_stack( struct stack * s, void * v ) {
    41         struct stack_node * n = malloc( sizeof(struct stack_node) ); /***/
    42         *n = (struct stack_node){ v, s->head }; /***/
     41void push_stack( stack * s, void * v ) {
     42        node * n = malloc( sizeof(node) ); /***/
     43        *n = (node){ v, s->head }; /***/
    4344        s->head = n;
    4445}
    4546
    46 void * pop_stack( struct stack * s ) {
    47         struct stack_node * n = s->head;
     47void * pop_stack( stack * s ) {
     48        node * n = s->head;
    4849        s->head = n->next;
    4950        void * v = n->value;
  • doc/papers/general/evaluation/c-stack.h

    raa5fdac rac4dad2  
    11#pragma once
    22
    3 struct stack_node;
    4 struct stack {
    5         struct stack_node* head;
    6 };
     3struct node;
     4typedef struct stack {
     5        struct node * head;
     6} stack;
    77
    8 struct stack new_stack();
    9 void copy_stack(struct stack* dst, const struct stack* src, void* (*copy)(const void*));
    10 struct stack* assign_stack(struct stack* dst, const struct stack* src,
    11         void* (*copy_el)(const void*), void (*free_el)(void*));
    12 void clear_stack(struct stack* s, void (*free_el)(void*));
     8stack new_stack();
     9void copy_stack(stack * dst, const stack * src, void * (* copy)(const void *));
     10stack * assign_stack( stack * dst, const stack * src,
     11        void * (* copy_el)(const void *), void (* free_el)(void *));
     12void clear_stack(stack * s, void (*free_el)(void *));
    1313
    14 _Bool stack_empty(const struct stack* s);
    15 void push_stack(struct stack* s, void* value);
    16 void* pop_stack(struct stack* s);
     14_Bool stack_empty( const stack * s );
     15void push_stack( stack * s, void * value );
     16void * pop_stack( stack * s );
  • doc/papers/general/evaluation/cfa-stack.c

    raa5fdac rac4dad2  
    22#include "cfa-stack.h"
    33
    4 forall( otype T ) struct stack_node {
    5         T value;
    6         stack_node(T) * next;
    7 };
     4forall( otype T ) {
     5        struct node {
     6                T value;
     7                node(T) * next;
     8        };
    89
    9 forall( otype T ) void clear( stack(T) & s ) with( s ) {
    10         for ( stack_node(T) * next = head; next; ) {
    11                 stack_node(T) * crnt = next;
    12                 next = crnt->next;
    13                 ^(*crnt){};
    14                 free(crnt);
     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;
    1520        }
    16         head = 0;
     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        }
    1757}
    18 
    19 forall( otype T ) void ?{}( stack(T) & s ) { (s.head){ 0 }; }
    20 
    21 forall( otype T ) void ?{}( stack(T) & s, stack(T) t ) {
    22         stack_node(T) ** crnt = &s.head;
    23         for ( stack_node(T) * next = t.head; next; next = next->next ) {
    24                 *crnt = alloc();
    25                 ((*crnt)->value){ next->value };
    26                 crnt = &(*crnt)->next;
    27         }
    28         *crnt = 0;
    29 }
    30 
    31 forall( otype T ) stack(T) ?=?( stack(T) & s, stack(T) t ) {
    32         if ( s.head == t.head ) return s;
    33         clear( s );
    34         s{ t };
    35         return s;
    36 }
    37 
    38 forall( otype T ) void ^?{}( stack(T) & s) { clear( s ); }
    39 
    40 forall( otype T ) _Bool empty( const stack(T) & s ) { return s.head == 0; }
    41 
    42 forall( otype T ) void push( stack(T) & s, T value ) with( s ) {
    43         stack_node(T) * n = alloc();
    44         (*n){ value, head };
    45         head = n;
    46 }
    47 
    48 forall( otype T ) T pop( stack(T) & s ) with( s ) {
    49         stack_node(T) * n = head;
    50         head = n->next;
    51         T v = n->value;
    52         ^(*n){};
    53         free( n );
    54         return v;
    55 }
  • doc/papers/general/evaluation/cfa-stack.h

    raa5fdac rac4dad2  
    11#pragma once
    22
    3 forall( otype T ) struct stack_node;
    4 forall( otype T ) struct stack {
    5         stack_node(T) * head;
    6 };
     3forall( otype T ) {
     4        struct node;
     5        struct stack {
     6                node(T) * head;
     7        };
    78
    8 forall( otype T ) void ?{}( stack(T) & s );
    9 forall( otype T ) void ?{}( stack(T) & s, stack(T) t );
    10 forall( otype T ) stack(T) ?=?( stack(T) & s, stack(T) t );
    11 forall( otype T ) void ^?{}( stack(T) & s);
     9        void ?{}( stack(T) & s );
     10        void ?{}( stack(T) & s, stack(T) t );
     11        void ^?{}( stack(T) & s);
     12        void clear( stack(T) & s );
    1213
    13 forall( otype T ) _Bool empty( const stack(T) & s );
    14 forall( otype T ) void push( stack(T) & s, T value );
    15 forall( otype T ) T pop( stack(T) & s );
    16 forall( otype T ) void clear( stack(T) & s );
     14        stack(T) ?=?( stack(T) & s, stack(T) t );
     15        _Bool empty( const stack(T) & s );
     16        void push( stack(T) & s, T value );
     17        T pop( stack(T) & s );
     18}
  • doc/papers/general/evaluation/cpp-stack.hpp

    raa5fdac rac4dad2  
    1414
    1515        void clear() {
    16                 for ( node * next = head; next; ) {
    17                         node * crnt = next;
    18                         next = crnt->next;
    19                         delete crnt;
     16                for ( node * nx = head; nx; ) {
     17                        node * cr = nx;
     18                        nx = cr->next;
     19                        delete cr;
    2020                }
    2121                head = nullptr;
     
    2323
    2424        void copy( const stack<T> & o ) {
    25                 node ** crnt = &head;
    26                 for ( node * next = o.head; next; next = next->next ) {
    27                         *crnt = new node{ next->value }; /***/
    28                         crnt = &(*crnt)->next;
     25                node ** cr = &head;
     26                for ( node * nx = o.head; nx; nx = nx->next ) {
     27                        *cr = new node{ nx->value }; /***/
     28                        cr = &(*cr)->next;
    2929                }
    30                 *crnt = nullptr;
     30                *cr = nullptr;
    3131        }
    3232
  • doc/papers/general/evaluation/cpp-vstack.cpp

    raa5fdac rac4dad2  
    55
    66void stack::clear() {
    7         for ( node * next = head; next; ) {
    8                 node * crnt = next;
    9                 next = crnt->next;
    10                 delete crnt;
     7        for ( node * nx = head; nx; ) {
     8                node * cr = nx;
     9                nx = cr->next;
     10                delete cr;
    1111        }
    1212        head = nullptr;
     
    1414
    1515void stack::copy( const stack & o ) {
    16         node ** crnt = &head;
    17         for ( node * next = o.head; next; next = next->next ) {
    18                 *crnt = new node{ *next->value }; /***/
    19                 crnt = &(*crnt)->next;
     16        node ** cr = &head;
     17        for ( node * nx = o.head; nx; nx = nx->next ) {
     18                *cr = new node{ *nx->value }; /***/
     19                cr = &(*cr)->next;
    2020        }
    21         *crnt = nullptr;
     21        *cr = nullptr;
    2222}
    2323
Note: See TracChangeset for help on using the changeset viewer.