Package org.eclipse.imp.pdb.facts

Examples of org.eclipse.imp.pdb.facts.IListWriter


  }

  public IValue toList(IMap M)
  //@doc{toList -- convert a map to a list}
  {
    IListWriter w = values.listWriter();
    Iterator<Entry<IValue,IValue>> iter = M.entryIterator();
    while (iter.hasNext()) {
      Entry<IValue,IValue> entry = iter.next();
      w.insert(values.tuple(entry.getKey(), entry.getValue()));
    }
    return w.done();
  }
View Full Code Here


  }

  public IValue getChildren(INode T)
  //@doc{getChildren -- get the children of a node}
  {
    IListWriter w = values.listWriter();
   
    for(IValue v : T.getChildren()){
      w.append(v);
    }
    return w.done();
  }
View Full Code Here

       
        Type elementType = type;
        if (!splicing && !isUntypedNodeType(type)) {
          elementType = type.getElementType();
        }
        IListWriter w = values.listWriter();
        for (IValue arg: TreeAdapter.getListASTArgs(tree)) {
          w.append(implode(store, elementType, (IConstructor) arg, false, ctx));
        }
        return w.done();
      }
      else if (type.isSet()) {
        Type elementType = splicing ? type : type.getElementType();
        ISetWriter w = values.setWriter();
        for (IValue arg: TreeAdapter.getListASTArgs(tree)) {
          w.insert(implode(store, elementType, (IConstructor) arg, false, ctx));
        }
        return w.done();
      }
      else {
        throw new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, null, null, "Cannot match list with " + type));
      }
    }
    //Changes end here
   
    if (TreeAdapter.isOpt(tree) && type.isBool()) {
      IList args = TreeAdapter.getArgs(tree);
      if (args.isEmpty()) {
        return values.bool(false);
      }
      return values.bool(true);
    }
   
    if (TreeAdapter.isOpt(tree)) {
      if (!type.isList() && !isUntypedNodeType(type)) {
        throw new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, null, null, "Optional should match with a list and not " + type));
      }
      Type elementType = isUntypedNodeType(type) ? type : type.getElementType();
      IListWriter w = values.listWriter();
      for (IValue arg: TreeAdapter.getASTArgs(tree)) {
        IValue implodedArg = implode(store, elementType, (IConstructor) arg, true, ctx);
        if (implodedArg instanceof IList) {
          // splicing
          for (IValue nextArg: (IList)implodedArg) {
            w.append(nextArg);
          }
        }
        else {
          w.append(implodedArg);
        }
        // opts should have one argument (if any at all)
        break;
      }
      return w.done();
    }
   
    if (TreeAdapter.isAmb(tree)) {
      if (!type.isSet()) {
        throw new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, null, null, "Ambiguous node should match with set and not " + type));
      }
      Type elementType = type.getElementType();
      ISetWriter w = values.setWriter();
      for (IValue arg: TreeAdapter.getAlternatives(tree)) {
        w.insert(implode(store, elementType, (IConstructor) arg, false, ctx));
      }
      return w.done();
    }
   
    if (ProductionAdapter.hasAttribute(TreeAdapter.getProduction(tree), Factory.Attribute_Bracket)) {
      return implode(store, type, (IConstructor) TreeAdapter.getASTArgs(tree).get(0), false, ctx);
    }
   
    if (TreeAdapter.isAppl(tree)) {
      IList args = TreeAdapter.getASTArgs(tree);
     
      int j = 0;
      IMapWriter cw = values.mapWriter();
      IListWriter aw = values.listWriter();
      for (IValue kid : TreeAdapter.getArgs(tree)) {
        if (TreeAdapter.isLayout((IConstructor) kid)) {
          IList cts = extractComments((IConstructor) kid);
          if (!cts.isEmpty()) {
            cw.put(values.integer(j), cts);
          }
          j++;
        }
        else if (!TreeAdapter.isLiteral((IConstructor) kid) &&
            !TreeAdapter.isCILiteral((IConstructor) kid) &&
            !TreeAdapter.isEmpty((IConstructor) kid)) {
          aw.append(kid);
        }
      }
      args = aw.done();
      int length = args.length();
      IMap comments = cw.done();
     
//      // this could be optimized.
//      i = 0;
View Full Code Here

    throw new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, null, null,
        "Cannot find a constructor for " + type));
  }
 
  private IList extractComments(IConstructor layout) {
    final IListWriter comments = values.listWriter();
    TreeVisitor<RuntimeException> visitor = new TreeVisitor<RuntimeException>() {

      @Override
      public IConstructor visitTreeAppl(IConstructor arg)
           {
        if (TreeAdapter.isComment(arg)) {
          comments.append(values.string(TreeAdapter.yield(arg)));
        }
        else {
          for (IValue t: TreeAdapter.getArgs(arg)) {
            t.accept(this);
          }
        }
        return arg;
      }

      @Override
      public IConstructor visitTreeAmb(IConstructor arg)
           {
        return arg;
      }

      @Override
      public IConstructor visitTreeChar(IConstructor arg)
           {
        return arg;
      }

      @Override
      public IConstructor visitTreeCycle(IConstructor arg)
           {
        return arg;
      }
     
    };
   
    layout.accept(visitor);
    return comments.done();
  }
