Search in sources :

Example 1 with IListWriter

use of io.usethesource.vallang.IListWriter in project rascal by usethesource.

the class Prelude method implode.

protected IValue implode(TypeStore store, Type type, IConstructor arg0, boolean splicing) {
    ITree tree = (ITree) arg0;
    Backtrack failReason = null;
    // always yield if expected type is str, except if regular
    if (type.isString() && !splicing) {
        return values.string(TreeAdapter.yield(tree));
    }
    if (SymbolAdapter.isStartSort(TreeAdapter.getType(tree))) {
        IList args = TreeAdapter.getArgs(tree);
        ITree before = (ITree) args.get(0);
        ITree ast = (ITree) args.get(1);
        ITree after = (ITree) args.get(2);
        IValue result = implode(store, type, ast, splicing);
        if (result.getType().isNode()) {
            IMapWriter comments = values.mapWriter();
            comments.putAll((IMap) ((INode) result).asWithKeywordParameters().getParameter("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).asWithKeywordParameters().setParameter("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) {
            // if there is a singleton constructor with a str argument
            if (!type.isAbstractData() && !isUntypedNodeType(type)) {
                throw RuntimeExceptionFactory.illegalArgument(tree, "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(store, type, constructorName, values.string(yield));
                    return ast.asWithKeywordParameters().setParameter("location", loc);
                } catch (Backtrack b) {
                    failReason = b;
                    continue;
                }
            }
            throw failReason != null ? failReason : new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, "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, "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, "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, (ITree) arg, false));
            }
            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, (ITree) arg, false));
            }
            return w.done();
        } else {
            throw new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, "Cannot match list with " + type));
        }
    }
    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, "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, (ITree) arg, true);
            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, "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, (ITree) arg, false));
        }
        return w.done();
    }
    if (ProductionAdapter.hasAttribute(TreeAdapter.getProduction(tree), RascalValueFactory.Attribute_Bracket)) {
        return implode(store, type, (ITree) TreeAdapter.getASTArgs(tree).get(0), false);
    }
    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((ITree) kid)) {
                IList cts = extractComments((ITree) kid);
                if (!cts.isEmpty()) {
                    cw.put(values.integer(j), cts);
                }
                j++;
            } else if (!TreeAdapter.isLiteral((ITree) kid) && !TreeAdapter.isCILiteral((ITree) kid) && !TreeAdapter.isEmpty((ITree) 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) {
        // if (TreeAdapter.isEmpty((IConstructor) args.get(i))) {
        // length--;
        // args = args.delete(i);
        // }
        // else {
        // i++;
        // }
        // }
        java.lang.String constructorName = unescapedConsName(tree);
        if (constructorName == null) {
            if (length == 1) {
                // jump over injection
                return implode(store, type, (ITree) args.get(0), splicing);
            }
            // make a tuple if we're in node space
            if (isUntypedNodeType(type)) {
                return values.tuple(implodeArgs(store, type, args));
            }
            if (!type.isTuple()) {
                throw new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, "Constructor does not match with " + type));
            }
            if (length != type.getArity()) {
                throw new Backtrack(RuntimeExceptionFactory.arityMismatch(type.getArity(), length));
            }
            return values.tuple(implodeArgs(store, type, args));
        }
        // if in node space, make untyped nodes
        if (isUntypedNodeType(type)) {
            INode ast = values.node(constructorName, implodeArgs(store, type, args));
            return ast.asWithKeywordParameters().setParameter("location", TreeAdapter.getLocation(tree)).asWithKeywordParameters().setParameter("comments", comments);
        }
        // make a typed constructor
        if (!type.isAbstractData()) {
            throw new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, "Constructor (" + constructorName + ") should match with abstract data type and not with " + type));
        }
        Set<Type> conses = findConstructors(type, constructorName, length, store);
        Iterator<Type> iter = conses.iterator();
        while (iter.hasNext()) {
            try {
                Type cons = iter.next();
                ISourceLocation loc = TreeAdapter.getLocation(tree);
                IValue[] implodedArgs = implodeArgs(store, cons, args);
                IConstructor ast = makeConstructor(store, type, constructorName, implodedArgs);
                return ast.asWithKeywordParameters().setParameter("location", loc).asWithKeywordParameters().setParameter("comments", comments);
            } catch (Backtrack b) {
                failReason = b;
                continue;
            }
        }
        throw failReason != null ? failReason : new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, "Cannot find a constructor for " + type + " with name " + constructorName + " and arity " + length + " for syntax type \'" + ProductionAdapter.getSortName(TreeAdapter.getProduction(tree)) + "\'"));
    }
    throw failReason != null ? failReason : new Backtrack(RuntimeExceptionFactory.illegalArgument(tree, "Cannot find a constructor for " + type));
}
Also used : INode(io.usethesource.vallang.INode) IMapWriter(io.usethesource.vallang.IMapWriter) IListWriter(io.usethesource.vallang.IListWriter) ITree(org.rascalmpl.values.parsetrees.ITree) IList(io.usethesource.vallang.IList) IMap(io.usethesource.vallang.IMap) Type(io.usethesource.vallang.type.Type) ISourceLocationType(org.rascalmpl.uri.ISourceLocationWatcher.ISourceLocationType) ISourceLocationChangeType(org.rascalmpl.uri.ISourceLocationWatcher.ISourceLocationChangeType) IValue(io.usethesource.vallang.IValue) ISetWriter(io.usethesource.vallang.ISetWriter) IConstructor(io.usethesource.vallang.IConstructor) ISourceLocation(io.usethesource.vallang.ISourceLocation)

