#include "cdefs.h"
#include "mem.h"
extern cptr object_methods[meth_max];
extern cptr list_methods[meth_max];
extern cptr slist_methods[meth_max];
extern cptr set_methods[meth_max];
extern cptr sset_methods[meth_max];
extern cptr string_methods[meth_max];
extern cptr sstring_methods[meth_max];
#include "cstring.h"
#include "cint.h"
extern cptr clib_methods[meth_max];
#include "cfile.h"
extern cptr strpool_methods[meth_max];
extern cptr stdout_methods[meth_max];

extern cint object_Undefined;
extern cint stdout_Curr_item;
extern cint clib_stdi;
extern cint stdout_Curr_col;

cint stdin_ch;
cint stdin_last_item_was_quoted_string;

cptr stdin_methods[meth_max];

cint stdin_get_rest_of_line(){
  cint stdin_result;
  cint t1, t2, t3, t4, t5, t6, t7, t8;
  stdout_Curr_item = 0;
  stdout_Curr_col = 1;
  stdin_last_item_was_quoted_string = 0;
  t2 = stdin_ch == 13;
  if (t2){
    stdin_ch = cfile_fgetc((cint)clib_stdi);
  };
  t3 = stdin_ch == 10;
  if (t3){
    stdin_ch = cfile_fgetc((cint)clib_stdi);
  };
  stdin_skip_delimiters();
  t4 = stdin_ch == -1;
  if (t4){
    stdin_ch = 10;
    t1 = object_Undefined;
    goto t1_label;
  };
  t5 = stdin_ch == 91;
  if (t5){
    t1 = stdin_get_list();
    goto t1_label;
  };
  t6 = stdin_ch == 96;
  if (t6){
    t1 = stdin_get_string();
    goto t1_label;
  };
  t7 = stdin_ch == 123;
  if (t7){
    t1 = stdin_get_set();
    goto t1_label;
  };
  t8 = stdin_get_string_upto((cint)10, (cint)0);
  t1 = t8;
  goto t1_label;
  t1_label: return t1;
}

cint stdin_get_item(){
  cint stdin_result;
  cint t9, t10, t11, t12, t13, t14, t15, t16;
  stdout_Curr_item = 0;
  stdout_Curr_col = 1;
  stdin_last_item_was_quoted_string = 0;
  t10 = stdin_ch == 13;
  if (t10){
    stdin_ch = cfile_fgetc((cint)clib_stdi);
  };
  t11 = stdin_ch == 10;
  if (t11){
    stdin_ch = cfile_fgetc((cint)clib_stdi);
  };
  stdin_skip_delimiters();
  t12 = stdin_ch == -1;
  if (t12){
    stdin_ch = 10;
    t9 = object_Undefined;
    goto t9_label;
  };
  t13 = stdin_ch == 91;
  if (t13){
    t9 = stdin_get_list();
    goto t9_label;
  };
  t14 = stdin_ch == 96;
  if (t14){
    t9 = stdin_get_string();
    goto t9_label;
  };
  t15 = stdin_ch == 123;
  if (t15){
    t9 = stdin_get_set();
    goto t9_label;
  };
  t16 = stdin_get_string_upto((cint)0, (cint)0);
  t9 = t16;
  goto t9_label;
  t9_label: return t9;
}

cint stdin_get_nitem(){
  cint stdin_result, stdin_item, stdin_num;
  cint t17, t18, t19, t20, t21, t22;
  stdin_item = stdin_get_item();
  t18 = stdin_last_item_was_quoted_string == 1;
  if (t18){
    t17 = stdin_item;
    goto t17_label;
  };
  ((cptr)stdin_item)[1] += 1;
  t19 = ((cptr)(((cptr)stdin_item)[0]))[87];
  t20 = ((f0)t19)((cint)stdin_item); /* Mknum*/
  stdin_num = t20;
  t22 = stdin_num == object_Undefined;
  if (t22){
    t17 = stdin_item;
    goto t17_label;
  };
  object_Dec_ref(stdin_item);
  t17 = stdin_num;
  goto t17_label;
  t17_label: return t17;
}

cint stdin_get_list(){
  cint stdin_result;
  cint t23, t24, t25, t28, t29, t30, t31;
  t24 = mem_malloc(40); ((cptr)t24)[0] = slist_methods;
  t25 = slist_alloc((cint)t24, (cint)0);
  stdin_result = t25;
  stdin_ch = cfile_fgetc((cint)clib_stdi);
  t26:
  t28 = stdin_ch != 93;
  if (!t28) goto t27;
    t29 = ((cptr)(((cptr)stdin_result)[0]))[7];
    t30 = ((f0)t29)((cint)stdin_result, (cint)stdin_get_nitem()); /* Append*/
    stdin_result = t30;
  goto t26;
  t27:;
  stdin_ch = cfile_fgetc((cint)clib_stdi);
  t23 = stdin_result;
  goto t23_label;
  t23_label: return t23;
}

cint stdin_get_set(){
  cint stdin_result;
  cint t32, t33, t34, t37, t38, t39, t40;
  t33 = mem_malloc(48); ((cptr)t33)[0] = sset_methods;
  t34 = sset_alloc((cint)t33);
  stdin_result = t34;
  stdin_ch = cfile_fgetc((cint)clib_stdi);
  t35:
  t37 = stdin_ch != 125;
  if (!t37) goto t36;
    t38 = ((cptr)(((cptr)stdin_result)[0]))[7];
    t39 = ((f0)t38)((cint)stdin_result, (cint)stdin_get_nitem()); /* Append*/
    stdin_result = t39;
  goto t35;
  t36:;
  stdin_ch = cfile_fgetc((cint)clib_stdi);
  t32 = stdin_result;
  goto t32_label;
  t32_label: return t32;
}

cint stdin_get_string(){
  cint stdin_result;
  cint t41, t42;
  stdin_last_item_was_quoted_string = 1;
  stdin_ch = cfile_fgetc((cint)clib_stdi);
  t42 = stdin_get_string_upto((cint)39, (cint)92);
  stdin_result = t42;
  stdin_ch = cfile_fgetc((cint)clib_stdi);
  t41 = stdin_result;
  goto t41_label;
  t41_label: return t41;
}

cint stdin_get_string_upto(cint stdin_terminator_char, cint stdin_escape_char){
  cint stdin_result, stdin_buf, stdin_i;
  cint t43, t46, t47, t48, t49, t50, t51, t52, t53, t54, t55, t56, t57, t58, t59, t60, t61, t62, t63, t64, t65, t66, t67, t68, t69, t70, t71, t72, t73, t74, t75, t76, t77, t78, t79, t80, t81, t82, t83, t84, t85, t86, t87, t88;
  stdin_result = 0;
  stdin_buf = cstring_mem_malloc((cint)64);
  stdin_i = 0;
  t44:
  t46 = stdin_ch != stdin_terminator_char;
  t47 = t46;
  if (t47){
    t48 = stdin_ch != 10;
    t47 = t48;
  };
  t49 = t47;
  if (t49){
    t50 = stdin_ch != 13;
    t49 = t50;
  };
  t51 = t49;
  if (t51){
    t52 = stdin_terminator_char != 0;
    t53 = t52;
    if (!t53){      t54 = stdin_ch != 8;
      t55 = t54;
      if (t55){
        t56 = stdin_ch != 32;
        t55 = t56;
      };
      t57 = t55;
      if (t57){
        t58 = stdin_ch != 44;
        t57 = t58;
      };
      t59 = t57;
      if (t59){
        t60 = stdin_ch != 59;
        t59 = t60;
      };
      t61 = t59;
      if (t61){
        t62 = stdin_ch != 91;
        t61 = t62;
      };
      t63 = t61;
      if (t63){
        t64 = stdin_ch != 93;
        t63 = t64;
      };
      t65 = t63;
      if (t65){
        t66 = stdin_ch != 123;
        t65 = t66;
      };
      t67 = t65;
      if (t67){
        t68 = stdin_ch != 125;
        t67 = t68;
      };
      t53 = t67;
    };
    t51 = t53;
  };
  if (!t51) goto t45;
    t69 = stdin_i >= 63;
    if (t69){
      cstring_update_char((cint)stdin_buf, (cint)63, (cint)0);
      stdin_i = 0;
      t70 = stdin_result == 0;
      if (t70){
        t71 = mem_malloc(40); ((cptr)t71)[0] = sstring_methods;
        t72 = sstring_alloc((cint)t71, (cint)stdin_buf);
        stdin_result = t72;
      }else{
        t73 = mem_malloc(40); ((cptr)t73)[0] = sstring_methods;
        t74 = sstring_alloc((cint)t73, (cint)stdin_buf);
        t75 = ((cptr)(((cptr)stdin_result)[0]))[97];
        t76 = ((f0)t75)((cint)stdin_result, (cint)t74); /* Plus*/
        stdin_result = t76;
      };
      stdin_buf = cstring_mem_malloc((cint)64);
    };
    t78 = stdin_ch == stdin_escape_char;
    if (t78){
      stdin_ch = cfile_fgetc((cint)clib_stdi);
      cstring_update_char((cint)stdin_buf, (cint)stdin_i, (cint)stdin_escape_char);
      t79 = stdin_i + 1;
      stdin_i = t79;
    };
    cstring_update_char((cint)stdin_buf, (cint)stdin_i, (cint)stdin_ch);
    t80 = stdin_i + 1;
    stdin_i = t80;
    stdin_ch = cfile_fgetc((cint)clib_stdi);
  goto t44;
  t45:;
  cstring_update_char((cint)stdin_buf, (cint)stdin_i, (cint)0);
  t81 = stdin_result == 0;
  if (t81){
    t82 = mem_malloc(40); ((cptr)t82)[0] = sstring_methods;
    t83 = sstring_alloc((cint)t82, (cint)stdin_buf);
    stdin_result = t83;
  }else{
    t84 = mem_malloc(40); ((cptr)t84)[0] = sstring_methods;
    t85 = sstring_alloc((cint)t84, (cint)stdin_buf);
    t86 = ((cptr)(((cptr)stdin_result)[0]))[97];
    t87 = ((f0)t86)((cint)stdin_result, (cint)t85); /* Plus*/
    stdin_result = t87;
  };
  t43 = stdin_result;
  goto t43_label;
  t43_label: return t43;
}

cint stdin_skip_delimiters(){
  cint t89, t92, t93, t94, t95, t96, t97, t98;
  t90:
  t92 = stdin_ch == 8;
  t93 = t92;
  if (!t93){    t94 = stdin_ch == 32;
    t93 = t94;
  };
  t95 = t93;
  if (!t95){    t96 = stdin_ch == 44;
    t95 = t96;
  };
  t97 = t95;
  if (!t97){    t98 = stdin_ch == 59;
    t97 = t98;
  };
  if (!t97) goto t91;
    stdin_ch = cfile_fgetc((cint)clib_stdi);
  goto t90;
  t91:;
  t89_label: return t89;
}

void init_stdin(){
  copy_meth_table(stdin_methods, entity_methods);
  stdin_methods[0] = 0;
  stdin_methods[164] = stdin_get_rest_of_line;
  stdin_methods[165] = stdin_get_item;
  stdin_methods[166] = stdin_get_nitem;
  stdin_methods[167] = stdin_get_list;
  stdin_methods[168] = stdin_get_set;
  stdin_methods[169] = stdin_get_string;
  stdin_methods[170] = stdin_get_string_upto;
  stdin_methods[171] = stdin_skip_delimiters;
  stdin_ch = 10;
  stdin_last_item_was_quoted_string = 0;
}