View Full Code Here

  }

  public IValue toList(ISet st)
  // @doc{toList -- convert a set to a list}
  {
    IListWriter w = values.listWriter();

    for (IValue v : st) {
      w.insert(v);
    }

    return w.done();
  }
View Full Code Here

    return values.string(result.toString());
  }
 
  public IList split(IString sep, IString src) {
    String[] lst = src.getValue().split(Pattern.quote(sep.getValue()));
    IListWriter lw = values.listWriter();
    for (String s: lst) {
      lw.append(values.string(s));
    }
    return lw.done();
  }
View Full Code Here

  protected <U extends IValue> Result<U> multiplyList(ListResult that) {
    Type t1 = that.type.getElementType();
    Type t2 = type.getElementType();
    // Note: reverse
    Type type = getTypeFactory().listType(getTypeFactory().tupleType(t1, t2));
    IListWriter w = this.getValueFactory().listWriter();
    for (IValue v1 : that.getValue()) {
      for (IValue v2 : getValue()) {
        w.append(getValueFactory().tuple(v1, v2))
      }
    }
    return makeResult(type, w.done(), ctx)
  }
View Full Code Here

  }
 
  public IValue findAll(IString str, IString find){
    int iLength = str.length();
    int fLength = find.length();
    IListWriter w = values.listWriter();
   
    for(int i = 0; i <= iLength - fLength; i++){
      if(match(str, i, find)){
        w.append(values.integer(i));
      }
    }
    return w.done();
  }
View Full Code Here

  }
 
  @Override
  protected <U extends IValue> Result<U> intersectList(ListResult that) {
    // Note the reversal of args
    IListWriter w = this.getValueFactory().listWriter();
    Type type = getType().lub(that.getType());
    for(IValue v : that.getValue())
      if(getValue().contains(v))
        w.append(v);
    return makeResult(type, w.done(), ctx);
  }
View Full Code Here

    for (int i = 0;  i < arity1; i++) {
      fieldTypes[i] = tupleType.getFieldType(i);
    }
    fieldTypes[arity1] = eltType;
    Type resultTupleType = getTypeFactory().tupleType(fieldTypes);
    IListWriter writer = getValueFactory().listWriter();
    IValue fieldValues[] = new IValue[arity1 + 1];
    for (IValue relValue: that.getValue()) {
      for (IValue setValue: this.getValue()) {
        for (int i = 0; i < arity1; i++) {
          fieldValues[i] = ((ITuple)relValue).get(i);
        }
        fieldValues[arity1] = setValue;
        writer.append(getValueFactory().tuple(fieldValues));
      }
    }
    Type resultType = getTypeFactory().lrelTypeFromTuple(resultTupleType);
    return makeResult(resultType, writer.done(), ctx);
  }
View Full Code Here

TOP

Related Classes of org.eclipse.imp.pdb.facts.IListWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.