Example 2 with IListWriter

use of io.usethesource.vallang.IListWriter in project rascal by usethesource.

the class Prelude method extractComments.

private IList extractComments(IConstructor layout) {
    final IListWriter comments = values.listWriter();
    TreeVisitor<RuntimeException> visitor = new TreeVisitor<RuntimeException>() {

        @Override
        public ITree visitTreeAppl(ITree 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 ITree visitTreeAmb(ITree arg) {
            return arg;
        }

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

        @Override
        public ITree visitTreeCycle(ITree arg) {
            return arg;
        }
    };
    layout.accept(visitor);
    return comments.done();
}
Also used : TreeVisitor(org.rascalmpl.values.parsetrees.visitors.TreeVisitor) IValue(io.usethesource.vallang.IValue) IListWriter(io.usethesource.vallang.IListWriter) ITree(org.rascalmpl.values.parsetrees.ITree)

Example 3 with IListWriter

use of io.usethesource.vallang.IListWriter in project rascal by usethesource.

the class Profiler method getProfileData.

public IList getProfileData() {
    IValueFactory VF = ValueFactoryFactory.getValueFactory();
    IListWriter w = VF.listWriter();
    for (Map.Entry<ISourceLocation, Count> e : sortData(ast)) {
        w.insert(VF.tuple(e.getKey(), VF.integer(e.getValue().getTicks())));
    }
    return w.done();
}
Also used : IListWriter(io.usethesource.vallang.IListWriter) ISourceLocation(io.usethesource.vallang.ISourceLocation) IValueFactory(io.usethesource.vallang.IValueFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with IListWriter

use of io.usethesource.vallang.IListWriter in project rascal by usethesource.

the class Prelude method toList.

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();
}
Also used : Entry(java.util.Map.Entry) IValue(io.usethesource.vallang.IValue) IListWriter(io.usethesource.vallang.IListWriter)

Example 5 with IListWriter

use of io.usethesource.vallang.IListWriter in project rascal by usethesource.

the class Prelude method extractPath.

private IList extractPath(IValue start, IValue u) {
    IListWriter w = values.listWriter();
    if (!start.equals(u)) {
        w.insert(u);
        while (!pred.get(u).equals(start)) {
            u = pred.get(u);
            w.insert(u);
        }
    }
    w.insert(start);
    return w.done();
}
Also used : IListWriter(io.usethesource.vallang.IListWriter)

Aggregations

IListWriter (io.usethesource.vallang.IListWriter)63 IValue (io.usethesource.vallang.IValue)30 IString (io.usethesource.vallang.IString)17 IList (io.usethesource.vallang.IList)16 Type (io.usethesource.vallang.type.Type)14 IConstructor (io.usethesource.vallang.IConstructor)10 UnexpectedType (org.rascalmpl.interpreter.staticErrors.UnexpectedType)9 IInteger (io.usethesource.vallang.IInteger)8 IOException (java.io.IOException)6 SQLException (java.sql.SQLException)6 ITree (org.rascalmpl.values.parsetrees.ITree)6 IMapWriter (io.usethesource.vallang.IMapWriter)5 ISourceLocation (io.usethesource.vallang.ISourceLocation)5 IValueFactory (io.usethesource.vallang.IValueFactory)5 Connection (java.sql.Connection)5 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 IMap (io.usethesource.vallang.IMap)4 ISetWriter (io.usethesource.vallang.ISetWriter)4 ITuple (io.usethesource.vallang.ITuple)4