Beispiel #1
0
static void ContextDelete(Context* ctx) {
  if(!ctx) {
    return;
  }

  Object* current = ctx->lastObject;
  while(current) {
    Object* next = current->next;
    ObjectDelete(ctx, current);
    free(current);
    current = next;
  }

  EnvironmentDelete(ctx->environment);
  StackDelete(ctx->stack);
  ReaderDelete(ctx->reader);
  free(ctx);
}
int main() {
	int i = 0;
	Stack * stack = SCreate();
	while (i < 5){
		int * temp = malloc(sizeof(int));
		* temp = i;
		push(stack,(void *)temp);
		printf("Data : %d\n",*(int * )peek(stack));
		i++;
	}
	while (isEmpty(stack )!= 0){
		int * temp = pop(stack);
		printf("Data : %d",*temp);
		free(temp);
	}
	StackDelete(stack);
	return 0;
}
Beispiel #3
0
static Context* ContextNew(Runtime* rt, StreamType inputType, const char* strOrFileName) {
  Context* ctx = (Context*)malloc(sizeof(Context));
  if(!ctx) {
    return NULL;
  }
  ctx->runtime = rt;
  ctx->lastObject = NULL;
  ctx->stack = NULL;

  ctx->environment = EnvironmentNew(rt->environment);
  if(!ctx->environment) {
    goto cleanup;
  }

  ctx->stack = StackNew();
  if(!ctx->stack) {
    goto cleanup;
  }

  ctx->reader = ReaderNew(inputType, strOrFileName);
  if(!ctx->reader) {
    goto cleanup;
  }

  goto end;

 cleanup:
  if(ctx) {
    EnvironmentDelete(ctx->environment);
    StackDelete(ctx->stack);
    ctx = NULL;
  }

 end:
  return ctx;
}