/[cvs]/stack/stack.c
ViewVC logotype

Diff of /stack/stack.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.19 by masse, Sat Feb 2 14:50:44 2002 UTC revision 1.23 by masse, Sat Feb 2 18:36:19 2002 UTC
# Line 160  int push_ref(stackitem** stack_head, has Line 160  int push_ref(stackitem** stack_head, has
160      else      else
161        def_sym(in_hash, in_string); /* Make symbol */        def_sym(in_hash, in_string); /* Make symbol */
162                
163      new_item->content.ptr= *hash(in_hash, in_string); /* XXX */      new_item->content.ptr= *hash(in_hash, in_string); /* The new reference
164                                                             shouldn't point at
165                                                             NULL */
166      new_item->type= ref;      new_item->type= ref;
167    }    }
168    
# Line 196  extern void nl() Line 198  extern void nl()
198  }  }
199    
200  /* Prints the top element of the stack. */  /* Prints the top element of the stack. */
201  void print_(stackitem** stack_head)  extern void print_(stackitem** stack_head)
202  {  {
203    if((*stack_head)==NULL) {    stackitem* temp= *stack_head;
204    
205      if(temp==NULL) {
206      printerr("Stack empty");      printerr("Stack empty");
207      return;      return;
208    }    }
209    
210    switch((*stack_head)->type) {    while(temp->type==ref)
211        temp= temp->content.ptr;
212    
213      switch(temp->type) {
214    case value:    case value:
215      printf("%d", (*stack_head)->content.val);      printf("%d", temp->content.val);
216      break;      break;
217    case string:    case string:
218      printf("%s", (char*)(*stack_head)->content.ptr);      printf("\"%s\"", (char*)temp->content.ptr);
     break;  
   case ref:  
     printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);  
219      break;      break;
220    case symbol:    case symbol:
221        printf("%s", temp->id);
222        break;
223    default:    default:
224      printf("%p", (*stack_head)->content.ptr);      printf("%p", temp->content.ptr);
225      break;      break;
226    }    }
227  }  }
# Line 243  extern void printstack(stackitem** stack Line 249  extern void printstack(stackitem** stack
249  {  {
250    if(*stack_head != NULL) {    if(*stack_head != NULL) {
251      print_st(*stack_head, 1);      print_st(*stack_head, 1);
252      printf("\n");      nl();
253    } else {    } else {
254      printerr("Stack empty");      printerr("Stack empty");
255    }    }
# Line 254  extern void printstack(stackitem** stack Line 260  extern void printstack(stackitem** stack
260  extern void eval(stackitem** stack_head)  extern void eval(stackitem** stack_head)
261  {  {
262    funcp in_func;    funcp in_func;
263      stackitem* temp= *stack_head;
264    
265    if((*stack_head)==NULL || (*stack_head)->type!=ref) {    if(temp==NULL) {
266      printerr("Stack empty or not a reference");      printerr("Stack empty");
267      return;      return;
268    }    }
269    
270    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {    while(temp->type==ref)
271      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;      temp= temp->content.ptr;
272    
273      if(temp->type==func) {
274        in_func= (funcp)(temp->content.ptr);
275      toss(stack_head);      toss(stack_head);
276      (*in_func)(stack_head);      (*in_func)(stack_head);
277      return;      return;
278    } else    }
279      printerr("Not a function");      
280      printerr("Couldn't evaluate");
281  }  }
282    
283  /* Make a list. */  /* Make a list. */
# Line 312  int stack_read(stackitem** stack_head, h Line 323  int stack_read(stackitem** stack_head, h
323    int itemp;    int itemp;
324    size_t inlength= strlen(in_line)+1;    size_t inlength= strlen(in_line)+1;
325    int convert= 0;    int convert= 0;
326      static int non_eval_flag= 0;
327    
328    temp= malloc(inlength);    temp= malloc(inlength);
329    rest= malloc(inlength);    rest= malloc(inlength);
# Line 334  int stack_read(stackitem** stack_head, h Line 346  int stack_read(stackitem** stack_head, h
346        break;        break;
347      }      }
348      /* If symbol */      /* If symbol */
349      if((convert= sscanf(in_line, "%[^] ;\n\r]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
350          push_ref(stack_head, in_hash, temp);          push_ref(stack_head, in_hash, temp);
351          break;          break;
352      }      }
353      /* If single char */      /* If single char */
354      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
355        if(*temp==';') {        if(*temp==';') {
356          eval(stack_head);               /* Evaluate top element */          if(!non_eval_flag) {
357              eval(stack_head);             /* Evaluate top element */
358              break;
359            }
360            
361            push_ref(stack_head, in_hash, ";");
362          break;          break;
363        }        }
364    
365        if(*temp==']') {        if(*temp==']') {
366          push_ref(stack_head, in_hash, "[");          push_ref(stack_head, in_hash, "[");
367          pack(stack_head);          pack(stack_head);
368            if(non_eval_flag!=0)
369              non_eval_flag--;
370            break;
371          }
372    
373          if(*temp=='[') {
374            push_ref(stack_head, in_hash, "[");
375            non_eval_flag++;
376          break;          break;
377        }        }
378      }      }

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.23

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26