Package org.eclipse.imp.pdb.facts

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


   
    Map<Type,Type> bindings = new HashMap<Type,Type>();
    bindings.put(Factory.TypeParam, t);
    Type typeType = Factory.Type.instantiate(bindings);
   
    IMapWriter defs = vf.mapWriter();
    for (Map.Entry<IConstructor, IConstructor> entry : definitions.entrySet()) {
      defs.put(entry.getKey(), entry.getValue());
    }
    IValue result = vf.constructor(Factory.Type_Reified.instantiate(bindings), symbol, defs.done());
   
    return ResultFactory.makeResult(typeType, result, ctx);
  }
View Full Code Here


            w.append(field.accept(this));
          }
        }
       
        IListWriter kwTypes = vf.listWriter();
        IMapWriter kwDefaults = vf.mapWriter();
       
        for (String key : type.getKeywordParameters()) {
          kwTypes.insert(vf.constructor(Factory.Symbol_Label, vf.string(key), type.getKeywordParameterType(key).accept(this)));
        }
       
        alts.insert(vf.constructor(Factory.Production_Cons, vf.constructor(Factory.Symbol_Label,  vf.string(type.getName()), adt), w.done(), kwTypes.done(), kwDefaults.done(), vf.set()));
        choice = vf.constructor(Factory.Production_Choice, adt, alts.done());
        definitions.put(adt, choice);
      }
     
     
View Full Code Here

  private IValue traverseMapOnce(IValue subject, CaseBlockList casesOrRules,
      DIRECTION direction, PROGRESS progress, FIXEDPOINT fixedpoint, TraverseResult tr) {
    IMap map = (IMap) subject;
    if(!map.isEmpty()){
      IMapWriter w = eval.getValueFactory().mapWriter(map.getType());
      Iterator<Entry<IValue,IValue>> iter = map.entryIterator();
      boolean hasChanged = false;
      boolean hasMatched = false;
     
      while (iter.hasNext()) {
        Entry<IValue,IValue> entry = iter.next();
        tr.changed = false;
        tr.matched = false;
        IValue newKey = traverseOnce(entry.getKey(), casesOrRules, direction, progress, fixedpoint, tr);
        hasChanged |= tr.changed;
        hasMatched |= tr.matched;
        tr.changed = false;
        tr.matched = false;
        IValue newValue = traverseOnce(entry.getValue(), casesOrRules, direction, progress, fixedpoint, tr);
        hasChanged |= tr.changed;
        hasMatched |= tr.matched;
        w.put(newKey, newValue);
      }
      tr.changed = hasChanged;
      tr.matched = hasMatched;
      return w.done();
    } else {
      return subject;
    }
  }
View Full Code Here

        hm.put(key, wValSet);
      }
      wValSet.insert(val);
    }
   
    IMapWriter w = values.mapWriter();
    for(IValueWrap v : hm.keySet()){
      w.put(v.getValue(), hm.get(v).done());
    }
    return w.done();
  }
View Full Code Here

  {
     if(lst.length() == 0){
        return values.mapWriter().done();
     }
   
     IMapWriter w = values.mapWriter();
     Set<IValueWrap> seenKeys = new HashSet<IValueWrap>();
     for(IValue v : lst){
       ITuple t = (ITuple) v;
       IValueWrap key = new IValueWrap(t.get(0));
       if(seenKeys.contains(key))
        throw RuntimeExceptionFactory.MultipleKey(key.getValue(), null, null);
       seenKeys.add(key);
       w.put(key.getValue(), t.get(1));
     }
     return w.done();
  }
View Full Code Here

  }
 
  public IValue invertUnique(IMap M)
  //@doc{invertUnique -- return map with key and value inverted; values are unique}
  {
    IMapWriter w = values.mapWriter();
    HashSet<IValue> seenValues = new HashSet<IValue>();
    Iterator<Entry<IValue,IValue>> iter = M.entryIterator();
    while (iter.hasNext()) {
      Entry<IValue,IValue> entry = iter.next();
      IValue key = entry.getKey();
      IValue val = entry.getValue();
      if(seenValues.contains(val))
          throw RuntimeExceptionFactory.MultipleKey(val, null, null);
      seenValues.add(val);
      w.put(val, key);
    }
    return w.done();
  }
View Full Code Here

        hm.put(val, wKeySet);
      }
      wKeySet.insert(key);
    }
   
    IMapWriter w = values.mapWriter();
   
    iter = M.entryIterator();
    for(IValue v : hm.keySet()){
      w.put(v, hm.get(v).done());
    }
    return w.done();
  }
View Full Code Here

  }
 
  public IValue getKeywordParameters(INode T)
  //@doc{getChildren -- get the children of a node}
  {
    IMapWriter w = values.mapWriter();
   
    if (T.mayHaveKeywordParameters()) {
      for(Entry<String, IValue> e : T.asWithKeywordParameters().getParameters().entrySet()){
        w.put(values.string(e.getKey()), e.getValue());
      }
    }
   
    return w.done();
  }
View Full Code Here

    return itoStringValue(T);
  }
 
  public IMap getAnnotations(INode node) {
    java.util.Map<java.lang.String,IValue> map = node.asAnnotatable().getAnnotations();
    IMapWriter w = values.mapWriter();
   
    for (Entry<java.lang.String,IValue> entry : map.entrySet()) {
      w.put(values.string(entry.getKey()), entry.getValue());
    }
   
    return w.done();
  }
View Full Code Here

      IConstructor before = (IConstructor) args.get(0);
      IConstructor ast = (IConstructor) args.get(1);
      IConstructor after = (IConstructor) args.get(2);
      IValue result = implode(store, type, ast, splicing, ctx);
      if (result.getType().isNode()) {
        IMapWriter comments = values.mapWriter();
        comments.putAll((IMap)((INode)result).asAnnotatable().getAnnotation("comments"));
        IList beforeComments = extractComments(before);
        if (!beforeComments.isEmpty()) {
          comments.put(values.integer(-1), beforeComments);
        }
        IList afterComments = extractComments(after);
        if (!afterComments.isEmpty()) {
          comments.put(values.integer(((INode)result).arity()), afterComments);
        }
        result = ((INode)result).asAnnotatable().setAnnotation("comments", comments.done());
      }
      return result;
    }
   
    if (TreeAdapter.isLexical(tree)) {
      java.lang.String constructorName = unescapedConsName(tree);
      java.lang.String yield = TreeAdapter.yield(tree);
      if (constructorName != null) {
        // make a single argument constructor  with yield as argument
        // if there is a singleton constructor with a str argument
        if (!type.isAbstractData() && !isUntypedNodeType(type)) {
          throw RuntimeExceptionFactory.illegalArgument(tree, null, null, "Constructor (" + constructorName + ") should match with abstract data type and not with " + type);
        }
       
        if (isUntypedNodeType(type)) {
          return values.node(constructorName, values.string(yield));
        }
       
        Set<Type> conses = findConstructors(type, constructorName, 1, store);
        Iterator<Type> iter = conses.iterator();
        while (iter.hasNext()) {
          try {
            @SuppressWarnings("unused")
            Type cons = iter.next();
            ISourceLocation loc = TreeAdapter.getLocation(tree);
            IConstructor ast = makeConstructor(type, constructorName, ctx, values.string(yield));
            return ast.asAnnotatable().setAnnotation("location", loc);
          }
          catch (Backtrack b) {
            continue;
          }
        }
        throw new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, null, null, "Cannot find a constructor " + type));
      }
      if (type.isInteger()) {
        return values.integer(yield);
      }
      if (type.isReal()) {
        return values.real(yield);
      }
      if (type.isBool()) {
        if (yield.equals("true")) {
          return values.bool(true);
        }
        if (yield.equals("false")) {
          return values.bool(false);
        }
        throw new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, null, null, "Bool type does not match with " + yield));
      }
      if (type.isString() || isUntypedNodeType(type)) {
        // NB: in "node space" all lexicals become strings
        return values.string(yield);
      }
     
      throw RuntimeExceptionFactory.illegalArgument(tree, null, null, "Missing lexical constructor");
    }
   
    //Set implementation added here by Jurgen at 19/07/12 16:45
    if (TreeAdapter.isList(tree)) {
      if (type.isList() || splicing || isUntypedNodeType(type)) {
        // if in node space, we also make a list;
        // NB: this breaks type safety if the top-level tree
        // is itself a list.
       
        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;
//      int length = args.length();
//      while (i < length) {
View Full Code Here

TOP

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

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.