Exemplo n.º 1
0
 public UnitUniLeafMathNode(IMathNode inner, UnitTypes unitType, UnitConverter converter, Enum labelledUnit)
     : base(null)
 {
     this.Inner        = inner;
     this.unitType     = unitType;
     this.converter    = converter;
     this.labelledUnit = labelledUnit;
 }
Exemplo n.º 2
0
        public EvaluationTree(IMathNode root)
        {
            this.Root = root;
            if (this.Root == null)
            {
                this.Root = new NumericMathNode(0);
            }

            this.Root.Evaluate(); // I guess I need to do this once?
        }
Exemplo n.º 3
0
        public void Run()
        {
            while (true)
            {
                //ITokenizer tokenizer = new Tokenizer();
                Parser          parser = new Parser();
                List <FPTToken> tokens = new List <FPTToken>();

                Console.WriteLine("Enter code homie:");
                string userInput = Console.ReadLine();
                var    found     = userInput.Contains("plus");



                IMathNode result = parser.MathParse(userInput);
                Console.WriteLine(result.Evaluate());
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create an operator node based on a character
        /// </summary>
        /// <param name="c">The operator being + - * / or ^</param>
        /// <param name="left">The left part of the operator</param>
        /// <param name="right">The right part of the operator</param>
        /// <returns>Valid operators return IMathNode, null if no operator</returns>
        public IMathNode CreateOperatorNode(string c, IMathNode left, IMathNode right)
        {
            switch (c)
            {
            case "+":
                return(new AdditionBiLeafMathNode(left, right));

            case "-":
                return(new SubtractionBiLeafMathNode(left, right));

            case "*":
                return(new MultiplicationBiLeafMathNode(left, right));

            case "/":
                return(new DivisionBiLeafMathNode(left, right));

            default:
                return(null);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Create an operator node based on a character
        /// </summary>
        /// <param name="c">The operator being + - * / or ^</param>
        /// <param name="left">The left part of the operator</param>
        /// <param name="right">The right part of the operator</param>
        /// <returns>Valid operators return IMathNode, null if no operator</returns>
        public IMathNode CreateOperatorNode(string c, IMathNode left, IMathNode right)
        {
            switch (c)
            {
            case "+":
                return(new AdditionBiLeafMathNode(left, right));

            case "-":
                return(new SubtractionBiLeafMathNode(left, right));

            case "*":
                return(new MultiplicationBiLeafMathNode(left, right));

            case "/":
                return(new DivisionBiLeafMathNode(left, right));

            case "^":
                return(new ExponentBiLeafMathNode(left, right));

            case "&":
                return(new BitwiseAndBiLeafMathNode(left, right));

            case "|":
                return(new BitwiseOrBiLeafMathNode(left, right));

            case "<<":
                return(new BitwiseShiftLeftBiLeafMathNode(left, right));

            case ">>":
                return(new BitwiseShiftRightBiLeafMathNode(left, right));

            case "%":
                return(new ModulusBiLeafMathNode(left, right));

            default:
                return(null);
            }
        }
Exemplo n.º 6
0
        private string read(string text, bool reporting, out IMathNode ast)
        {
            ast = null;

            IEnumerable <ITokenMatch <TokenEnum> > tokens = lexer.ScanText(text);

            ITokenMatch <TokenEnum> err_token = tokens.FirstOrDefault(it => it.Token == TokenEnum.Error);

            if (err_token != null)
            {
                return("Incorrect symbol in input: " + err_token.Text);
            }
            else
            {
                object root = parser.Parse(tokens, new ParserOptions()
                {
                    Trace = reporting
                }).FirstOrDefault();

                if (reporting)
                {
                    foreach (var s in parser.ParseLog)
                    {
                        Console.WriteLine(s + Environment.NewLine);
                    }
                }

                if (!parser.IsSuccessfulParse)
                {
                    return("There were errors while parsing." + Environment.NewLine + String.Join(Environment.NewLine, parser.ErrorMessages));
                }
                else
                {
                    ast = (IMathNode)root;
                    return(null);
                }
            }
        }
Exemplo n.º 7
0
 public SymbolMathNode(IMathNode left, IMathNode right, string symbol)
     : base(left, right, symbol)
 {
 }
Exemplo n.º 8
0
 public BitwiseAndBiLeafMathNode(IMathNode left, IMathNode right)
     : base(left, right, '&')
 {
 }
 public BitwiseShiftLeftBiLeafMathNode(IMathNode left, IMathNode right)
     : base(left, right, "<<")
 {
 }
Exemplo n.º 10
0
 public LogUniLeafMathNode(IMathNode logbase, IMathNode inner)
     : base(null)
 {
     this.Base  = logbase;
     this.Inner = inner;
 }
Exemplo n.º 11
0
 internal CosNode(IMathNode s)
 {
     this.sub = s;
 }
Exemplo n.º 12
0
 public SymbolMathNode(IMathNode left, IMathNode right, char symbol)
     : base(left, right, symbol)
 {
 }
Exemplo n.º 13
0
 internal SqrtNode(IMathNode s)
 {
     this.sub = s;
 }
 public BitwiseShiftRightBiLeafMathNode(IMathNode left, IMathNode right)
     : base(left, right, ">>")
 {
 }
Exemplo n.º 15
0
 public SubtractionBiLeafMathNode(IMathNode left, IMathNode right)
     : base(left, right, '-')
 {
 }
Exemplo n.º 16
0
 public ModulusBiLeafMathNode(IMathNode left, IMathNode right)
     : base(left, right, '%')
 {
 }
Exemplo n.º 17
0
 internal SinNode(IMathNode s)
 {
     this.sub = s;
 }
Exemplo n.º 18
0
 public Add(IMathNode m1, IMathNode m2)
 {
     this.m1 = m1;
     this.m2 = m2;
 }
Exemplo n.º 19
0
 public NegateUniLeafMathNode(IMathNode inner)
     : base(null)
 {
     this.Inner = inner;
 }
Exemplo n.º 20
0
 public DivisionBiLeafMathNode(IMathNode left, IMathNode right)
     : base(left, right, '/')
 {
 }
Exemplo n.º 21
0
 public BitwiseOrBiLeafMathNode(IMathNode left, IMathNode right)
     : base(left, right, '|')
 {
 }
Exemplo n.º 22
0
 internal PowNode(IMathNode s1, IMathNode s2)
 {
     this.sub1 = s1;
     this.sub2 = s2;
 }
Exemplo n.º 23
0
 public BiLeafMathNode(IMathNode left, IMathNode right, object value)
 {
     this.left  = left;
     this.right = right;
     this.value = value;
 }
 public Add(IMathNode m1, IMathNode m2)
 {
     this.m1 = m1;
     this.m2 = m2;
 }
Exemplo n.º 25
0
 public MultiplicationBiLeafMathNode(IMathNode left, IMathNode right)
     : base(left, right, '*')
 {
 }
Exemplo n.º 26
0
 public ExponentBiLeafMathNode(IMathNode left, IMathNode right)
     : base(left, right, '^')
 {
 }
Exemplo n.º 27
0
 public AdditionBiLeafMathNode(IMathNode left, IMathNode right)
     : base(left, right, '+')
 {
 }
Exemplo n.º 28
0
 public StringMathNode(IMathNode inner)
 {
     this.value = inner;
 }
Exemplo n.º 29
0
 public SinhUniLeafMathNode(IMathNode inner)
     : base(null)
 {
     this.Inner = inner;
 }
Exemplo n.º 30
0
 /// <summary>
 /// Creates a new OperationNode, which represents a mathematical expression.
 /// The OperationNode contains the operation, and its branches are the operands.
 /// Each operand can be any node which inherits from MathNode and thus has an evaluate() function.
 /// </summary>
 /// <param name="op"></param>
 /// <param name="left"></param>
 /// <param name="right"></param>
 public OperatorNode(Operation op, IMathNode left, IMathNode right)
 {
     this.op    = op;
     this.left  = left ?? throw new NullReferenceException("Left branch of OperatorNode cannot be null.");
     this.right = right ?? throw new NullReferenceException("Right branch of OperatorNode cannot be null.");
 }