Scopes.java 2.71 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.forms.formcalc.support;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class Scopes {
    public static final Frame FUNCTION_DECLARATION = new Frame("FUNCTION_DECLARATION");
    public static final Frame LAST_EXPRESSION_IN_BLOCK = new Frame("LAST_STATEMENT_IN_BLOCK");
    public static final Frame ASSIGNMENT_EXPRESSION = new Frame("ASSIGNMENT_STATEMENT");
    public static final Frame FRAGMENT = new Frame("ASSIGNMENT_STATEMENT");
    private Stack<Frame> frameStack = new Stack();

    public Scopes() {
        this.enter("GLOBAL");
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void runInScope(Object block, Action action) {
        try {
            this.enter(block);
            action.call();
        }
        finally {
            this.exit(block);
        }
    }

    public boolean generateReturnStatement() {
        boolean lastStatement = false;
        for (int i = this.frameStack.size() - 1; i >= 0; --i) {
            Frame f = this.frameStack.get(i);
            if (f != LAST_EXPRESSION_IN_BLOCK) {
                if (f != FUNCTION_DECLARATION) break;
                return lastStatement;
            }
            lastStatement = true;
        }
        return false;
    }

    void enter(Object block) {
        if (block instanceof Frame) {
            this.frameStack.push((Frame)block);
        } else {
            this.frameStack.push(new ScopeFrame(block));
        }
    }

    void exit(Object block) {
        this.frameStack.pop();
    }

    public String lookupFunction(String var) {
        return null;
    }

    static class ScopeFrame
    extends Frame {
        private Map<String, String> variables = new HashMap<String, String>();
        private Map<String, String> functions = new HashMap<String, String>();

        public ScopeFrame(Object block) {
            super(block);
        }

        public void defineVariable(String name) {
            this.variables.put(name.toLowerCase(), name);
        }

        public void defineFunction(String name) {
            this.functions.put(name.toLowerCase(), name);
        }

        public String lookupVariable(String name) {
            return this.variables.get(name.toLowerCase());
        }

        public String lookupFunction(String name) {
            return this.functions.get(name.toLowerCase());
        }

        public Object getBlock() {
            return this.block;
        }
    }

    public static class Frame {
        protected Object block;

        public Frame(Object block) {
            this.block = block;
        }
    }

    public static interface Action {
        public void call();
    }

}