Пример #1
0
Файл: vm.c Проект: txus/terrorvm
void Value_print_all(STATE, DArray* objs)
{
  for(int i=0; i < DArray_count(objs); i++) {
    Value_print(state, (VALUE)DArray_at(objs, i));
    printf("\n");
  }
}
Пример #2
0
Файл: vm.c Проект: txus/terrorvm
void Stack_print(STATE, Stack* stack)
{
  printf("---STACK (%i)---\n", Stack_count(stack));
  int i = 0;
  STACK_FOREACH(stack, node) {
    if(node) {
      printf("\n%i. ", i);
      Value_print(state, (VALUE)node->value);
    }
    i++;
  }
  printf("\n");
}
Пример #3
0
int main(int argc, char* argv[]) {
	Context* ctx = Context_new();
	register_math(ctx);
	
	for(nextLine(); !feof(stdin); nextLine()) {
		/* Strip trailing newline */
		char* end;
		if((end = strchr(line, '\n')) != NULL) *end = '\0';
		if((end = strchr(line, '\r')) != NULL) *end = '\0';
		if((end = strchr(line, '#')) != NULL) *end = '\0';
		
		const char* p = line;
		
		/* Get verbosity level */
		int verbose = 0;
		while(p[0] == '?') {
			verbose++;
			p++;
		}
		trimSpaces(&p);
		
		if(*p == '~') {
			/* Variable deletion */
			p++;
			
			char* name = nextToken(&p);
			if(name == NULL) {
				/* '~~~' means reset interpreter */
				if(p[0] == '~' && p[1] == '~') {
					/* Wipe out context */
					Context_free(ctx);
					ctx = Context_new();
					register_math(ctx);
					continue;
				}
				
				if(*p == '\0') {
					RAISE(earlyEnd());
					continue;
				}
				
				RAISE(badChar(*p));
				continue;
			}
			
			Context_del(ctx, name);
			
			free(name);
			
			continue;
		}
		
		/* Parse the user's input */
		Expression* expr = Expression_parse(&p);
		
		/* Print expression depending on verbosity */
		Expression_print(expr, ctx, verbose);
		
		/* Error? Go to next loop iteration */
		if(Expression_didError(expr)) {
			Expression_free(expr);
			continue;
		}
		
		/* Evaluate expression */
		Value* result = Expression_eval(expr, ctx);
		Expression_free(expr);
		
		/* Print result */
		Value_print(result, ctx);
		
		Value_free(result);
	}
	
	Context_free(ctx);
	
	return 0;
}