refactor repl
This commit is contained in:
		
							parent
							
								
									777f951caf
								
							
						
					
					
						commit
						2cdcbb150a
					
				
							
								
								
									
										51
									
								
								tools/main.c
								
								
								
								
							
							
						
						
									
										51
									
								
								tools/main.c
								
								
								
								
							| 
						 | 
					@ -54,9 +54,7 @@ repl(pic_state *pic)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  char code[CODE_MAX_LENGTH] = "", line[LINE_MAX_LENGTH];
 | 
					  char code[CODE_MAX_LENGTH] = "", line[LINE_MAX_LENGTH];
 | 
				
			||||||
  char *prompt;
 | 
					  char *prompt;
 | 
				
			||||||
  struct pic_proc *proc;
 | 
					 | 
				
			||||||
  pic_value v, exprs;
 | 
					  pic_value v, exprs;
 | 
				
			||||||
  jmp_buf jmp;
 | 
					 | 
				
			||||||
  int ai;
 | 
					  int ai;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if PIC_ENABLE_READLINE
 | 
					#if PIC_ENABLE_READLINE
 | 
				
			||||||
| 
						 | 
					@ -103,54 +101,33 @@ repl(pic_state *pic)
 | 
				
			||||||
      goto overflow;
 | 
					      goto overflow;
 | 
				
			||||||
    strcat(code, line);
 | 
					    strcat(code, line);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (setjmp(jmp) == 0) {
 | 
					    pic_try {
 | 
				
			||||||
      pic->jmp = &jmp;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    else {
 | 
					 | 
				
			||||||
      /* error occured */
 | 
					 | 
				
			||||||
      printf("%s\n", pic_errmsg(pic));
 | 
					 | 
				
			||||||
      code[0] = '\0';
 | 
					 | 
				
			||||||
      pic->err = NULL;
 | 
					 | 
				
			||||||
      goto next;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      /* read */
 | 
					      /* read */
 | 
				
			||||||
      exprs = pic_read_cstr(pic, code);
 | 
					      exprs = pic_read_cstr(pic, code);
 | 
				
			||||||
    if (pic_undef_p(exprs)) {   /* wait for more input */
 | 
					
 | 
				
			||||||
      goto next;
 | 
					      if (pic_undef_p(exprs)) {
 | 
				
			||||||
 | 
					        /* wait for more input */
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					      else {
 | 
				
			||||||
        code[0] = '\0';
 | 
					        code[0] = '\0';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        pic_for_each (v, exprs) {
 | 
					        pic_for_each (v, exprs) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if DEBUG
 | 
					 | 
				
			||||||
      printf("[read: ");
 | 
					 | 
				
			||||||
      pic_debug(pic, v);
 | 
					 | 
				
			||||||
      printf("]\n");
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
          /* eval */
 | 
					          /* eval */
 | 
				
			||||||
      proc = pic_compile(pic, v);
 | 
					          v = pic_eval(pic, v);
 | 
				
			||||||
      if (proc == NULL) {
 | 
					 | 
				
			||||||
	printf("compilation error: %s\n", pic_errmsg(pic));
 | 
					 | 
				
			||||||
	pic->err = NULL;
 | 
					 | 
				
			||||||
	goto next;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      v = pic_apply(pic, proc, pic_nil_value());
 | 
					 | 
				
			||||||
      if (pic_undef_p(v)) {
 | 
					 | 
				
			||||||
	printf("runtime error: %s\n", pic_errmsg(pic));
 | 
					 | 
				
			||||||
	pic->err = NULL;
 | 
					 | 
				
			||||||
	goto next;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
          /* print */
 | 
					          /* print */
 | 
				
			||||||
      printf("=> "); fflush(stdout);
 | 
					          pic_printf(pic, "=> ~s\n", v);
 | 
				
			||||||
      pic_debug(pic, v);
 | 
					        }
 | 
				
			||||||
      printf("\n"); fflush(stdout);
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    pic_catch {
 | 
				
			||||||
 | 
					      printf("error: %s\n", pic_errmsg(pic));
 | 
				
			||||||
 | 
					      pic->err = NULL;
 | 
				
			||||||
 | 
					      code[0] = '\0';
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  next:
 | 
					 | 
				
			||||||
    pic_gc_arena_restore(pic, ai);
 | 
					    pic_gc_arena_restore(pic, ai);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue