2017年04月08日22時24分26秒に更新されたバージョンを表示しています.
最新のページはこちらをご覧ください.


cLayコード(version 20170408-3)

#include<bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define mygc(c) (c)=getchar_unlocked()
#define mypc(c) putchar_unlocked(c)

#define ll long long
#define ull unsigned ll

void reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
void reader(ll *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
void reader(double *x){scanf("%lf",x);}
int reader(char c[]){int i,s=0;for(;;){mygc(i);if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF) break;}c[s++]=i;for(;;){mygc(i);if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF) break;c[s++]=i;}c[s]='\0';return s;}
template <class T, class S> void reader(T *x, S *y){reader(x);reader(y);}
template <class T, class S, class U> void reader(T *x, S *y, U *z){reader(x);reader(y);reader(z);}
template <class T, class S, class U, class V> void reader(T *x, S *y, U *z, V *w){reader(x);reader(y);reader(z);reader(w);}

void writer(int x, char c){int s=0,m=0;char f[10];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(ll x, char c){int s=0,m=0;char f[20];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(double x, char c){printf("%.15f",x);mypc(c);}
void writer(const char c[]){int i;for(i=0;c[i]!='\0';i++)mypc(c[i]);}
void writer(const char x[], char c){int i;for(i=0;x[i]!='\0';i++)mypc(x[i]);mypc(c);}
template<class T> void writerLn(T x){writer(x,'\n');}
template<class T, class S> void writerLn(T x, S y){writer(x,' ');writer(y,'\n');}
template<class T, class S, class U> void writerLn(T x, S y, U z){writer(x,' ');writer(y,' ');writer(z,'\n');}
template<class T> void writerArr(T x[], int n){int i;if(!n){mypc('\n');return;}rep(i,n-1)writer(x[i],' ');writer(x[n-1],'\n');}

char memarr[17000000]; void *mem = memarr;
#define MD 1000000007



struct insertfunctions{
  vector<string> name;
  std::set<string> doit;
  map<string,string> func;
  map<string,vector<string> > need;

  void set(){
    int i;
    {
      string n = "stdc";
      string c = "#include<bits/stdc++.h>\n";
      vector<string> d;
      
      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    {
      string n = "namespace";
      string c = "using namespace std;\n";
      vector<string> d;
      
      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    {
      string n = "reader_int";
      string c = "void rd(int &x){\n  int k, m=0;\n  x=0;\n  for(;;){\n    k = getchar_unlocked();\n    if(k=='-'){\n      m=1;\n      break;\n    }\n    if('0'<=k&&k<='9'){\n      x=k-'0';\n      break;\n    }\n  }\n  for(;;){\n    k = getchar_unlocked();\n    if(k<'0'||k>'9'){\n      break;\n    }\n    x=x*10+k-'0';\n  }\n  if(m){\n    x=-x;\n  }\n}\n";
      vector<string> d;

      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    {
      string n = "reader_ll";
      string c = "void rd(ll &x){\n  int k, m=0;\n  x=0;\n  for(;;){\n    k = getchar_unlocked();\n    if(k=='-'){\n      m=1;\n      break;\n    }\n    if('0'<=k&&k<='9'){\n      x=k-'0';\n      break;\n    }\n  }\n  for(;;){\n    k = getchar_unlocked();\n    if(k<'0'||k>'9'){\n      break;\n    }\n    x=x*10+k-'0';\n  }\n  if(m){\n    x=-x;\n  }\n}\n";
      vector<string> d;

      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    {
      string n = "reader_char_array";
      string c = "void rd(char c[]){\n  int i, sz = 0;\n  for(;;){\n    i = getchar_unlocked();\n    if(i!=' '&&i!='\\n'&&i!='\\r'&&i!='\\t'&&i!=EOF) break;\n  }\n  c[sz++] = i;\n  for(;;){\n    i = getchar_unlocked();\n    if(i==' '||i=='\\n'||i=='\\r'||i=='\\t'||i==EOF) break;\n    c[sz++] = i;\n  }\n  c[sz]='\\0';\n}";
      vector<string> d;

      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    {
      string n = "writer_int";
      string c = "void wt_L(int x){\n  int s=0, m=0;\n  char f[10];\n  if(x<0) m=1, x=-x;\n  while(x) f[s++]=x%10, x/=10;\n  if(!s) f[s++]=0;\n  if(m) putchar_unlocked('-');\n  while(s--) putchar_unlocked(f[s]+'0');\n}\n";
      vector<string> d;
      
      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    {
      string n = "writer_int_withBase";
      string c = "void wt_L(int x, int b){\n  int s=0, m=0;\n  char f[35];\n  if(x<0) m=1, x=-x;\n  while(x) f[s++]=x%b, x/=b;\n  if(!s) f[s++]=0;\n  if(m) putchar_unlocked('-');\n  while(s--) putchar_unlocked(\"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\"[f[s]]);\n}\n";
      vector<string> d;
      
      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    {
      string n = "writer_ll";
      string c = "void wt_L(ll x){\n  int s=0, m=0;\n  char f[20];\n  if(x<0) m=1, x=-x;\n  while(x) f[s++]=x%10, x/=10;\n  if(!s) f[s++]=0;\n  if(m) putchar_unlocked('-');\n  while(s--) putchar_unlocked(f[s]+'0');\n}\n";
      vector<string> d;
      
      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    {
      string n = "writer_ll_withBase";
      string c = "void wt_L(ll x, int b){\n  int s=0, m=0;\n  char f[70];\n  if(x<0) m=1, x=-x;\n  while(x) f[s++]=x%b, x/=b;\n  if(!s) f[s++]=0;\n  if(m) putchar_unlocked('-');\n  while(s--) putchar_unlocked(\"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\"[f[s]]);\n}\n";
      vector<string> d;
      
      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    {
      string n = "writer_char_array";
      string c = "void wt_L(const char c[]){\n  int i=0;\n  for(i=0;c[i]!='\\0';i++) putchar_unlocked(c[i]);\n}\n";
      vector<string> d;
      
      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    {
      string n = "chmin";
      string c = "template<class S, class T> inline void chmin(S &a, T b){if(a>b)a=b;}";
      vector<string> d;
      
      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    {
      string n = "chmax";
      string c = "template<class S, class T> inline void chmax(S &a, T b){if(a<b)a=b;}";
      vector<string> d;
      
      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }
    
    if(0){
      string n = "";
      string c = "";
      vector<string> d;
      
      name.push_back(n);
      func[n] = c;
      need[n] = d;
    }

    doit.insert((string)"stdc");
    doit.insert((string)"namespace");
  }


  string get_insert_string(){
    int i, fg;
    map<string,vector<string> >::iterator it;
    vector<string> vs;
    string res, tmp;

    for(;;){
      fg = 0;
      for(it=need.begin(); it!=need.end(); it++){
        tmp = it->first;
        vs = it->second;
        if(doit.count(tmp)==0) continue;
        rep(i,vs.size()){
          if(doit.count(vs[i])==0){
            fg++;
            doit.insert(vs[i]);
          }
        }
      }
      if(!fg) break;
    }

    rep(i,name.size()){
      if(doit.count(name[i])){
        res += func[name[i]];
      }
    }

    return res;
  }
};

insertfunctions ifun;
set<string> g_flags;



struct code{
  code *up;

  vector<string> str, strtype;
  vector<int> nxt;
  vector<code*> nxtlst;

  string name, type;
  std::set<string> vartype, tvartype;
  map<string,string> localvar, globalvar, argvar;

  void set_init(){
    vartype.insert((string)"void");
    vartype.insert((string)"char");
    vartype.insert((string)"signed char");
    vartype.insert((string)"unsigned char");
    vartype.insert((string)"int");
    vartype.insert((string)"signed int");
    vartype.insert((string)"unsigned int");
    vartype.insert((string)"signed");
    vartype.insert((string)"unsigned");
    vartype.insert((string)"long");
    vartype.insert((string)"signed long");
    vartype.insert((string)"unsigned long");
    vartype.insert((string)"long long");
    vartype.insert((string)"signed long long");
    vartype.insert((string)"unsigned long long");
    vartype.insert((string)"ll");
    vartype.insert((string)"ull");
    vartype.insert((string)"float");
    vartype.insert((string)"double");
    vartype.insert((string)"bool");
    vartype.insert((string)"string");

    tvartype.insert((string)"pair");
    tvartype.insert((string)"vector");
    tvartype.insert((string)"stack");
    tvartype.insert((string)"queue");
    tvartype.insert((string)"deque");
    tvartype.insert((string)"priority_queue");
    tvartype.insert((string)"set");
    tvartype.insert((string)"map");
    tvartype.insert((string)"unordered_set");
    tvartype.insert((string)"unordered_map");
  }


  void merge(std::set<string> &a, std::set<string> &b){
    std::set<string>::iterator it;
    for(it=b.begin();it!=b.end();it++){
      a.insert(*it);
    }
  }
  void merge(map<string,string> &a, map<string,string> &b){
    map<string,string>::iterator it;
    for(it=b.begin();it!=b.end();it++){
      a[it->first] = it->second;
    }
  }
  void set_next_types(std::set<string> &s_vartype, set<string> &s_tvartype, map<string,string> &s_localvar, map<string,string> &s_globalvar, map<string,string> &s_argvar){
    merge(vartype, s_vartype);
    merge(tvartype, s_tvartype);
    merge(globalvar, s_globalvar);
    merge(globalvar, s_localvar);
    merge(globalvar, s_argvar);
  }

  void setUpnode(code *cc){
    up = cc;
    set_next_types(cc->vartype, cc->tvartype, cc->localvar, cc->globalvar, cc->argvar);
  }


  code* get_root(void){
    code *r = this;
    while(r->up != NULL) r = r->up;
    return r;
  }

  void add_vartype(string in){
    int i;
    vartype.insert(in);
    rep(i,nxtlst.size()) nxtlst[i]->add_vartype(in);
  }

  void add_tvartype(string in){
    int i;
    tvartype.insert(in);
    rep(i,nxtlst.size()) nxtlst[i]->add_tvartype(in);
  }
  
  void add_localvar(string in1, string in2){
    int i;
    localvar[in1] = in2;
    rep(i,nxtlst.size()) nxtlst[i]->add_globalvar(in1, in2);
  }
  
  void add_globalvar(string in1, string in2){
    int i;
    globalvar[in1] = in2;
    rep(i,nxtlst.size()) nxtlst[i]->add_globalvar(in1, in2);
  }
  
  void add_argvar(string in1, string in2){
    int i;
    argvar[in1] = in2;
    rep(i,nxtlst.size()) nxtlst[i]->add_globalvar(in1, in2);
  }


  void ftrim(string &in){
    while(in.size() && isspace(in[0])) in = in.substr(1);
  }
  void etrim(string &in){
    while(in.size() && isspace(in[in.size()-1])) in = in.substr(0, in.size()-1);
  }
  void trim(string &in){
    ftrim(in);
    etrim(in);
  }

  void ftrim_until(string &in, char s){
    while(in.size() && in[0] != s) in = in.substr(1);
  }
  void etrim_until(string &in, char t){
    while(in.size() && in[in.size()-1] != t) in = in.substr(0, in.size()-1);
  }
  void trim_until(string &in, char s, char t){
    ftrim_until(in,s);
    etrim_until(in,t);
  }


  void code_replace(string &in){
    trim(in);
    replaceAll_t(in, "ll", "long long");
    replaceAll_t(in, "ull", "unsigned long long");
    replaceAll_t(in, "int_inf", "1073709056");
    replaceAll_t(in, "ll_inf", "4611686016279904256LL");
    replaceAll_t(in, "double_inf", "1e150");
  }
  

  vector<string> split_p(string in, char c){
    int i;
    int k1 = 0, k2 = 0, k3 = 0, k4 = 0, k5 = 0;
    vector<string> res;
    string tmp;

    rep(i,in.size()){
      if(in[i]=='(') k1++;
      if(in[i]==')') k1--;
      if(in[i]=='[') k2++;
      if(in[i]==']') k2--;
      if(in[i]=='{') k3++;
      if(in[i]=='}') k3--;
      if(in[i]=='\'') k4 ^= 1;
      if(in[i]=='"') k5 ^= 1;
      if(k1==0 && k2==0 && k3==0 && k4==0 && k5==0 && in[i]==c){
        res.push_back(tmp);
        tmp = "";
      } else {
        tmp += in[i];
      }
    }

    res.push_back(tmp);
    return res;
  }
  vector<string> split_p2(string in, char c){
    int i;
    int k1 = 0, k2 = 0, k3 = 0, k4 = 0, k5 = 0, k6 = 0;
    vector<string> res;
    string tmp;

    rep(i,in.size()){
      if(in[i]=='(') k1++;
      if(in[i]==')') k1--;
      if(in[i]=='[') k2++;
      if(in[i]==']') k2--;
      if(in[i]=='{') k3++;
      if(in[i]=='}') k3--;
      if(in[i]=='<') k6++;
      if(in[i]=='>') k6--;
      if(in[i]=='\'') k4 ^= 1;
      if(in[i]=='"') k5 ^= 1;
      if(k1==0 && k2==0 && k3==0 && k4==0 && k5==0 && k6==0 && in[i]==c){
        res.push_back(tmp);
        tmp = "";
      } else {
        tmp += in[i];
      }
    }

    res.push_back(tmp);
    return res;
  }

  int isValidVarType(string in, char nxt){
    int i, j, cnt;

    if(isalnum(nxt)) return 0;

    if(in.substr(0,6) == "const "){
      in = in.substr(6);
      ftrim(in);
    }

    if(vartype.count(in)) return 1;

    rep(i,in.size()){
      if(tvartype.count( in.substr(0,i) )){
        cnt = 0;
        REP(j,i,in.size()){
          if(cnt==0 && isalnum(in[j])) break;
          if(in[j]=='<') cnt++;
          if(in[j]=='>') cnt--;
          if(cnt==0 && in[j]=='>'){
            j++;
            if(j==in.size()) return 1;
            if(in.substr(j) == "::iterator") return 1;
            break;
          }
        }
      }
    }

    return 0;
  }

  pair<string, char> nextToken(string &in){
    int i, f, dig = 0;
    string res1 = "";
    char res2 = '\0';

    i = 0;
    while(i < in.size() && isspace(in[i])) i++;
    if(i==in.size()) return make_pair(res1,res2);

    if(isdigit(in[0])) dig = 1;
    while(i < in.size() && (isalnum(in[i]) || (dig && in[i]=='.'))) res1 += in[i++];
    while(i < in.size() && isspace(in[i])) i++;
    res2 = in[i];
    return make_pair(res1,res2);
  }

  pair<string,string> var_definition(string in){
    int i, j, k, tt;
    string type, name, pre, suf, eq;
    vector<string> tmp;

    rep(i,in.size()+1) if(isValidVarType(in.substr(0,i), in[i])) tt = i;

    type = in.substr(0, tt);
    
    in = in.substr(tt);
    trim(in);

    if(in[in.size()-1] == ';'){
      in = in.substr(0, in.size()-1);
      trim(in);
    }

    tmp = split_p(in, '=');
    assert(tmp.size() <= 2);
    if(tmp.size()==2){
      eq = tmp[1];
      trim(eq);
      in = tmp[0];
    }

    rep(i,in.size()) if(isalpha(in[i])) break;
    REP(j,i,in.size()) if(!isalnum(in[j])) break;
    name = in.substr(i, j-i);
    pre = in.substr(0, i);
    suf = in.substr(j);
    trim(name);
    trim(pre);
    trim(suf);
    
    return make_pair(name, type+","+pre+","+suf+","+eq);
  }

  string getElementalyVarType(string in){
    int i;
    vector<string> vs;
    
    while(in.size() && !isalpha(in[0])) in = in.substr(1);
    if(in.size()==0) return (string)"error";
    rep(i,in.size()) if(!isalnum(in[i])) break;
    in = in.substr(0, i);

    if(localvar.count(in)){
      in = localvar[in];
    } else if(argvar.count(in)){
      in = argvar[in];
    } else if(globalvar.count(in)){
      in = globalvar[in];
    } else {
      return (string)"error";
    }

    vs = split_p(in, ',');
    in = vs[0];
    return in;
  }

  string getEquationType(string in){
    int i;
    int double_fg = 0, float_fg = 0, ull_fg = 0, ll_fg = 0, unsigned_fg = 0, int_fg = 0, char_fg = 0;
    pair<string,char> stchar;
    string tp, str;

    for(;;){
      trim(in);
      if(in.size()==0) break;

      if(in[0] == '['){
        i = pairBracket(in, 0);
        if(i == -1) return (string)"error";
        in = in.substr(i+1);
        continue;
      }

      if(in[0] == '"'){
        char_fg = 1;
        for(i=1;;i++) if(in[i]=='"') break;
        in = in.substr(i+1);
        continue;
      }

      if(in[0] == '\''){
        char_fg = 1;
        for(i=1;;i++) if(in[i]=='\'') break;
        in = in.substr(i+1);
        continue;
      }

      stchar = nextToken(in);
      str = stchar.first;
//      fprintf(stderr,"[%s - %c]\n", stchar.first.c_str(), stchar.second);
      in = in.substr(str.size());
      if(str.size()==0) in = in.substr(1);

      if(str.size()){
        if(isdigit(str[0])) {
          if(strpos(str, (string)".") >= 0){
            if(str[str.size()-1] == 'f') tp = "float";
            else                         tp = "double";
          } else {
            if(str.size() >= 2 && str.substr(str.size()-2) == "ll") tp = "long long";
            else                                                    tp = "int";
          }
        } else if(isalpha(str[0])) {
          tp = getElementalyVarType(str);
        }

        if(tp == "double") double_fg = 1;
        if(tp == "float") float_fg = 1;
        if(tp == "long long") ll_fg = 1;
        if(tp == "int") int_fg = 1;
        if(tp == "char") char_fg = 1;
      }
    }

    if(double_fg) return (string)"double";
    if(float_fg) return (string)"float";
    if(ll_fg) return (string)"long long";
    if(int_fg) return (string)"int";
    if(char_fg) return (string)"char";
    return (string)"error";
  }

  string getUnusedVarName(void){
    int i, k, p, r;
    string f = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
    string res;

    for(k=8;;k++) rep(p,100){
      res = "";

      r = rand()%52;
      res += f[r];
      REP(i,1,k){
        r = rand()%f.size();
        res += f[r];
      }

      if(localvar.count(res)) continue;
      if(argvar.count(res)) continue;
      if(globalvar.count(res)) continue;
      return res;
    }

    return res;
  }

  void insert(string &in, int p){
    int pure;
    for(;;){
      ftrim(in);
      if(in.size()==0) break;

      if(in[0]=='{') pure = 1; else  pure = 0;
      
      code *cc = new code;
      cc->setUpnode(this);
      cc->set(in, (string)"block");
      nxt.insert(nxt.begin()+p, nxtlst.size());
      nxtlst.push_back(cc);
      str.insert(str.begin()+p, (string)"");
      if(pure){
        strtype.insert(strtype.begin()+p, (string)"block");
      } else {
        strtype.insert(strtype.begin()+p, (string)"block-inserted");
      }

      p++;
    }
  }

  int strpos(string &A, string B){
    int i, As, Bs;
    As = A.size();
    Bs = B.size();
    rep(i,As-Bs+1) if(A.substr(i,Bs) == B) return i;
    return -1;
  }

  int replaceAll(string &A, string B, string C){
    int i, res = 0;
    for(;;){
      i = strpos(A, B);
      if(i==-1) break;
      A = A.substr(0, i) + C + A.substr(i+B.size());
    }
    return res;
  }

  int strpos_t(string &A, string B){
    int i, As, Bs;
    As = A.size();
    Bs = B.size();
    rep(i,As-Bs+1) if(A.substr(i,Bs) == B){
      if(i && (isalnum(A[i-1]) || A[i-1]=='_')) continue;
      if(i+Bs<As && (isalnum(A[i+Bs]) || A[i+Bs]=='_')) continue;
      return i;
    }
    return -1;
  }

  int replaceAll_t(string &A, string B, string C){
    int i, res = 0;
    for(;;){
      i = strpos_t(A, B);
      if(i==-1) break;
      A = A.substr(0, i) + C + A.substr(i+B.size());
    }
    return res;
  }

  vector<string> rd_wt_array(string in){ // 1+((a,b)(N)+2) returns "1+(", "(a,b)", "N", "+2)", hoge returns "hoge"
    int i, j, k, bf = -1, fg;
    vector<string> res;
    
    rep(i,in.size()){
      fg = 0;
      if(bf == ')' || bf == ']') fg = 1;
      if(isalnum(bf)) fg = 1;
      if(fg && in[i] == '('){
        rep(k,4) res.push_back((string)"");
        j = pairBracket(in, i);
        res[2] = in.substr(i+1, j-i-1);
        res[3] = in.substr(j+1);
        in = in.substr(0, i);

        trim(in);
        i = in.size() - 1;
        while(in[i] == ']'){
          i = pairBracket(in, i) - 1;
          while(isspace(in[i])) i--;
        }
        if(in[i]==')'){
          i = pairBracket(in, i);
          res[0] = in.substr(0, i);
          res[1] = in.substr(i);
        } else {
          while(i >= 0 && isalnum(in[i])) i--;
          i++;
          res[0] = in.substr(0, i);
          res[1] = in.substr(i);
        }

        rep(k,4) trim(res[k]);
        return res;
      }
      if(!isspace(in[i])) bf = in[i];
    }

    res.push_back(in);
    return res;
  }

  int checkBracketsCoressponding(string &in){
    int i;
    int k4, k5;
    stack<int> s;

    k4 = k5 = 0;
    rep(i,in.size()){
      if(k4==0 && k5==0){
        if(in[i] == '(') s.push(1);
        if(in[i] == '[') s.push(2);
        if(in[i] == '{') s.push(3);
        if(in[i] == ')'){
          if(s.size()==0 || s.top()!=1) return 0;
          s.pop();
        }
        if(in[i] == ']'){
          if(s.size()==0 || s.top()!=2) return 0;
          s.pop();
        }
        if(in[i] == '}'){
          if(s.size()==0 || s.top()!=3) return 0;
          s.pop();
        }
      }
      if(in[i] == '\'') k4^=1;
      if(in[i] == '"') k5^=1;
    }
    if(k4 || k5 || s.size()) return 0;
    return 1;
  }

  int pairBracket(string &in, int p){
    int i;
    int k1, k2, k3, k4, k5;

    k1 = k2 = k3 = k4 = k5 = 0;
    if(in[p]=='(' || in[p]=='[' || in[p]=='{'){
      for(i=p;i<in.size();i++){
        if(k4==0 && k5==0){
          if(in[i] == '(') k1++;
          if(in[i] == ')') k1--;
          if(in[i] == '[') k2++;
          if(in[i] == ']') k2--;
          if(in[i] == '{') k3++;
          if(in[i] == '}') k3--;
        }
        if(in[i] == '\'') k4^=1;
        if(in[i] == '"') k5^=1;
        if(k1==0 && k2==0 && k3==0 && k4==0 && k5==0) return i;
      }
    } else if(in[p]==')' || in[p]==']' || in[p]=='}'){
      for(i=p;i>=0;i--){
        if(k4==0 && k5==0){
          if(in[i] == '(') k1++;
          if(in[i] == ')') k1--;
          if(in[i] == '[') k2++;
          if(in[i] == ']') k2--;
          if(in[i] == '{') k3++;
          if(in[i] == '}') k3--;
        }
        if(in[i] == '\'') k4^=1;
        if(in[i] == '"') k5^=1;
        if(k1==0 && k2==0 && k3==0 && k4==0 && k5==0) return i;
      }
    }
    return -1;
  }

  void set(string &in, string tp){
    int i, j, k, st, cnt, tt, fg;
    int k1, k2, k3, k4, k5;
    int end_type = 0;
    int fg_main = 0, fg_return = 0;
    string tmp, tmpstr;
    string str_type, str_var;
    vector<string> str_arg;
    pair<string, char> stchar;
    vector<string> vtmp;

    type = tp;
    set_init();

    ftrim(in);
    if(tp != "program" && in[0]=='{') end_type = 1, in = in.substr(1);
    if(tp == "program") end_type = 2;

    for(;;){
      ftrim(in);
//      printf("--- %s\n",in.substr(0,10).c_str());

      if(in.substr(0,13) == "//no-unlocked" && (isspace(in[13]) || in[13]=='\0')){
        g_flags.insert((string)"no-unlocked");
        in = in.substr(13);
        continue;
      }
      
      if(in.substr(0,2) == "//"){
        rep(i,in.size()) if(in[i] == '\n'){ i++; break; }
        in = in.substr(i);
        continue;
      }
      if(in.substr(0,2) == "/*"){
        REP(i,1,in.size()) if(in.substr(i-1,2) == "*/"){ i++; break; }
        in = in.substr(i);
        continue;
      }

      if(end_type==-1) break;
      if(end_type==0) end_type = -1;

      if(in.size()==0 && end_type==2) break;
      if(in.size() && in[0]=='}' && end_type==1){
        in = in.substr(1);
        if(name == "int main()" && fg_return == 0){
          str.push_back((string)"return 0;");
          nxt.push_back(-1);
          strtype.push_back((string)"sentence");
        }
        break;
      }

      
      if(in.substr(0,8) == "#include"){
        cnt = 0;
        for(i=0;;i++){
          if(in[i]=='"') cnt++;
          if(cnt==2 || in[i]=='>'){ i++; break; }
        }
        tmp = in.substr(0, i);
        in = in.substr(i);
        str.push_back(tmp);
        nxt.push_back(-1);
        strtype.push_back((string)"sentence-include");
        continue;
      }

      if(in.substr(0,7) == "#define"){
        for(i=0;;i++){
          if(in[i]=='\n' || in[i]=='\r') break;
        }
        tmp = in.substr(0, i);
        in = in.substr(i);
        str.push_back(tmp);
        nxt.push_back(-1);
        strtype.push_back((string)"sentence-define");
        continue;
      }

      
      tmpstr = "";
      for(;;){
        stchar = nextToken(in);
//        printf("[%s] [%c]\n",stchar.first.c_str(),stchar.second);
        if(stchar.first == "template"){
          cnt = 0;
          for(i=0;;i++){
            if(in[i]=='<') cnt++;
            if(in[i]=='>') cnt--;
            if(cnt==0 && in[i]=='>'){ i++; break; }
          }
          tmpstr += in.substr(0, i) + " ";
          in = in.substr(i);
          ftrim(in);
          continue;
        }

        if(stchar.first == "inline"){
          tmpstr += "inline ";
          in = in.substr(6);
          ftrim(in);
          continue;
        }
        
        break;
      }

      fg = 0;

      tt = -1;
      REP(i,1,100) if(isValidVarType(in.substr(0,i), in[i])) tt = i;

      stchar = nextToken(in);
      
      if(tt >= 0){
        for(i=0;;i++){
          if(in[i] == ';') break;
          if(in[i] == '('){ fg++; stchar.first = "function"; break; }
        }
      }

      if(stchar.first == "if" && stchar.second =='(') fg = 1;
      if(stchar.first == "for" && stchar.second =='(') fg = 1;
      if(stchar.first == "rep" && stchar.second =='(') fg = 1;
      if(stchar.first == "REP" && stchar.second =='(') fg = 1;
      if(stchar.first == "while" && stchar.second =='(') fg = 1;
      if(stchar.first == "else") fg = 2;
      if(stchar.first == "struct") fg = 3;

      if(fg==2){
        string str_t = in.substr(4,100);
        pair<string,char> stch_t = nextToken(str_t);
        if(stch_t.first == "if" && stch_t.second == '('){
          fg = 1;
          stchar.first = "else if";
        }
      }
      
      if(fg || in[0]=='{'){
        fg_return = 0;

        if(fg==1){
          cnt = 0;
          rep(i,in.size()){
            if(in[i] == '(') cnt++;
            if(in[i] == ')') cnt--;
            if(in[i] == ')' && cnt == 0){
              i++; break;
            }
          }
        } else if(fg==2) {
          i = stchar.first.size();
        } else if(fg==3) {
          rep(i,in.size()) if(in[i]=='{' || in[i] == ';') break;
          tmp = in.substr(0, i);
          trim(tmp);
          vtmp = split_p(tmp, ' ');
          if(tmpstr.size() == 0) add_vartype(vtmp[vtmp.size()-1]);
          else                   add_tvartype(vtmp[vtmp.size()-1]);
        } else {
          i = 0;
        }
        tmp = in.substr(0, i);
        in = in.substr(i);

//        printf("[[ %s : %s : fg = %d]]\n", tmp.c_str(), in.substr(0,10).c_str(), fg);
//        exit(0);

        code_replace(tmp);

        if(type=="program" && tmp=="" && fg_main==0) tmp = "int main()", fg_main = 1;

        if(tmp.substr(0,3) == "rep" || tmp.substr(0,3) == "REP"){
          while(tmp[0]!='(') tmp = tmp.substr(1);
          while(tmp[tmp.size()-1]!=')') tmp = tmp.substr(0, tmp.size()-1);
          tmp = tmp.substr(1, tmp.size()-2);
          str_arg = split_p(tmp, ',');
          if(str_arg.size()==2){
            tmp = (string)"for(" + str_arg[0] + "=0;" + str_arg[0] + "<" + str_arg[1] + ";" + str_arg[0] + "++)";
          } else if(str_arg.size()==3){
            tmp = (string)"for(" + str_arg[0] + "=" + str_arg[1] + ";" + str_arg[0] + "<" + str_arg[2] + ";" + str_arg[0] + "++)";
          } else {
            assert(0);
          }
          stchar.first = "for";
        }

        code *cc = new code;
        cc->setUpnode(this);

        if(tmpstr.size()){
          string tt = tmpstr;
          trim_until(tt, '<', '>');
          assert(tt.size()>=2);
          tt = tt.substr(1, tt.size()-2);
          vtmp = split_p(tt, ',');
          rep(i,vtmp.size()){
            trim(vtmp[i]);
            if(vtmp[i].substr(0,6)=="class "){
              vtmp[i] = vtmp[i].substr(5);
              trim(vtmp[i]);
              cc->vartype.insert(vtmp[i]);
            }
          }
        }
        if(stchar.first == "function"){
          string tt = tmp;
          pair<string,string> ss;
          trim_until(tt, '(', ')');
          assert(tt.size()>=2);
          tt = tt.substr(1, tt.size()-2);
          vtmp = split_p(tt, ',');
          rep(i,vtmp.size()){
            trim(vtmp[i]);
            if(vtmp[i].size()==0 || vtmp[i]=="void") continue;
            ss = cc->var_definition(vtmp[i]);
            cc->argvar[ss.first] = ss.second;
          }
        }
        if(stchar.first == "for"){
        }

        cc->name = tmpstr + tmp;
        cc->set(in, (string)"block");
        nxt.push_back(nxtlst.size());
        nxtlst.push_back(cc);
        str.push_back(tmpstr + tmp);
        strtype.push_back((string)"block-"+stchar.first);
        continue;
      }

      k1 = k2 = k3 = k4 = k5 = 0;
      st = 0;
      if(tt != -1) st = tt;
      
      for(i=st;;i++){
        if(k4==0 && k5==0){
          if(in[i] == '(') k1++;
          if(in[i] == ')') k1--;
          if(in[i] == '[') k2++;
          if(in[i] == ']') k2--;
          if(in[i] == '{') k3++;
          if(in[i] == '}') k3--;
        }
        if(in[i] == '\'') k4^=1;
        if(in[i] == '"') k5^=1;

        if(k1==0 && k2==0 && k3==0 && k4==0 && k5==0){
          if(in[i] == ',' || in[i] == ';'){
            tmp = in.substr(st, i+1-st);
            tmp[tmp.size()-1] = ';';

            if(tt >= 0){
              if(isalnum(tmp[0])) tmp = " "+tmp;
              str_type = in.substr(0, tt);
              tmp = str_type + tmp;

              code_replace(tmp);

              pair<string,string> pss = var_definition(tmp);
              str_var = pss.first;
              str_type = pss.second;
              localvar[str_var] = str_type;
            }

            code_replace(tmp);
            if(strpos(tmp, (string)">?=") >= 0){
              string bf, af;
              k = strpos(tmp, (string)">?=");
              ifun.doit.insert((string)"chmax");

              bf = tmp.substr(0,k);
              af = tmp.substr(k+3);
              af = af.substr(0, af.size()-1);
              trim(bf);
              trim(af);
              tmp = (string)"chmax(" + bf + ", " + af + ");";
            }
            if(strpos(tmp, (string)"<?=") >= 0){
              string bf, af;
              k = strpos(tmp, (string)"<?=");
              ifun.doit.insert((string)"chmin");

              bf = tmp.substr(0,k);
              af = tmp.substr(k+3);
              af = af.substr(0, af.size()-1);
              trim(bf);
              trim(af);
              tmp = (string)"chmin(" + bf + ", " + af + ");";
            }
              
            //cout << tmpstr + tmp << endl;
            stchar = nextToken(tmp);
            if(stchar.first == "return") fg_return = 1; else fg_return = 0;
            
//            printf("[%s][%c]\n",stchar.first.c_str(), stchar.second);
            if( (stchar.first == "rd" || stchar.first == "reader") && stchar.second == '(' ){
              trim_until(tmp, '(', ')');
              tmp = tmp.substr(1,tmp.size()-2);
              vtmp = split_p(tmp, ',');
              rep(k,vtmp.size()){
                trim(vtmp[k]);
                string etype = getElementalyVarType(vtmp[k]);
                string stc;

                if(etype=="int"){
                  ifun.doit.insert((string)"reader_int");
                } else if(etype=="long long"){
                  ifun.doit.insert((string)"reader_ll");
                } else if(etype=="char"){
                  ifun.doit.insert((string)"reader_char_array");
                } else {
                  fprintf(stderr, "unknown type [%s] for rd (reader)\n", etype.c_str());
                  assert(0);
                }

                string strtmp;
                vector<string> vstrtmp;

                strtmp = vtmp[k];
                trim(strtmp);
                vstrtmp = rd_wt_array(strtmp);
                if(vstrtmp.size() > 1){
                  string vn = getUnusedVarName(), var;
                  
                  if(vstrtmp[1][0]=='('){
                    vector<string> vars;
                    vstrtmp[1] = vstrtmp[1].substr(1, vstrtmp[1].size()-2);
                    vars = split_p(vstrtmp[1], ',');
                    var = "";
                    rep(j,vars.size()){
                      if(j) var += ", ";
                      trim(vars[j]);
                      var += vstrtmp[0] + vars[j] + "[" + vn + "]" + vstrtmp[3];
                    }
                  } else {
                    var = vstrtmp[0] + vstrtmp[1] + "[" + vn + "]" + vstrtmp[3];
                  }
                  stc = (string)"{ int " + vn + "; rep(" + vn + "," + vstrtmp[2] + ") rd(" + var + "); }";
                  insert(stc, str.size());
                } else {
                  stc = (string)"rd(" + vstrtmp[0] + ");";

                  str.push_back(stc);
                  nxt.push_back(-1);
                  strtype.push_back((string)"sentence");
                }

              }
            } else if( (stchar.first == "wt" || stchar.first == "writer") && (stchar.second == '[' || stchar.second == '(') ){
              string optarg = "", basestr = "";
              if(stchar.second == '['){
                int ss, tt;
                string stmp; vector<string> vtmp1, vtmp2;
                pair<string,char> ctmp;
                
                ss = strpos(tmp, "[");
                tt = pairBracket(tmp, ss);
                optarg = tmp.substr(ss, tt-ss+1);
                tmp = tmp.substr(tt+1);

                stmp = optarg.substr(1, optarg.size()-2);
                vtmp1 = split_p(stmp, ',');
                rep(k,vtmp1.size()){
                  trim(vtmp1[k]);
                  ctmp = nextToken(vtmp1[k]);
                  if(ctmp.first == "B" && ctmp.second == '='){
                    vtmp2 = split_p(vtmp1[k], '=');
                    trim(vtmp2[1]);
                    basestr = "," + vtmp2[1];
                  }
                }
              }

              
              trim_until(tmp, '(', ')');
              tmp = tmp.substr(1,tmp.size()-2);
              vtmp = split_p(tmp, ',');
              rep(k,vtmp.size()){
                trim(vtmp[k]);
                string etype = getEquationType(vtmp[k]);
                string stc;

                if(etype=="int"){
                  if(basestr=="") ifun.doit.insert((string)"writer_int");
                  else            ifun.doit.insert((string)"writer_int_withBase");
                } else if(etype=="long long"){
                  if(basestr=="") ifun.doit.insert((string)"writer_ll");
                  else            ifun.doit.insert((string)"writer_ll_withBase");
                } else if(etype=="char"){
                  ifun.doit.insert((string)"writer_char_array");
                } else {
                  assert(0);
                }

                string strtmp;
                vector<string> vstrtmp;

                strtmp = vtmp[k];
                trim(strtmp);
                vstrtmp = rd_wt_array(strtmp);
                if(vstrtmp.size() > 1){
                  string vn = getUnusedVarName(), var;
                  
                  if(vstrtmp[1][0]=='('){
                    vector<string> vars;
                    vstrtmp[1] = vstrtmp[1].substr(1, vstrtmp[1].size()-2);
                    vars = split_p(vstrtmp[1], ',');
                    var = "";
                    rep(j,vars.size()){
                      if(j) var += ", ";
                      trim(vars[j]);
                      var += vstrtmp[0] + vars[j] + "[" + vn + "]" + vstrtmp[3];
                    }
                  } else {
                    var = vstrtmp[0] + vstrtmp[1] + "[" + vn + "]" + vstrtmp[3];
                  }
                  
                  if(k==vtmp.size()-1){
                    stc = (string)"{\n  int "+vn+";\n  if("+vstrtmp[2]+"==0) putchar_unlocked('\\n');\n  else {\n    rep("+vn+","+vstrtmp[2]+"-1) wtSp" + optarg + "("+var+");\n    wt" + optarg + "("+var+");\n  }\n}";
                  } else {
                    stc = (string)"{ int " + vn + "; rep(" + vn + "," + vstrtmp[2] + ") wtSp" + optarg + "(" + var + "); }";
                  }
                  insert(stc, str.size());
                } else {
                  if(etype=="int" || etype=="long long") vstrtmp[0] += basestr;
                  stc = (string)"wt_L(" + vstrtmp[0] + ");";

                  str.push_back(stc);
                  nxt.push_back(-1);
                  strtype.push_back((string)"sentence");
                  
                  if(k==vtmp.size()-1){
                    stc = (string)"putchar_unlocked('\\n');";
                  } else {
                    stc = (string)"putchar_unlocked(' ');";
                  }
                  str.push_back(stc);
                  nxt.push_back(-1);
                  strtype.push_back((string)"sentence");
                }

              }
            } else if( (stchar.first == "wtSp" || stchar.first == "writerSp") && (stchar.second == '[' || stchar.second == '(') ){
              string optarg = "", basestr = "";
              if(stchar.second == '['){
                int ss, tt;
                string stmp; vector<string> vtmp1, vtmp2;
                pair<string,char> ctmp;
                
                ss = strpos(tmp, "[");
                tt = pairBracket(tmp, ss);
                optarg = tmp.substr(ss, tt-ss+1);
                tmp = tmp.substr(tt+1);

                stmp = optarg.substr(1, optarg.size()-2);
                vtmp1 = split_p(stmp, ',');
                rep(k,vtmp1.size()){
                  trim(vtmp1[k]);
                  ctmp = nextToken(vtmp1[k]);
                  if(ctmp.first == "B" && ctmp.second == '='){
                    vtmp2 = split_p(vtmp1[k], '=');
                    trim(vtmp2[1]);
                    basestr = "," + vtmp2[1];
                  }
                }
              }

              trim_until(tmp, '(', ')');
              tmp = tmp.substr(1,tmp.size()-2);
              vtmp = split_p(tmp, ',');
              rep(k,vtmp.size()){
                trim(vtmp[k]);
                string etype = getEquationType(vtmp[k]);
                string stc;

                if(etype=="int"){
                  if(basestr=="") ifun.doit.insert((string)"writer_int");
                  else            ifun.doit.insert((string)"writer_int_withBase");
                } else if(etype=="long long"){
                  if(basestr=="") ifun.doit.insert((string)"writer_ll");
                  else            ifun.doit.insert((string)"writer_ll_withBase");
                } else if(etype=="char"){
                  ifun.doit.insert((string)"writer_char_array");
                } else {
                  assert(0);
                }

                string strtmp;
                vector<string> vstrtmp;

                strtmp = vtmp[k];
                trim(strtmp);
                vstrtmp = rd_wt_array(strtmp);
                if(vstrtmp.size() > 1){
                  string vn = getUnusedVarName(), var;
                  
                  if(vstrtmp[1][0]=='('){
                    vector<string> vars;
                    vstrtmp[1] = vstrtmp[1].substr(1, vstrtmp[1].size()-2);
                    vars = split_p(vstrtmp[1], ',');
                    var = "";
                    rep(j,vars.size()){
                      if(j) var += ", ";
                      trim(vars[j]);
                      var += vstrtmp[0] + vars[j] + "[" + vn + "]" + vstrtmp[3];
                    }
                  } else {
                    var = vstrtmp[0] + vstrtmp[1] + "[" + vn + "]" + vstrtmp[3];
                  }
                  
                  stc = (string)"{ int " + vn + "; rep(" + vn + "," + vstrtmp[2] + ") wtSp" + optarg + "(" + var + "); }";
                  insert(stc, str.size());
                } else {
                  if(etype=="int" || etype=="long long") vstrtmp[0] += basestr;
                  stc = (string)"wt_L(" + vstrtmp[0] + ");";

                  str.push_back(stc);
                  nxt.push_back(-1);
                  strtype.push_back((string)"sentence");
                  
                  stc = (string)"putchar_unlocked(' ');";
                  str.push_back(stc);
                  nxt.push_back(-1);
                  strtype.push_back((string)"sentence");
                }

              }
            } else if( (stchar.first == "wtLn" || stchar.first == "writerLn") && (stchar.second == '[' || stchar.second == '(') ){
              string optarg = "", basestr = "";
              if(stchar.second == '['){
                int ss, tt;
                string stmp; vector<string> vtmp1, vtmp2;
                pair<string,char> ctmp;
                
                ss = strpos(tmp, "[");
                tt = pairBracket(tmp, ss);
                optarg = tmp.substr(ss, tt-ss+1);
                tmp = tmp.substr(tt+1);

                stmp = optarg.substr(1, optarg.size()-2);
                vtmp1 = split_p(stmp, ',');
                rep(k,vtmp1.size()){
                  trim(vtmp1[k]);
                  ctmp = nextToken(vtmp1[k]);
                  if(ctmp.first == "B" && ctmp.second == '='){
                    vtmp2 = split_p(vtmp1[k], '=');
                    trim(vtmp2[1]);
                    basestr = "," + vtmp2[1];
                  }
                }
              }

              trim_until(tmp, '(', ')');
              tmp = tmp.substr(1,tmp.size()-2);
              vtmp = split_p(tmp, ',');
              rep(k,vtmp.size()){
                trim(vtmp[k]);
                string etype = getEquationType(vtmp[k]);
                string stc;

                if(etype=="int"){
                  if(basestr=="") ifun.doit.insert((string)"writer_int");
                  else            ifun.doit.insert((string)"writer_int_withBase");
                } else if(etype=="long long"){
                  if(basestr=="") ifun.doit.insert((string)"writer_ll");
                  else            ifun.doit.insert((string)"writer_ll_withBase");
                } else if(etype=="char"){
                  ifun.doit.insert((string)"writer_char_array");
                } else {
                  assert(0);
                }

                string strtmp;
                vector<string> vstrtmp;

                strtmp = vtmp[k];
                trim(strtmp);
                vstrtmp = rd_wt_array(strtmp);
                if(vstrtmp.size() > 1){
                  string vn = getUnusedVarName(), var;
                  
                  if(vstrtmp[1][0]=='('){
                    vector<string> vars;
                    vstrtmp[1] = vstrtmp[1].substr(1, vstrtmp[1].size()-2);
                    vars = split_p(vstrtmp[1], ',');
                    var = "";
                    rep(j,vars.size()){
                      if(j) var += ", ";
                      trim(vars[j]);
                      var += vstrtmp[0] + vars[j] + "[" + vn + "]" + vstrtmp[3];
                    }
                  } else {
                    var = vstrtmp[0] + vstrtmp[1] + "[" + vn + "]" + vstrtmp[3];
                  }
                  
                  stc = (string)"{ int " + vn + "; rep(" + vn + "," + vstrtmp[2] + ") wtLn" + optarg + "(" + var + "); }";
                  insert(stc, str.size());
                } else {
                  if(etype=="int" || etype=="long long") vstrtmp[0] += basestr;
                  stc = (string)"wt_L(" + vstrtmp[0] + ");";

                  str.push_back(stc);
                  nxt.push_back(-1);
                  strtype.push_back((string)"sentence");
                  
                  stc = (string)"putchar_unlocked('\\n');";
                  str.push_back(stc);
                  nxt.push_back(-1);
                  strtype.push_back((string)"sentence");
                }

              }
            } else if(strpos(tmp, (string)"..") >= 0) {
              int pl, pb, pa, fg = 0;
              int k1, k2, k3, k4, k5;
              string dots = "..";
              string vn, stc, bg, ed, fbg, fed;
              
              while(strpos(tmp, dots) >= 0) dots += ".";
              dots = dots.substr(1);

              vn = getUnusedVarName();
              stc = (string)"{ int " + vn + ";";

              for(;;){
                pl = strpos(tmp, dots);
                if(pl<0) break;
                
                k1 = k2 = k3 = k4 = k5 = 0;
                for(k=pl-1;k>=0;k--){
                  if(k4==0 && k5==0){
                    if(tmp[k] == '(') k1++;
                    if(tmp[k] == ')') k1--;
                    if(tmp[k] == '[') k2++;
                    if(tmp[k] == ']') k2--;
                    if(tmp[k] == '{') k3++;
                    if(tmp[k] == '}') k3--;
                  }
                  if(tmp[k] == '\'') k4^=1;
                  if(tmp[k] == '"') k5^=1;
                  if(k1 > 0 || k2 > 0 || k3 > 0) break;
                }
                pb = k + 1;
                
                k1 = k2 = k3 = k4 = k5 = 0;
                REP(k,pl,tmp.size()){
                  if(k4==0 && k5==0){
                    if(tmp[k] == '(') k1++;
                    if(tmp[k] == ')') k1--;
                    if(tmp[k] == '[') k2++;
                    if(tmp[k] == ']') k2--;
                    if(tmp[k] == '{') k3++;
                    if(tmp[k] == '}') k3--;
                  }
                  if(tmp[k] == '\'') k4^=1;
                  if(tmp[k] == '"') k5^=1;
                  if(k1 < 0 || k2 < 0 || k3 < 0) break;
                }
                pa = k;

                bg = tmp.substr(pb, pl-pb);
                ed = tmp.substr(pl+dots.size(), pa-pl-dots.size());

                if(fg==0){
                  fg = 1;
                  fbg = bg;
                  fed = ed;
                }
                if(bg==fbg) tmp = tmp.substr(0, pb) + vn + tmp.substr(pa);
                else        tmp = tmp.substr(0, pb) + vn + " - (" + fbg + ") + (" + bg + ")" + tmp.substr(pa);
              }

              stc += "rep(" + vn + ", " + fbg + ", (" + fed + ") + 1) ";
              stc += tmp;

              stc += "}";
              insert(stc, str.size());

            } else {

              str.push_back(tmpstr + tmp);
              nxt.push_back(-1);
              if(tt==-1){
                strtype.push_back((string)"sentence");
              } else {
                strtype.push_back((string)"sentence-var-def");
              }
            }
            
            if(in[i] == ','){
              st = i+1;
              continue;
            } else {
              i++;
              break;
            }
          }
        }
      }
      in = in.substr(i);
      continue;

    }
  }

  void debug_writer(int tab = 0){
    int i, j;
    
    std::set<string>::iterator it1;
    map<string,string>::iterator it2;
    printf("vartype  :");
    for(it1=vartype.begin();it1!=vartype.end();it1++) printf(" (%s)", it1->c_str()); puts("");
    printf("tvartype :");
    for(it1=tvartype.begin();it1!=tvartype.end();it1++) printf(" (%s)", it1->c_str()); puts("");
    printf("localvar :");
    for(it2=localvar.begin();it2!=localvar.end();it2++) printf(" (%s->%s)", it2->first.c_str(), it2->second.c_str()); puts("");
    printf("globalvar:");
    for(it2=globalvar.begin();it2!=globalvar.end();it2++) printf(" (%s->%s)", it2->first.c_str(), it2->second.c_str()); puts("");
    printf("argvar   :");
    for(it2=argvar.begin();it2!=argvar.end();it2++) printf(" (%s->%s)", it2->first.c_str(), it2->second.c_str()); puts("");
    
    rep(i,str.size()){
      rep(j,tab) printf(" ");
      printf("%4d: %s   [%s]\n",i,str[i].c_str(),strtype[i].c_str());
      if(nxt[i]>=0) nxtlst[nxt[i]]->debug_writer(tab+4);
    }
  }

  void output_vardef(int mode, int tab){
    int i, f;
    std::set<string> tp;
    std::set<string>::iterator it;
    vector<string> tmp;
    map<string,string>::iterator mt;

    for(mt=localvar.begin(); mt!=localvar.end(); mt++){
      tmp = split_p2(mt->second, ',');
      assert(tmp.size()==4);
      tp.insert(tmp[0]);
    }
    for(it=tp.begin(); it!=tp.end(); it++){
      rep(i,tab) printf(" ");
      f = 0;
      for(mt=localvar.begin(); mt!=localvar.end(); mt++){
        tmp = split_p2(mt->second, ',');
        assert(tmp.size()==4);
        if(*it == tmp[0]){
          if(f) printf(", ");
          else  printf("%s ", it->c_str());
          f++;
          printf("%s%s%s", tmp[1].c_str(), mt->first.c_str(), tmp[2].c_str());
          if(tmp[3].size() != 0) printf("=%s",tmp[3].c_str());
        }
      }
      printf(";\n");
    }
  }

  void output(int mode, int tab = 0){
    int i, j;
    int var = 0;
    string s;
    
    rep(i,str.size()){
      if(strtype[i]=="sentence-var-def") continue;

      if(var==0 && (type != "program" || strtype[i] != "block-inserted")){
        var = 1;
        output_vardef(mode, tab);
      }
      
      if(strtype[i]=="block-inserted"){
        nxtlst[nxt[i]]->output(mode, tab);
        continue;
      }
      
      rep(j,tab) printf(" ");
      s = str[i];
      if(g_flags.count((string)"no-unlocked")) replaceAll_t(s, "putchar_unlocked", "putchar");
      if(g_flags.count((string)"no-unlocked")) replaceAll_t(s, "getchar_unlocked", "getchar");
      printf("%s",s.c_str());
      
      if(strtype[i].substr(0,5)=="block"){
        printf("{\n");
      } else {
        printf("\n");
      }
      if(nxt[i]>=0) nxtlst[nxt[i]]->output(mode, tab+(mode==0?2:0));
      if(strtype[i].substr(0,5)=="block"){
        rep(j,tab) printf(" ");
        printf("}\n");
      }
    }
  }
};

int main(){
  int i, j, k, f;
  char buf[10001];
  string str, str_store;

  for(;;){
    k = fread(buf, 1, 10000, stdin);
    buf[k] = '\0';
    str += buf;
    if(k < 10000) break;
  }
  str_store = str;

  ifun.set();
  string tmp;

  code c;
  c.up = NULL;
  c.set(str, (string)"program");

  tmp = ifun.get_insert_string();
  c.insert(tmp, 0);
  
//  c.debug_writer();
  c.output(0);
  printf("// cLay varsion 20170408-3\n");


  str = str_store;
  f = 0;
  printf("\n// --- original code ---\n");
  rep(i,str.size()){
    if(!f) printf("// "), f = 1;
    if(str[i]=='\r') continue;
    putchar(str[i]);
    if(str[i]=='\n') f = 0;
  }

  return 0;
}

Current time: 2021年11月29日17時55分17秒
Last modified: 2017年04月08日22時24分26秒 (by laycrs)
Tags: no_tags
トップページに戻る

Logged in as: unknown user (not login)

ログイン: