Package org.yinwang.yin

Examples of org.yinwang.yin.Scope


    public static Declare parseDeclare(Tuple tuple) throws ParserException {
        List<Node> elements = tuple.elements;
        if (elements.size() < 2) {
            throw new ParserException("syntax error in record type definition", tuple);
        }
        Scope properties = parseProperties(elements.subList(1, elements.size()));
        return new Declare(properties, tuple.file, tuple.start, tuple.end, tuple.line, tuple.col);
    }
View Full Code Here


        if (hasName && hasTuple) {
            throw new ParserException("parameters must be either all names or all tuples: " +
                    preParams.toString(), preParams);
        }

        Scope properties;
        if (hasTuple) {
            properties = parseProperties(paramTuples);
        } else {
            properties = null;
        }
View Full Code Here

        } else {
            parents = null;
            fields = elements.subList(2, elements.size());
        }

        Scope properties = parseProperties(fields);
        return new RecordDef((Name) name, parents, properties, tuple.file,
                tuple.start, tuple.end, tuple.line, tuple.col);
    }
View Full Code Here

        return ret;
    }


    public static Scope parseProperties(List<Node> fields) throws ParserException {
        Scope properties = new Scope();
        for (Node field : fields) {
            if (!(field instanceof Tuple &&
                    delimType(((Tuple) field).open, Constants.SQUARE_BEGIN) &&
                    ((Tuple) field).elements.size() >= 2))
            {
                throw new ParserException("incorrect form of descriptor: " + field.toString(), field);
            } else {
                List<Node> elements = parseList(((Tuple) field).elements);
                Node nameNode = elements.get(0);
                if (!(nameNode instanceof Name)) {
                    throw new ParserException("expect a name, but got: " + nameNode.toString(), nameNode);
                }
                String id = ((Name) nameNode).id;
                if (properties.containsKey(id)) {
                    throw new ParserException("duplicated name: " + nameNode.toString(), nameNode);
                }

                Node typeNode = elements.get(1);
                if (!(typeNode instanceof Name)) {
                    throw new ParserException("type must be a name, but got: " + typeNode.toString(), typeNode);
                }
                properties.put(id, "type", typeNode);

                Map<String, Node> props = parseMap(elements.subList(2, elements.size()));
                Map<String, Object> propsObj = new LinkedHashMap<>();
                for (Map.Entry<String, Node> e : props.entrySet()) {
                    propsObj.put(e.getKey(), e.getValue());
                }
                properties.putProperties(((Name) nameNode).id, propsObj);
            }
        }
        return properties;
    }
View Full Code Here

        }
    }


    public static Scope evalProperties(Scope unevaled, Scope s) {
        Scope evaled = new Scope();

        for (String field : unevaled.keySet()) {
            Map<String, Object> props = unevaled.lookupAllProps(field);
            for (Map.Entry<String, Object> e : props.entrySet()) {
                Object v = e.getValue();
                if (v instanceof Node) {
                    Value vValue = ((Node) v).interp(s);
                    evaled.put(field, e.getKey(), vValue);
                } else {
                    Util.abort("property is not a node, parser bug: " + v);
                }
            }
        }
View Full Code Here

        return evaled;
    }


    public static Scope typecheckProperties(Scope unevaled, Scope s) {
        Scope evaled = new Scope();

        for (String field : unevaled.keySet()) {
            if (field.equals(Constants.RETURN_ARROW)) {
                evaled.putProperties(field, unevaled.lookupAllProps(field));
            } else {
                Map<String, Object> props = unevaled.lookupAllProps(field);
                for (Map.Entry<String, Object> e : props.entrySet()) {
                    Object v = e.getValue();
                    if (v instanceof Node) {
                        Value vValue = ((Node) v).typecheck(s);
                        evaled.put(field, e.getKey(), vValue);
                    } else {
                        Util.abort("property is not a node, parser bug: " + v);
                    }
                }
            }
View Full Code Here

        this.propertyForm = propertyForm;
    }


    public Value interp(Scope s) {
        Scope properties = Declare.evalProperties(propertyForm, s);

        if (parents != null) {
            for (Node p : parents) {
                Value pv = p.interp(s);
                properties.putAll(((RecordType) pv).properties);
            }
        }
        Value r = new RecordType(name.id, this, properties);
        s.putValue(name.id, r);
        return r;
View Full Code Here

    }


    @Override
    public Value typecheck(Scope s) {
        Scope properties = Declare.typecheckProperties(propertyForm, s);

        if (parents != null) {
            for (Node p : parents) {
                Value pv = p.typecheck(s);
                if (!(pv instanceof RecordType)) {
                    Util.abort(p, "parent is not a record: " + pv);
                    return null;
                }
                Scope parentProps = ((RecordType) pv).properties;

                // check for duplicated keys
                for (String key : parentProps.keySet()) {
                    Value existing = properties.lookupLocalType(key);
                    if (existing != null) {
                        Util.abort(p, "conflicting field " + key +
                                " inherited from parent " + p + ": " + pv);
                        return null;
View Full Code Here

    }


    public Value interp(Scope s) {
        // evaluate and cache the properties in the closure
        Scope properties = propertyForm == null ? null : Declare.evalProperties(propertyForm, s);
        return new Closure(this, properties, s);
    }
View Full Code Here


    @Override
    public Value typecheck(Scope s) {
        // evaluate and cache the properties in the closure
        Scope properties = propertyForm == null ? null : Declare.typecheckProperties(propertyForm, s);
        FunType ft = new FunType(this, properties, s);
        TypeChecker.self.uncalled.add(ft);
        return ft;
    }
View Full Code Here

TOP

Related Classes of org.yinwang.yin.Scope

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.