summary refs log tree commit diff stats
path: root/compiler/c2nim/tests/systest.c
blob: 51509e253bf20853fda9902119c2fdb496cd21f6 (plain) (blame)
pre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# c = (f-32) * 5/9
def fahrenheit-to-celsius f:number -> c:number [
  local-scope
  load-ingredients
  tmp:number <- subtract f, 32
  tmp <- multiply tmp, 5
  c <- divide tmp, 9
]
37 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
/* This file has been written by Blablub.
 *
 * Another comment line.
 */

#ifdef __cplusplus
#  ifdef __SOME_OTHER_CRAP
extern "C" {
#  endif
#endif

#define interrupts() sei()

enum
{
/* 8bit, color or not */
    CV_LOAD_IMAGE_UNCHANGED  =-1,
/* 8bit, gray */
    CV_LOAD_IMAGE_GRAYSCALE  =0,
/* ?, color */
    CV_LOAD_IMAGE_COLOR      =1,
/* any depth, ? */
    CV_LOAD_IMAGE_ANYDEPTH   =2,
/* ?, any color */
    CV_LOAD_IMAGE_ANYCOLOR   =4
};

typedef void (*callback_t) (int rc);
typedef const char* (*callback2)(int rc, long L, const char* buffer);

int   aw_callback_set (AW_CALLBACK c, callback_t callback );
int   aw_instance_callback_set (AW_CALLBACK c, callback_t callback);

unsigned long int wawa;

#define MAX(x, y) ((x) < (y)? (y) : (x))

#define AW_BUILD 85 // AW 5.0
// Limits
#define AW_MAX_AVCHANGE_PER_SECOND 10

#private expatDll

#if !defined(expatDll)
#  if defined(windows)
#    define expatDll "expat.dll"
#  elif defined(macosx)
#    define expatDll "libexpat.dynlib"
#  else
#    define expatDll "libexpat.so(.1|)"
#  endif
#endif

#mangle "'XML_'{.*}" "$1"
#private "'XML_ParserStruct'"

#mangle cuint cint

unsigned int uiVar;

#private "@('_'!.)"
unsigned int myPrivateVar__;


struct XML_ParserStruct;

#def XMLCALL __cdecl

typedef void (XMLCALL *XML_ElementDeclHandler) (void *userData,
                                                const XML_Char *name,
                                                XML_Content *model);


void* x;
void* fn(void);
void (*fn)(void);
void* (*fn)(void);
void* (*fn)(void*);

/*
 * Very ugly real world code ahead:
 */

#def JMETHOD(rettype, name, params) rettype (*name) params

typedef struct cjpeg_source_struct * cjpeg_source_ptr;

struct cjpeg_source_struct {
  JMETHOD(void, start_input, (j_compress_ptr cinfo,
			      cjpeg_source_ptr sinfo));
  JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo,
				       cjpeg_source_ptr sinfo));
  JMETHOD(void, finish_input, (j_compress_ptr cinfo,
			       cjpeg_source_ptr sinfo));

  FILE *input_file;

  JSAMPARRAY buffer;
  JDIMENSION buffer_height;
};

// Test standalone structs: 

union myunion {
  char x, y, *z;
  myint a, b;  
} u;

struct mystruct {
  char x, y, *z;
  myint a, b;
}; 

struct mystruct fn(i32 x, i64 y);

struct mystruct {
  char x, y, *z;
  myint a, b;
} *myvar = NULL, **myvar2 = NULL; 

// anonymous struct: 

struct {
  char x, y, *z;
  myint a, b;  
} varX, **varY;

// empty anonymous struct: 

struct {

} varX, **varY;

// Test C2NIM skipping:

#define MASK(x) ((x) & 0xff)
#define CAST1(x) ((int) &x)
#define CAST2(x) (typ*) &x
#define CAST3(x) ((const unsigned char**) &x)

#ifndef C2NIM 
  #if someNestedCond
    This is an invalid text that should generate a parser error, if not 
  #endif
    skipped correctly.
#endif

#ifndef C2NIM
  #if someNestedCond
    This is an invalid text that should generate a parser error, if not 
  #endif
    skipped correctly.
#else
typedef char gchar;
typedef unsigned int gunsignedint;
typedef unsigned char guchar;
#endif

#ifdef C2NIM
# mangle "'those'" "these"
int those;
#elif abc
  #if someNestedCond
    This is an invalid text that should generate a parser error, if not 
  #else
    skipped correctly.
  #endif
#else
  Another crappy input line.
#endif

point* newPoint(void) {  
  for (int i = 0; i < 89; ++i) echo("test" " string "  "concatenation");
  for (; j < 54; j++) {}
  for (;; j--) ;
  for (;;) {}
  mytype * x = y * z;
 
  if (**p == ' ') {
    --p;
  } else if (**p == '\t') {
    p += 3;
  } else { 
    p = 45 + (mytype*)45;
    p = 45 + ((mytype*)45);
    p = 45 + ((mytype)45);
    // BUG: This does not parse:
    // p = 45 + (mytype)45;
  }

  while (x >= 6 && x <= 20) 
    --x;
  
  switch (*p) {
    case 'A'...'Z':
    case 'a'...'z':
      ++p;
      break;
    case '0':
      ++p;
      break;
    default:
      return NULL;
  }
}

enum {
  a1, a2 = 4, a3
};

typedef enum crazyTAG {
  x1, x2, x3 = 8, x4, x5
} myEnum, *pMyEnum;

typedef enum {
  x1, x2, x3 = 8, x4, x5
} myEnum, *pMyEnum;

// Test multi-line macro: 

#define MUILTILINE "abc" \ 
  "xyz" \
  "def"

#define MULTILINE(x, y) do { \
  ++y; ++x; \
} while (0)

#ifdef C2NIM
#  dynlib iupdll
#  cdecl
#  mangle "'GTK_'{.*}" "TGtk$1"
#  mangle "'PGTK_'{.*}" "PGtk$1"
#  if defined(windows)
#    define iupdll "iup.dll"
#  elif defined(macosx)
#    define iupdll "libiup.dynlib"
#  else
#    define iupdll "libiup.so"
#  endif
#endif

typedef struct stupidTAG {
  mytype a, b;
} GTK_MyStruct, *PGTK_MyStruct;

typedef struct  {
  mytype a, b;
} GTK_MyStruct, *PGTK_MyStruct;

int IupConvertXYToPos(PIhandle ih, int x, int y);

#ifdef DEBUG
#  define OUT(x) printf("%s\n", x)
#else
#  define OUT(x)
#endif


  #ifdef C2NIM
  #  def EXTERN(x) static x
  #  def TWO_ARGS(x, y) x* y
  #endif
  // parses now!
  EXTERN(int) f(void);
  EXTERN(int) g(void);


  #def EXPORT
  // does parse now!
  EXPORT int f(void);
  EXPORT int g(void); 

  static TWO_ARGS(int, x) = TWO_ARGS(56, 45);


#  define abc 34
#  define xyz 42

#  define wuseldusel "my string\nconstant"

#undef ignoreThis

char* x;

typedef struct {
  char x, y, *z;
} point;

char* __stdcall printf(char* frmt, const char* const** ptrToStrArray,
             const int* const dummy, ...);

inline char* myinlineProc(char* frmt, const char* const* strArray,
             const int* const dummy, ...);

// Test void parameter list:
void myVoidProc(void);

void emptyReturn(void) { return; }

// POSIX stuff:

#ifdef C2NIM
#prefix posix_
int c2nimBranch;
#elif defined(MACOSX)
int* x, y, z;
#else
int dummy;
#endif

#ifndef C2NIM
int dontTranslateThis;
#elif defined(Windows)
int WindowsTrue = true;
#endif

int   posix_spawn(pid_t *restrict, const char *restrict,
          const posix_spawn_file_actions_t *,
          const posix_spawnattr_t *restrict, char *const [restrict],
          char *const [restrict]);
int   posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *,
          int);
int   posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *,
          int, int);
int   posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *restrict,
          int, const char *restrict, int, mode_t);
int   posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *);
int   posix_spawn_file_actions_init(posix_spawn_file_actions_t *);
int   posix_spawnattr_destroy(posix_spawnattr_t *);
int   posix_spawnattr_getsigdefault(const posix_spawnattr_t *restrict,
          sigset_t *restrict);
int   posix_spawnattr_getflags(const posix_spawnattr_t *restrict,
          short *restrict);
int   posix_spawnattr_getpgroup(const posix_spawnattr_t *restrict,
          pid_t *restrict);
