All pastes #956093 Raw Edit

Someone

public text v1 · immutable
#956093 ·published 2008-03-25 00:52 UTC
rendered paste body
/* lisp.c */                                                                                                                                                                                                        
#include <stdlib.h>                                                                                                                                                                                                 
#include <ctype.h>                                                                                                                                                                                                  
#include "lisp.h"                                                                                                                                                                                                   
                                                                                                                                                                                                                    
struct exp* mk_simp( char* s )  /* makes a tree consisting of 1 L_NODE and many B_NODE */                                                                                                                           
{                                                                                                                                                                                                                   
    char temp[12];              /* temporary buffer */                                                                                                                                                              
    int c;                                                                                                                                                                                                          
    struct exp* ret,                                                                                                                                                                                                
        * tret,                 /*  */                                                                                                                                                                              
        template;               /* used to malloc */                                                                                                                                                                
    init_exp( &template );                                                                                                                                                                                          
    ret = malloc( ( sizeof template ) * 2 );                                                                                                                                                                        
    *ret = template;                                                                                                                                                                                                
    for ( c = 0, s++; ; s++ )                                                                                                                                                                                       
        if ( isspace( *s ) || *s == ')' )                                                                                                                                                                           
        {                                                                                                                                                                                                           
            temp[c] = '\0';                                                                                                                                                                                         
            c = 0;                                                                                                                                                                                                  
            tret = malloc( ( sizeof template ) * 2 );                                                                                                                                                               
            *tret = template;                                                                                                                                                                                       
            set_vexp( tret, temp );                                                                                                                                                                                 
            add_node_exp( ret, tret );                                                                                                                                                                              
            if ( *s == ')' )                                                                                                                                                                                        
                break;                                                                                                                                                                                              
        }                                                                                                                                                                                                           
        else                                                                                                                                                                                                        
            temp[c++] = *s;                                                                                                                                                                                         
    return ret;                                                                                                                                                                                                     
}                                                                                                                                                                                                                   
int issimp( char* s )                                                                                                                                                                                               
{                                                                                                                                                                                                                   
    int n_open, n_close;                                                                                                                                                                                            
    n_open = n_close = 0;                                                                                                                                                                                           
    for ( ; *s != '\0'; s++ )                                                                                                                                                                                       
        if ( *s == '(' )                                                                                                                                                                                            
            n_open++;                                                                                                                                                                                               
        else if ( *s == ')' )                                                                                                                                                                                       
            n_close++;                                                                                                                                                                                              
    if ( n_open != n_close )    /* syntax error */                                                                                                                                                                  
        return 2;                                                                                                                                                                                                   
    else if ( n_open - 1 )      /* not simplified  */                                                                                                                                                               
        return 1;                                                                                                                                                                                                   
    else                        /* simplified  */                                                                                                                                                                   
        return 0;                                                                                                                                                                                                   
                                                                                                                                                                                                                    
}