~alexreg/mathtexdotnet/0.1

« back to all changes in this revision

Viewing changes to TexDotNet/ExpressionTreeBuilder.cs

  • Committer: Alex Regueiro
  • Date: 2009-09-17 22:26:29 UTC
  • Revision ID: alexreg@gmail.com-20090917222629-d63m1r77gumcvi24
Minor restructuring to parser and ExpressionTreeBuilder. Parser can now handle postfix operators (such as factorial).
Fixed bug with lexer not reading decimal numbers correctly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
        {
20
20
            switch (parseNode.Kind)
21
21
            {
22
 
                case ParseNodeKind.Operation:
23
 
                    return FromOperationParseNode(parseNode);
24
 
                case ParseNodeKind.FunctionCall:
25
 
                    return FromFunctionCallParseNode(parseNode);
 
22
                case ParseNodeKind.PrefixOperator:
 
23
                    return FromPrefixCallParseNode(parseNode);
 
24
                case ParseNodeKind.InfixOperator:
 
25
                    return FromInfixOperatorParseNode(parseNode);
 
26
                case ParseNodeKind.PostfixOperator:
 
27
                    return FromPostfixOperatorParseNode(parseNode);
26
28
                case ParseNodeKind.Token:
27
29
                    return new ExpressionNode(parseNode.Token.Symbol, parseNode.Token.Value);
28
30
                default:
31
33
            }
32
34
        }
33
35
 
34
 
        public static ExpressionNode FromOperationParseNode(ParseNode parseNode)
 
36
        public static ExpressionNode FromPrefixCallParseNode(ParseNode parseNode)
35
37
        {
36
38
            if (parseNode.Children.Count == 1)
37
39
            {
38
40
                return FromParseNode(parseNode.Children[0]);
39
41
            }
40
 
            else if (parseNode.Children.Count == 3)
41
 
            {
42
 
                var node = new ExpressionNode(parseNode.Children[1].Token.Symbol, new[] {
43
 
                    FromParseNode(parseNode.Children[0]), FromParseNode(parseNode.Children[2])});
44
 
                return node;
45
 
            }
46
 
            else
47
 
            {
48
 
                throw new InvalidOperationException(string.Format(
49
 
                    errMsgWrongNumberOfChildren, parseNode.Kind, parseNode.Children.Count));
50
 
            }
51
 
        }
52
 
 
53
 
        public static ExpressionNode FromFunctionCallParseNode(ParseNode parseNode)
54
 
        {
55
 
            if (parseNode.Children.Count >= 2)
 
42
            else if (parseNode.Children.Count >= 2)
56
43
            {
57
44
                var children = new List<ExpressionNode>(parseNode.Children.Count - 1);
58
45
                var arguments = new List<ExpressionNode>(parseNode.Children.Count - 1);
73
60
                    errMsgWrongNumberOfChildren, parseNode.Kind, parseNode.Children.Count));
74
61
            }
75
62
        }
 
63
 
 
64
        public static ExpressionNode FromInfixOperatorParseNode(ParseNode parseNode)
 
65
        {
 
66
            if (parseNode.Children.Count == 1)
 
67
            {
 
68
                return FromParseNode(parseNode.Children[0]);
 
69
            }
 
70
            else if (parseNode.Children.Count == 3)
 
71
            {
 
72
                var node = new ExpressionNode(parseNode.Children[1].Token.Symbol, new[] {
 
73
                    FromParseNode(parseNode.Children[0]), FromParseNode(parseNode.Children[2])});
 
74
                return node;
 
75
            }
 
76
            else
 
77
            {
 
78
                throw new InvalidOperationException(string.Format(
 
79
                    errMsgWrongNumberOfChildren, parseNode.Kind, parseNode.Children.Count));
 
80
            }
 
81
        }
 
82
 
 
83
        public static ExpressionNode FromPostfixOperatorParseNode(ParseNode parseNode)
 
84
        {
 
85
            if (parseNode.Children.Count == 1)
 
86
            {
 
87
                return FromParseNode(parseNode.Children[0]);
 
88
            }
 
89
            else if (parseNode.Children.Count >= 2)
 
90
            {
 
91
                var children = new List<ExpressionNode>(parseNode.Children.Count - 1);
 
92
                ParseNode childParseNode;
 
93
                for (int i = 0; i < parseNode.Children.Count - 1; i++)
 
94
                {
 
95
                    childParseNode = parseNode.Children[i];
 
96
                    children.Add(FromParseNode(childParseNode));
 
97
                }
 
98
                return new ExpressionNode(parseNode.Children[parseNode.Children.Count - 1].Token.Symbol, children);
 
99
            }
 
100
            else
 
101
            {
 
102
                throw new InvalidOperationException(string.Format(
 
103
                    errMsgWrongNumberOfChildren, parseNode.Kind, parseNode.Children.Count));
 
104
            }
 
105
        }
76
106
    }
77
107
}