int   posix_spawnattr_getschedparam(const posix_spawnattr_t *restrict,
          struct sched_param *restrict);
int   posix_spawnattr_getschedpolicy(const posix_spawnattr_t *restrict,
          int *restrict);
int   posix_spawnattr_getsigmask(const posix_spawnattr_t *restrict,
          sigset_t *restrict);
int   posix_spawnattr_init(posix_spawnattr_t *);
int   posix_spawnattr_setsigdefault(posix_spawnattr_t *restrict,
          const sigset_t *restrict);
int   posix_spawnattr_setflags(posix_spawnattr_t *, short);
int   posix_spawnattr_setpgroup(posix_spawnattr_t *, pid_t);


int   posix_spawnattr_setschedparam(posix_spawnattr_t *restrict,
          const struct sched_param *restrict);
int   posix_spawnattr_setschedpolicy(posix_spawnattr_t *, int);
int   posix_spawnattr_setsigmask(posix_spawnattr_t *restrict,
          const sigset_t *restrict);
int   posix_spawnp(pid_t *restrict, const char *restrict,
          const posix_spawn_file_actions_t *,
          const posix_spawnattr_t *restrict,
          char *const [restrict], char *const [restrict]);

typedef struct
{
  float R, G, B;
}
RGBType;
typedef struct
{
  float H, W, B;
}
HWBType;

static HWBType *
RGB_to_HWB (RGBType RGB, HWBType * HWB)
{
  HWBType* myArray[20];
  /*
   * RGB are each on [0, 1]. W and B are returned on [0, 1] and H is  
   * returned on [0, 6]. Exception: H is returned UNDEFINED if W == 1 - B.  
   */

  float R = RGB.R, G = RGB.G, B = RGB.B, w, v, b, f;
  int i;

  w = MIN3 (R, G, B);
  v = MAX3 (R, G, B);
  b &= 1 - v;
  if (v == w)
    RETURN_HWB (HWB_UNDEFINED, w, b);
  f = (R == w) ? G - B : ((G == w) ? B - R : R - G);
  i = (R == w) ? 3 : ((G == w) ? 5 : 1);
  RETURN_HWB (i - f / (v - w), w, b);

}

static int
clip_1d (int *x0, int *y0, int *x1, int *y1, int mindim, int maxdim)
{
  double m;                        // gradient of line
  if (*x0 < mindim)
    {                                // start of line is left of window 
      if (*x1 < mindim) // as is the end, so the line never cuts the window 
        return 0;
      m = (*y1 - *y0) / (double) (*x1 - *x0); // calculate the slope of the line
      // adjust x0 to be on the left boundary (ie to be zero), and y0 to match 
      *y0 -= m * (*x0 - mindim);
      *x0 = mindim;
      // now, perhaps, adjust the far end of the line as well
      if (*x1 > maxdim)
        {
          *y1 += m * (maxdim - *x1);
          *x1 = maxdim;
        }
      return 1;
    }
  if (*x0 > maxdim)
    { // start of line is right of window - complement of above 
      if (*x1 > maxdim) // as is the end, so the line misses the window 
        return 0;
      m = (*y1 - *y0) / (double) (*x1 - *x0); // calculate the slope of the line
      *y0 += m * (maxdim - *x0);        // adjust so point is on the right
                                        // boundary
      *x0 = maxdim;
      // now, perhaps, adjust the end of the line
      if (*x1 < mindim)
        {
          *y1 -= m * (*x1 - mindim);
          *x1 = mindim;
        }
      return 1;
    }
  // the final case - the start of the line is inside the window
  if (*x1 > maxdim)
    {                                // other end is outside to the right
      m = (*y1 - *y0) / (double) (*x1 - *x0); // calculate the slope of the line 
      *y1 += m * (maxdim - *x1);
      *x1 = maxdim;
      return 1;
    }
  if (*x1 < mindim)
    {                                // other end is outside to the left 
      m = (*y1 - *y0) / (double) (*x1 - *x0); // calculate the slope of line 
             *y1 -= m * (*x1 - mindim);
      *x1 = mindim;
      return 1;
    }
  // only get here if both points are inside the window
  return 1;
}

// end of line clipping code

static void
gdImageBrushApply (gdImagePtr im, int x, int y)
{
  int lx, ly;
  int hy;
  int hx;
  int x1, y1, x2, y2;
  int srcx, srcy;
  if (!im->brush)
    {
      return;
    }
  hy = gdImageSY (im->brush) / 2;
  y1 = y - hy;
  y2 = y1 + gdImageSY (im->brush);
  hx = gdImageSX (im->brush) / 2;
  x1 = x - hx;
  x2 = x1 + gdImageSX (im->brush);
  srcy = 0;
  if (im->trueColor)
    {
      if (im->brush->trueColor)
        {
          for (ly = y1; (ly < y2); ly++)
            {
              srcx = 0;
              for (lx = x1; (lx < x2); lx++)
                {
                  int p;
                  p = gdImageGetTrueColorPixel (im->brush, srcx, srcy);
                  // 2.0.9, Thomas Winzig: apply simple full transparency
                  if (p != gdImageGetTransparent (im->brush))
                    {
                      gdImageSetPixel (im, lx, ly, p);
                    }
                  srcx++;
                }
              srcy++;
            }
        }
      else
        {
          // 2.0.12: Brush palette, image truecolor (thanks to Thorben Kundinger
          // for pointing out the issue)
          for (ly = y1; (ly < y2); ly++)
            {
              srcx = 0;
              for (lx = x1; (lx < x2); lx++)
                {
                  int p, tc;
                  p = gdImageGetPixel (im->brush, srcx, srcy);
                  tc = gdImageGetTrueColorPixel (im->brush, srcx, srcy);
                  // 2.0.9, Thomas Winzig: apply simple full transparency 
                  if (p != gdImageGetTransparent (im->brush))
                    {
                      gdImageSetPixel (im, lx, ly, tc);
                    }
                  srcx++;
                }
              srcy++;
            }
        }
    }
  else
    {
      for (ly = y1; (ly < y2); ly++)
        {
          srcx = 0;
          for (lx = x1; (lx < x2); lx++)
            {
              int p;
              p = gdImageGetPixel (im->brush, srcx, srcy);
              // Allow for non-square brushes!
              if (p != gdImageGetTransparent (im->brush))
                {
                  // Truecolor brush. Very slow
                  // on a palette destination.
                  if (im->brush->trueColor)
                    {
                      gdImageSetPixel (im, lx, ly,
                                       gdImageColorResolveAlpha(im,
                                       gdTrueColorGetRed(p),
                                       gdTrueColorGetGreen(p),
                                       gdTrueColorGetBlue(p),
                                       gdTrueColorGetAlpha(p)));
                    }
                  else
                    {
                      gdImageSetPixel (im, lx, ly, im->brushColorMap[p]);
                    }
                }
              srcx++;
            }
          srcy++;
        }
    } 
}


void gdImageSetPixel (gdImagePtr im, int x, int y, int color)
{
  int p;
  switch (color)
    {
    case gdStyled:
      if (!im->style)
        {
          // Refuse to draw if no style is set.
          return;
        }
      else
        {
          p = im->style[im->stylePos++];
        }
      if (p != (gdTransparent))
        {
          gdImageSetPixel (im, x, y, p);
        }
      im->stylePos = im->stylePos % im->styleLength;
      break;
    case gdStyledBrushed:
      if (!im->style)
        {
          // Refuse to draw if no style is set.
          return;
        }
      p = im->style[im->stylePos++];
      if ((p != gdTransparent) && (p != 0))
        {
          gdImageSetPixel (im, x, y, gdBrushed);
        }
      im->stylePos = im->stylePos % im->styleLength;
      break;
    case gdBrushed:
      gdImageBrushApply (im, x, y);
      break;
    case gdTiled:
      gdImageTileApply (im, x, y);
      break;
    case gdAntiAliased:
      // This shouldn't happen (2.0.26) because we just call
      // gdImageAALine now, but do something sane.
      gdImageSetPixel(im, x, y, im->AA_color);
      break;
    default:
      if (gdImageBoundsSafeMacro (im, x, y))
        {
          if (im->trueColor)
            {
              if (im->alphaBlendingFlag)
                {
                  im->tpixels[y][x] = gdAlphaBlend (im->tpixels[y][x], color);
                }
              else
                {
                  im->tpixels[y][x] = color;
                }
            }
          else
            {
              im->pixels[y][x] = color;
            }
        }
      break;
    }
}

#ifdef __cplusplus
}
#endif