zoukankan      html  css  js  c++  java
  • 解释器模式

    定义一个语言的文法,并且建立一个解释器来解释该语言中的句子,“语言”是指使用规定格式和语法的代码。

    其中,Context类用于存储解释器之外的一些全局信息;NumberNode类称作终结符表达式;SymbolNode类称作非终结符表达式;非终结符表达式中包含其他非终结符表达式或终结符表达式,非终结符表达式中的interpret方法通常以递归方式执行。

    public interface Node {
        Float interpret(Context context);
    }
     
    public class NumberNode implements Node {
        private String key;
     
        public NumberNode(String key) {
            this.key = key;
        }
     
        @Override
        public Float interpret(Context context) {
            return context.getNode().get(key);
        }
    }
     
    public class SymbolNode implements Node {
        private Node leftNode;
        private Node rightNode;
        private String symbol;
     
        public SymbolNode(Node leftNode, Node rightNode, String symbol) {
            this.leftNode = leftNode;
            this.rightNode = rightNode;
            this.symbol = symbol;
        }
     
        @Override
        public Float interpret(Context context) {
            switch (this.symbol) {
                case "+":
                    return leftNode.interpret(context) + rightNode.interpret(context);
                case "-":
                    return leftNode.interpret(context) - rightNode.interpret(context);
                case "*":
                    return leftNode.interpret(context) * rightNode.interpret(context);
                case "/":
                    return leftNode.interpret(context) / rightNode.interpret(context);
                default:
                    return null;
            }
        }
    }
     
    public class Context {
        private String text;
        private Map<String, Float> node;
     
        public Context(String text) {
            this.text = text;
            node = new LinkedHashMap<>();
        }
     
        public String getText() {
            return text;
        }
     
        public void setText(String text) {
            this.text = text;
        }
     
        public Map<String, Float> getNode() {
            return node;
        }
     
        public void interpret() {
            String[] split = text.split(" ");
            for (String textSplit : split) {
                if (!"+".equals(textSplit) && !"-".equals(textSplit) && !"*".equals(textSplit) && !"/".equals(textSplit)) {
                    node.put(textSplit, new Random().nextFloat());
                }
            }
            Node leftNode = null;
            Node rightNode = null;
            LinkedList<Node> nodeList = new LinkedList<Node>();
            nodeList.push(new NumberNode(split[0]));
            for (int i = 1; i < split.length; i++) {
                if ("+".equals(split[i]) || "-".equals(split[i]) ||"*".equals(split[i]) || "/".equals(split[i])) {
                    leftNode = nodeList.pop();
                    rightNode = new NumberNode(split[i + 1]);
                    nodeList.push(new SymbolNode(leftNode, rightNode, split[i]));
                }
            }
            System.out.println(nodeList.pop().interpret(this));
        }
    }
     
    @Test
    public void interpreterTest() {
        Context context = new Context("number1 * number2 / number3 + number4 - number5");
        context.interpret();
    }

    转载于:https://blog.csdn.net/sinat_32787481/article/details/83448951

  • 相关阅读:
    不支持ie9一下代码
    jquery ajax done 函数 异步调用方法中不能给全局变量赋值的原因及解决办法
    WaitMe是一款使用CSS3来创建加载动画的jQuery插件
    Masked Input这个jQuery插件让用户能够按照预先设定好的固定格式输入数据(如日期、电话号码等)
    Autosize插件允许textarea元素根据录入的内容自动调整元素的高度
    两个列表选项插件bootstrap-duallistbox.js
    jquery滚动插件slimscroll
    modernizr.custom.js应用
    bootbox基于bootstrap的扩展弹窗
    洛谷P3503 [POI2010]KLO-Blocks 单调栈
  • 原文地址:https://www.cnblogs.com/it-deepinmind/p/13411757.html
Copyright © 2011-2022 走看看