LiteralTreeImpl.java revision 1304:4da1c371efcb
161981Sbrian/*
261981Sbrian * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
361981Sbrian * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
461981Sbrian *
561981Sbrian * This code is free software; you can redistribute it and/or modify it
661981Sbrian * under the terms of the GNU General Public License version 2 only, as
761981Sbrian * published by the Free Software Foundation.  Oracle designates this
861981Sbrian * particular file as subject to the "Classpath" exception as provided
961981Sbrian * by Oracle in the LICENSE file that accompanied this code.
1061981Sbrian *
1161981Sbrian * This code is distributed in the hope that it will be useful, but WITHOUT
1261981Sbrian * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1361981Sbrian * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1461981Sbrian * version 2 for more details (a copy is included in the LICENSE file that
1561981Sbrian * accompanied this code).
1661981Sbrian *
1761981Sbrian * You should have received a copy of the GNU General Public License version
1861981Sbrian * 2 along with this work; if not, write to the Free Software Foundation,
1961981Sbrian * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2061981Sbrian *
2161981Sbrian * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2261981Sbrian * or visit www.oracle.com if you need additional information or have any
2361981Sbrian * questions.
2461981Sbrian */
2561981Sbrian
2661981Sbrianpackage jdk.nashorn.api.tree;
2761981Sbrian
2861981Sbrianimport jdk.nashorn.internal.ir.LiteralNode;
2961981Sbrian
3061981Sbrianfinal class LiteralTreeImpl extends ExpressionTreeImpl implements LiteralTree {
3161981Sbrian    private final Object value;
3261981Sbrian    private final Kind kind;
3361981Sbrian    LiteralTreeImpl(final LiteralNode<?> node) {
3461981Sbrian        super(node);
3561981Sbrian        this.kind = literalKind(node);
3661981Sbrian        this.value = node.getValue();
3761981Sbrian    }
3861981Sbrian
3961981Sbrian    @Override
4061981Sbrian    public Kind getKind() {
4161981Sbrian        return kind;
4261981Sbrian    }
4361981Sbrian
4461981Sbrian    @Override
4561981Sbrian    public Object getValue() {
4661981Sbrian        return value;
4761981Sbrian    }
4861981Sbrian
4961981Sbrian    private static Kind literalKind(final LiteralNode<?> node) {
5061981Sbrian        if (node.isBoolean()) {
5161981Sbrian            return Kind.BOOLEAN_LITERAL;
5261981Sbrian        } else if (node.isNumeric()) {
5361981Sbrian            return Kind.NUMBER_LITERAL;
5461981Sbrian        } else if (node.isString()) {
5561981Sbrian            return Kind.STRING_LITERAL;
5661981Sbrian        } else if (node.isNull()) {
5761981Sbrian            return Kind.NULL_LITERAL;
5861981Sbrian        } else {
5961981Sbrian            throw new AssertionError("should not reach here: " + node.getValue());
6061981Sbrian        }
6161981Sbrian    }
6261981Sbrian
6361981Sbrian    @Override
6461981Sbrian    public <R,D> R accept(final TreeVisitor<R,D> visitor, final D data) {
6561981Sbrian        return visitor.visitLiteral(this, data);
6661981Sbrian    }
6761981Sbrian}
6861981Sbrian