All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lt.compiler.SemanticScope Maven / Gradle / Ivy

Go to download

The latte-lang compiler project, which contains compiler and runtime required library.

The newest version!
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 KuiGang Wang
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package lt.compiler;

import lt.compiler.semantic.*;
import lt.compiler.semantic.builtin.DoubleTypeDef;
import lt.compiler.semantic.builtin.LongTypeDef;

import java.util.*;

/**
 * semantic scope
 */
public class SemanticScope {
        public static class MethodRecorder {
                public final SMethodDef method;
                public final int paramCount;

                public MethodRecorder(SMethodDef method, int paramCount) {
                        this.method = method;
                        this.paramCount = paramCount;
                }
        }

        public final SemanticScope parent;

        private final InvokableMeta meta;

        private final Map leftValueMap = new LinkedHashMap();
        private final Map innerMethodMap = new HashMap();

        private final STypeDef sTypeDef;

        private Ins.This aThis;

        public SemanticScope(SemanticScope parent, InvokableMeta meta) {
                this.parent = parent;
                sTypeDef = null;
                this.meta = meta;
        }

        public SemanticScope(STypeDef sTypeDef, InvokableMeta meta) {
                this.sTypeDef = sTypeDef;
                parent = null;
                this.meta = meta;
        }

        public InvokableMeta getMeta() {
                return meta;
        }

        public LeftValue getLeftValue(String name) {
                if (leftValueMap.containsKey(name)) {
                        return leftValueMap.get(name);
                } else if (parent != null) {
                        return parent.getLeftValue(name);
                } else {
                        return null;
                }
        }

        public LinkedHashMap getLocalVariables() {
                LinkedHashMap map;
                if (parent != null) {
                        map = parent.getLocalVariables();
                } else {
                        map = new LinkedHashMap();
                }
                for (Map.Entry entry : leftValueMap.entrySet()) {
                        String k = entry.getKey();
                        LeftValue v = entry.getValue();
                        map.put(k, v.type());
                }
                return map;
        }

        public LinkedHashMap getRawLocalVariables() {
                LinkedHashMap map;
                if (parent != null) {
                        map = parent.getRawLocalVariables();
                } else {
                        map = new LinkedHashMap();
                }
                for (Map.Entry entry : leftValueMap.entrySet()) {
                        String k = entry.getKey();
                        LeftValue v = entry.getValue();
                        map.put(k, v);
                }
                return map;
        }

        public void putLeftValue(String name, LeftValue v) {
                leftValueMap.put(name, v);
        }

        public void addMethodDef(String name, MethodRecorder innerMethod) {
                innerMethodMap.put(name, innerMethod);
        }

        /**
         * get a list of leftValue
         *
         * @param count       size of the required list (may be less, but won't be more)
         * @param lambdaParam lambda param
         * @return a list of LeftValue(s)
         */
        public List getLeftValues(int count, boolean lambdaParam) {
                List list;
                if (parent != null) list = parent.getLeftValues(count, lambdaParam);
                else list = new ArrayList();
                Iterator> it = leftValueMap.entrySet().iterator();
                while (list.size() != count && it.hasNext()) {
                        Map.Entry entry = it.next();
                        if ((!lambdaParam || !entry.getKey().equals("$"))
                                && CompileUtil.isValidName(entry.getKey())) {
                                list.add(entry.getValue());
                        }
                }
                return list;
        }

        public MethodRecorder getInnerMethod(String name) {
                if (innerMethodMap.containsKey(name)) {
                        return innerMethodMap.get(name);
                } else if (parent == null) {
                        return null;
                } else return parent.getInnerMethod(name);
        }

        public Map getInnerMethods() {
                Map recorders = new HashMap();
                SemanticScope scope = this;
                while (scope != null) {
                        recorders.putAll(scope.innerMethodMap);
                        scope = scope.parent;
                }
                return recorders;
        }

        public boolean containsInnerMethod(String name) {
                return innerMethodMap.containsKey(name);
        }

        public STypeDef type() {
                if (sTypeDef != null)
                        return sTypeDef;
                if (parent != null) return parent.type();
                return null;
        }

        public void setThis(Ins.This aThis) {
                this.aThis = aThis;
        }

        public Ins.This getThis() {
                if (aThis != null)
                        return aThis;
                if (parent != null) return parent.getThis();
                return null;
        }

        public String generateTempName() {
                int i = 0;
                while (getLeftValue("*" + i) != null) ++i;
                return "*" + i;
        }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy