예제 #1
0
static 
PT_Symbol renameInSymbol(PT_Symbol symbol,
                         PT_Symbol formalParam,
                         PT_Symbol actualParam)
{
  if (PT_isEqualSymbol(symbol, formalParam)) {
    return actualParam;
  }
  if (PT_hasSymbolSymbols(symbol)) {
    PT_Symbols args = PT_getSymbolSymbols(symbol);

    PT_Symbols newArgs = renameInSymbols(args, formalParam, actualParam);
 
    symbol = PT_setSymbolSymbols(symbol, newArgs);
  }
  if (PT_hasSymbolParameters(symbol)) {
    PT_Symbols args = PT_getSymbolParameters(symbol);

    PT_Symbols newArgs = renameInSymbols(args, formalParam, actualParam);
 
    symbol = PT_setSymbolParameters(symbol, newArgs);
  }
  if (PT_hasSymbolSymbol(symbol)) {
    PT_Symbol arg = PT_getSymbolSymbol(symbol);

    PT_Symbol newArg = renameInSymbol(arg, formalParam, actualParam);

    symbol = PT_setSymbolSymbol(symbol, newArg);
  }
  if (PT_hasSymbolLhs(symbol)) {
    PT_Symbol arg = PT_getSymbolLhs(symbol);

    PT_Symbol newArg = renameInSymbol(arg, formalParam, actualParam);
 
    symbol = PT_setSymbolLhs(symbol, newArg);
  }
  if (PT_hasSymbolRhs(symbol)) {
    PT_Symbol arg = PT_getSymbolRhs(symbol);

    PT_Symbol newArg = renameInSymbol(arg, formalParam, actualParam);
 
    symbol = PT_setSymbolRhs(symbol, newArg);
  }
  if (PT_hasSymbolHead(symbol)) {
    PT_Symbol arg = PT_getSymbolHead(symbol);

    PT_Symbol newArg = renameInSymbol(arg, formalParam, actualParam);
 
    symbol = PT_setSymbolHead(symbol, newArg);
  }
  if (PT_hasSymbolRest(symbol)) {
    PT_Symbols args = PT_getSymbolRest(symbol);

    PT_Symbols newArgs = renameInSymbols(args, formalParam, actualParam);
 
    symbol = PT_setSymbolRest(symbol, newArgs);
  }
  if (PT_hasSymbolSeparator(symbol)) {
    PT_Symbol arg = PT_getSymbolSeparator(symbol);

    PT_Symbol newArg = renameInSymbol(arg, formalParam, actualParam);
 
    symbol = PT_setSymbolSeparator(symbol, newArg);
  }
  return symbol;
}
예제 #2
0
static int 
yieldSymbol(PT_Symbol symbol, int idx, char *buf, int bufSize)
{
  int i;

  assert(idx <= bufSize); 

  if (PT_isOptLayoutSymbol(symbol)) {
    return idx;
  }
  if (PT_isSymbolLit(symbol) || PT_isSymbolCilit(symbol)) {
    ATbool ci = PT_isSymbolCilit(symbol);
    char *str = PT_getSymbolString(symbol);
    int len = strlen(str);

    buf[idx++] = ci ? '\'' : '"';
    for (i = 0; i < len; i++) {
      buf[idx++] = str[i];
    }
    buf[idx++] = ci ? '\'' : '"';

    return idx;
  }
  if (PT_isSymbolSort(symbol)) {
    char *str = PT_getSymbolString(symbol);
    int len = strlen(str);

    for (i = 0; i < len; i++) {
      buf[idx++] = str[i];
    }

    return idx; 
  }
  if (PT_isSymbolEmpty(symbol)) {
    buf[idx++] = '(';
    buf[idx++] = ' ';
    buf[idx++] = ')';
    return idx;
  }
  if (PT_isSymbolSeq(symbol)) {
    PT_Symbols newSymbols = PT_getSymbolSymbols(symbol);
    buf[idx++] = '(';
    idx = yieldSymbols(newSymbols, idx, buf, bufSize);
    buf[idx++] = ')';
    return idx;
  }
  if (PT_isSymbolOpt(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    idx = yieldSymbol(newSymbol, idx, buf, bufSize);
    buf[idx++] = '?';
    return idx;
  }
  if (PT_isSymbolVarSym(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    buf[idx++] = '<';
    idx = yieldSymbol(newSymbol, idx, buf, bufSize);
    buf[idx++] = '-';
    buf[idx++] = 'V';
    buf[idx++] = 'A';
    buf[idx++] = 'R';
    buf[idx++] = '>';
    return idx;
  }
  if (PT_isSymbolCf(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    idx = yieldSymbol(newSymbol, idx, buf, bufSize);

    return idx;
  }
  if (PT_isSymbolLex(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    buf[idx++] = '<';
    idx = yieldSymbol(newSymbol, idx, buf, bufSize);
    buf[idx++] = '-';
    buf[idx++] = 'L';
    buf[idx++] = 'E';
    buf[idx++] = 'X';
    buf[idx++] = '>';

    return idx;
  }
  if (PT_isSymbolAlt(symbol)) {
    PT_Symbol leftSymbol = PT_getSymbolLhs(symbol);
    PT_Symbol rightSymbol = PT_getSymbolRhs(symbol);

    idx = yieldSymbol(leftSymbol, idx, buf, bufSize);
    buf[idx++] = ' ';
    buf[idx++] = '|';
    buf[idx++] = ' ';
    idx = yieldSymbol(rightSymbol, idx, buf, bufSize);

    return idx;
  }
  if (PT_isSymbolTuple(symbol)) {
    PT_Symbol headSymbol = PT_getSymbolHead(symbol);
    PT_Symbols restSymbols = PT_getSymbolRest(symbol);

    buf[idx++] = '<';
    idx = yieldSymbol(headSymbol, idx, buf, bufSize);
    buf[idx++] = ',';
    idx = yieldSymbolParameters(restSymbols, idx, buf, bufSize);
    buf[idx++] = '>';

    return idx;
  }
  if (PT_isSymbolIterPlus(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    idx = yieldSymbol(newSymbol, idx, buf, bufSize);
    buf[idx++] = '+';

    return idx;
  }
  if (PT_isSymbolIterStar(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    idx = yieldSymbol(newSymbol, idx, buf, bufSize);
    buf[idx++] = '*';

    return idx;
  }
  if (PT_isSymbolIterPlusSep(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    PT_Symbol separator = PT_getSymbolSeparator(symbol);
    buf[idx++] = '{';
    idx = yieldSymbol(newSymbol, idx, buf, bufSize);
    buf[idx++] = ' ';
    idx = yieldSymbol(separator, idx, buf, bufSize);
    buf[idx++] = '}';
    buf[idx++] = '+';

    return idx;
  }
  if (PT_isSymbolIterStarSep(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    PT_Symbol separator = PT_getSymbolSeparator(symbol);
    buf[idx++] = '{';
    idx = yieldSymbol(newSymbol, idx, buf, bufSize);
    buf[idx++] = ' ';
    idx = yieldSymbol(separator, idx, buf, bufSize);
    buf[idx++] = '}';
    buf[idx++] = '*';

    return idx;
  }
  if (PT_isSymbolIterN(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    int number = PT_getSymbolNumber(symbol);
    idx = yieldSymbol(newSymbol, idx, buf, bufSize);
    idx = yieldInteger(number, idx, buf, bufSize);
    buf[idx++] = '+';

    return idx; 
  }
  if (PT_isSymbolIterSepN(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    PT_Symbol separator = PT_getSymbolSeparator(symbol);
    int number = PT_getSymbolNumber(symbol);
    buf[idx++] = '{';
    idx = yieldSymbol(newSymbol, idx, buf, bufSize);
    buf[idx++] = ' ';
    idx = yieldSymbol(separator, idx, buf, bufSize);
    buf[idx++] = '}';
    idx = yieldInteger(number, idx, buf, bufSize);
    buf[idx++] = '+';

    return idx;      
  }
  if (PT_isSymbolParameterizedSort(symbol)) {
    char *str = PT_getSymbolSort(symbol);
    PT_Symbols newSymbols = PT_getSymbolParameters(symbol);
    int len = strlen(str);

    for (i = 0; i < len; i++) {
      buf[idx++] = str[i];
    }

    buf[idx++] = '[';
    buf[idx++] = '[';
    idx = yieldSymbolParameters(newSymbols, idx, buf, bufSize);
    buf[idx++] = ']';
    buf[idx++] = ']';
    return idx;
  }
  if (PT_isSymbolFunc(symbol)) {
    PT_Symbols newSymbols = PT_getSymbolSymbols(symbol);
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    buf[idx++] = '(';
    idx = yieldSymbols(newSymbols, idx, buf, bufSize);
    buf[idx++] = ' ';
    buf[idx++] = '=';
    buf[idx++] = '>';
    buf[idx++] = ' ';
    idx = yieldSymbol(newSymbol, idx, buf, bufSize);
    buf[idx++] = ')';
    return idx;
  }
  if (PT_isSymbolCharClass(symbol)) {
    PT_CharRanges charRanges = PT_getSymbolRanges(symbol);
    buf[idx++] = '[';
    idx = yieldCharRanges(charRanges,idx,buf,bufSize);
    buf[idx++] = ']';

    return idx;
  }
  if (PT_isSymbolLayout(symbol)) {
    return idx;
  }

  ATwarning("yieldSymbol: unknown symbol: %t\n", symbol);
  return idx;
}
예제 #3
0
static int
lengthOfSymbol(PT_Symbol symbol)
{
  if (PT_isOptLayoutSymbol(symbol)) {
    return 0;
  }

  if (PT_isSymbolLit(symbol) || PT_isSymbolCilit(symbol)) {
    char *str = PT_getSymbolString(symbol);
    return strlen(str) + 2;
  }
  if (PT_isSymbolSort(symbol)) {
    char *str = PT_getSymbolString(symbol);
    return strlen(str);
  }
  if (PT_isSymbolEmpty(symbol)) {
    return 3;
  }
  if (PT_isSymbolSeq(symbol)) {
    PT_Symbols newSymbols = PT_getSymbolSymbols(symbol);
    return lengthOfSymbols(newSymbols) + 2;
  }
  if (PT_isSymbolOpt(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    return lengthOfSymbol(newSymbol) + 1;
  }
  if (PT_isSymbolVarSym(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    return 6 + lengthOfSymbol(newSymbol);
  }
  if (PT_isSymbolCf(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    return lengthOfSymbol(newSymbol);
  }
  if (PT_isSymbolLex(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    return 6+ lengthOfSymbol(newSymbol);
  }
  if (PT_isSymbolAlt(symbol)) {
    PT_Symbol leftSymbol = PT_getSymbolLhs(symbol);
    PT_Symbol rightSymbol = PT_getSymbolRhs(symbol);
    return lengthOfSymbol(leftSymbol) + 3 + lengthOfSymbol(rightSymbol);
  }
  if (PT_isSymbolTuple(symbol)) {
    PT_Symbol headSymbol = PT_getSymbolHead(symbol);
    PT_Symbols restSymbols = PT_getSymbolRest(symbol);
    return lengthOfSymbol(headSymbol) + 3 + lengthOfSymbols(restSymbols);
  }
  if (PT_isSymbolIterPlus(symbol) 
      ||
      PT_isSymbolIterStar(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    return lengthOfSymbol(newSymbol) + 1;
  }
  if (PT_isSymbolIterPlusSep(symbol) 
      ||
      PT_isSymbolIterStarSep(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    PT_Symbol separator = PT_getSymbolSeparator(symbol);
    return lengthOfSymbol(newSymbol) + lengthOfSymbol(separator) + 4;
  }
  if (PT_isSymbolIterN(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    int number = PT_getSymbolNumber(symbol);
    return lengthOfSymbol(newSymbol) + lengthOfInteger(number) + 1;
  }
  if (PT_isSymbolIterSepN(symbol)) {
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    PT_Symbol separator = PT_getSymbolSeparator(symbol);
    int number = PT_getSymbolNumber(symbol);
    return lengthOfSymbol(newSymbol) + lengthOfInteger(number) +
           lengthOfSymbol(separator) + 4;
  }
  if (PT_isSymbolParameterizedSort(symbol)) {
    char *str = PT_getSymbolSort(symbol);
    PT_Symbols newSymbols = PT_getSymbolParameters(symbol);
    return lengthOfSymbols(newSymbols) + strlen(str) + 4;
  }
  if (PT_isSymbolFunc(symbol)) {
    PT_Symbols newSymbols = PT_getSymbolSymbols(symbol);
    PT_Symbol newSymbol = PT_getSymbolSymbol(symbol);
    return lengthOfSymbols(newSymbols) + lengthOfSymbol(newSymbol) + 6;
  }
  if (PT_isSymbolCharClass(symbol)) {
    return lengthOfCharRanges(PT_getSymbolRanges(symbol)) + 2;
  }
  if (PT_isSymbolLayout(symbol)) {
    return 0;
  }

  ATwarning("lengthOfSymbol: unknown symbol: %t\n", symbol);
  
  return 0;
}
예제 #4
0
파일: pt2sdf.c 프로젝트: codyhanson/sdfmetz
SDF_Symbol PTSymbolToSDFSymbol(PT_Symbol ptSymbol)
{
  SDF_Symbol result;

  if (PT_isSymbolLit(ptSymbol)) {
    char *str = PT_getSymbolString(ptSymbol);
    char *qstr = escape(str, "\"\\", QUOTED);
    SDF_Literal lit = SDF_makeLiteralQlit(SDF_makeQLiteralQuoted(SDF_makeCHARLISTString(qstr)));
    free(qstr);

    result = SDF_makeSymbolLit(lit);
  }
  else if (PT_isSymbolCf(ptSymbol)
	   || PT_isSymbolLex(ptSymbol)
	   || PT_isSymbolVarSym(ptSymbol)) {
    result = PTSymbolToSDFSymbol(PT_getSymbolSymbol(ptSymbol));
  }
  else if (PT_isSymbolEmpty(ptSymbol)) {
    result = SDF_makeSymbolEmpty(SDF_makeLayoutEmpty());
  }
  else if (PT_isSymbolSeq(ptSymbol)) {
    PT_Symbols ptSymbols = PT_getSymbolSymbols(ptSymbol);
    PT_Symbol ptHead = PT_getSymbolsHead(ptSymbols);
    SDF_Symbol sdfHead = PTSymbolToSDFSymbol(ptHead);
    PT_Symbols ptTail = PT_getSymbolsTail(ptSymbols);
 
    if (PT_isSymbolsEmpty(ptTail)) {
      ATabort("PTSymbolToSDFSymbol: empty tail in %s\n",
              PT_yieldSymbol(ptSymbol));

      result = NULL;
    }
    else {
      SDF_SymbolTail sdfSymbolTail = 
        (SDF_SymbolTail)PTSymbolsToSDFSymbolList(ptTail);

      result = SDF_makeSymbolSeq(SDF_makeLayoutEmpty(),
                                 sdfHead, 
                                 SDF_makeLayoutSpace(),
                                 sdfSymbolTail,
                                 SDF_makeLayoutEmpty());
    }
  }
  else if (PT_isSymbolOpt(ptSymbol)) {
    SDF_Symbol sdfSymbol = PTSymbolToSDFSymbol(PT_getSymbolSymbol(ptSymbol));

    result = SDF_makeSymbolOpt(sdfSymbol, SDF_makeLayoutEmpty());
  }
  else if (PT_isSymbolAlt(ptSymbol)) {
    SDF_Symbol sdfLhs = PTSymbolToSDFSymbol(PT_getSymbolLhs(ptSymbol));
    SDF_Symbol sdfRhs = PTSymbolToSDFSymbol(PT_getSymbolRhs(ptSymbol));

    result = SDF_makeSymbolAlt(sdfLhs, 
                               SDF_makeLayoutEmpty(), 
                               SDF_makeLayoutEmpty(),
                               sdfRhs);
  }
  else if (PT_isSymbolTuple(ptSymbol)) {
    SDF_Symbol sdfHead = PTSymbolToSDFSymbol(PT_getSymbolHead(ptSymbol));
    SDF_SymbolRest sdfRest = PTSymbolsToSDFSymbolRest(PT_getSymbolRest(ptSymbol));

    result = SDF_makeSymbolTuple(SDF_makeLayoutEmpty(),
                                 sdfHead, 
                                 SDF_makeLayoutEmpty(), 
                                 SDF_makeLayoutEmpty(),
                                 sdfRest,
                                 SDF_makeLayoutEmpty());
  }
  else if (PT_isSymbolSort(ptSymbol)) {
    char *str = PT_getSymbolSort(ptSymbol);
    SDF_Sort sort = SDF_makeSortMoreChars(SDF_makeCHARLISTString(str));

    result = SDF_makeSymbolSort(sort);
  }
  else if (PT_isSymbolIterPlus(ptSymbol)) { 
    SDF_Symbol sdfSymbol = PTSymbolToSDFSymbol(PT_getSymbolSymbol(ptSymbol));

    result = SDF_makeSymbolIter(sdfSymbol, SDF_makeLayoutEmpty());
  }
  else if (PT_isSymbolIterStar(ptSymbol)) { 
    SDF_Symbol sdfSymbol = PTSymbolToSDFSymbol(PT_getSymbolSymbol(ptSymbol));

    result = SDF_makeSymbolIterStar(sdfSymbol, SDF_makeLayoutEmpty());
  }
  else if (PT_isSymbolIterPlusSep(ptSymbol)) { 
    SDF_Symbol sdfSymbol = PTSymbolToSDFSymbol(PT_getSymbolSymbol(ptSymbol));
    SDF_Symbol sdfSep = PTSymbolToSDFSymbol(PT_getSymbolSeparator(ptSymbol));

    result = SDF_makeSymbolIterSep(SDF_makeLayoutEmpty(),
                                   sdfSymbol, 
                                   SDF_makeLayoutSpace(),
                                   sdfSep,
                                   SDF_makeLayoutEmpty(),
                                   SDF_makeLayoutEmpty());
  }
  else if (PT_isSymbolIterStarSep(ptSymbol)) { 
    SDF_Symbol sdfSymbol = PTSymbolToSDFSymbol(PT_getSymbolSymbol(ptSymbol));
    SDF_Symbol sdfSep = PTSymbolToSDFSymbol(PT_getSymbolSeparator(ptSymbol));

    result = SDF_makeSymbolIterStarSep(SDF_makeLayoutEmpty(),
                                       sdfSymbol, 
                                       SDF_makeLayoutSpace(),
                                       sdfSep,
                                       SDF_makeLayoutEmpty(),
                                       SDF_makeLayoutEmpty());
  }
  else if (PT_isSymbolIterN(ptSymbol)) { 
    char str[BUFSIZ];
    SDF_Symbol sdfSymbol = PTSymbolToSDFSymbol(PT_getSymbolSymbol(ptSymbol));
    int nr = PT_getSymbolNumber(ptSymbol);
    SDF_NatCon sdfNatCon;
    sprintf(str, "%d", nr);
    
    sdfNatCon = SDF_makeNatConDigits(SDF_makeCHARLISTString(str));

    result = SDF_makeSymbolIterN(sdfSymbol, 
                                 SDF_makeLayoutSpace(),
                                 sdfNatCon,
                                 SDF_makeLayoutEmpty());
  }
  else if (PT_isSymbolIterSepN(ptSymbol)) { 
    char str[BUFSIZ];
    SDF_Symbol sdfSymbol = PTSymbolToSDFSymbol(PT_getSymbolSymbol(ptSymbol));
    SDF_Symbol sdfSep = PTSymbolToSDFSymbol(PT_getSymbolSeparator(ptSymbol));
    int nr = PT_getSymbolNumber(ptSymbol);
    SDF_NatCon sdfNatCon;
    sprintf(str, "%d", nr);
    
    sdfNatCon = SDF_makeNatConDigits(SDF_makeCHARLISTString(str));

    result = SDF_makeSymbolIterSepN(SDF_makeLayoutEmpty(),
                                    sdfSymbol, 
                                    SDF_makeLayoutSpace(),
                                    sdfSep,
                                    SDF_makeLayoutEmpty(),
                                    SDF_makeLayoutEmpty(),
                                    sdfNatCon,
                                    SDF_makeLayoutEmpty());
  }
  else if (PT_isSymbolLayout(ptSymbol)) {
    ATabort("PTSymbolToSDFSymbol: layout.\n");
    result = NULL;
  }
  else {
    ATabort("PTSymbolToSDFSymbol: unable to convert symbol %t: %s\n",
            ptSymbol, PT_yieldSymbol(ptSymbol));

    result = NULL;
  }
  return result;
}