Example #1
0
 public LexToken(LexTokenType type, string content, SourcePosition pos)
 {
     this.Tag      = null;
     this.Position = pos;
     this.Type     = type;
     this.Content  = content;
 }
Example #2
0
 public ASTNode(SourcePosition position, string content, LexTokenType tokenType)
 {
     this.Pos            = position;
     this.Content        = content;
     this.LexicalType    = tokenType;
     this.m_compileHints = new List <CompileHint>();
 }
        public AddressToken(string lexum, LexTokenType token)
        {
            Lexum       = lexum;
            LexToken    = token;
            ResultToken = StreetToken.UNKNOWN;

            switch (LexToken)
            {
            case LexTokenType.ADDRLEX_AMP:
                PossibleTokens = StreetToken.COLLASE;
                ResultToken    = StreetToken.COLLASE;
                break;

            case LexTokenType.ADDRLEX_DASH:
                PossibleTokens = StreetToken.COLLASE;
                ResultToken    = StreetToken.COLLASE;
                break;

            case LexTokenType.ADDRLEX_ONECHAR:
                PossibleTokens = StreetToken.PREDIR | StreetToken.SUFDIR;
                break;

            case LexTokenType.ADDRLEX_TWOCHAR:
                PossibleTokens = StreetToken.PREDIR | StreetToken.SUFDIR | StreetToken.STREETTYPE;
                break;

            case LexTokenType.ADDRLEX_FRACTION:
                PossibleTokens = StreetToken.HOUSE | StreetToken.UNITNUM;
                break;

            case LexTokenType.ADDRLEX_ALPHA:
                PossibleTokens = StreetToken.STREET | StreetToken.STREETTYPE | StreetToken.UNITTYPE;
                break;

            case LexTokenType.ADDRLEX_ALPHANUM:
                PossibleTokens = StreetToken.UNITNUM;
                break;

            case LexTokenType.ADDRLEX_NUM:
                PossibleTokens = StreetToken.HOUSE | StreetToken.STREET | StreetToken.UNITNUM;
                break;

            case LexTokenType.ADDRLEX_ORDINAL:
                PossibleTokens = StreetToken.STREET | StreetToken.HOUSE;
                break;

            default:
                throw new Exception("Internal error");
            }
        }
Example #4
0
        private void ApplyGroup <T>(List <ASTNode> nodes, int i, LexTokenType open, LexTokenType close) where T : IGroupedASTNode
        {
            while (i < nodes.Count)
            {
                if (nodes[i].LexicalType == open)
                {
                    List <ASTNode> sub = new List <ASTNode>();
                    int            j   = i + 1;
                    while (nodes[j].LexicalType != close)
                    {
                        if (nodes[j].LexicalType == open)
                        {
                            int k = j;
                            this.ApplyGroup <T>(nodes, k, open, close);
                            sub.Add(nodes[j]);
                            j++;
                        }
                        else
                        {
                            sub.Add(nodes[j]);
                            j++;
                        }
                    }

                    T groupNode = (T)Activator.CreateInstance(typeof(T), nodes[i].Pos);
                    groupNode.SetNodes(sub);

                    nodes.RemoveRange(i + 1, sub.Count + 1);
                    nodes[i] = groupNode as ASTNode;
                }
                else if (nodes[i] is IGroupedASTNode group)
                {
                    int j = 0;
                    this.ApplyGroup <T>(group.Nodes, j, open, close);
                }

                i++;
            }
        }
Example #5
0
 public TypeDirectiveNode(SourcePosition position, string content, LexTokenType tokenType) : base(position, content, tokenType)
 {
 }
Example #6
0
        private void ApplyOrderOfOperations(List <ASTNode> nodes)  // AKA operator presedence
        {
            IOperatorBehaviour[][] order = new IOperatorBehaviour[][] {
                new IOperatorBehaviour[] { new IndexLookupOperatorBehaviour("[]") },
                new IOperatorBehaviour[] { new MemberAccessBehaviour(".") },
                new IOperatorBehaviour[] { new WordOperatorBehaviour("new", false) },
                new IOperatorBehaviour[] { new UnaryPostOperatorBehaviour("++"), new UnaryPostOperatorBehaviour("--") }, // Not registering properly!
                new IOperatorBehaviour[] { new UnaryPreOperatorBehaviour("!") },
                new IOperatorBehaviour[] { new BinOpBehaviour("*"), new BinOpBehaviour("/") },
                new IOperatorBehaviour[] { new UnaryPreOperatorBehaviour("++"), new UnaryPreOperatorBehaviour("--") },
                new IOperatorBehaviour[] { new BinOpBehaviour("+"), new BinOpBehaviour("-") },
                new IOperatorBehaviour[] {
                    new BinOpBehaviour("<"), new BinOpBehaviour("<="), new BinOpBehaviour(">"), new BinOpBehaviour(">="),
                    new BinOpBehaviour("=="), new BinOpBehaviour("!=")
                },
                new IOperatorBehaviour[] { new BinOpBehaviour("^"), new BinOpBehaviour("|"), new BinOpBehaviour("&") },
                new IOperatorBehaviour[] { new BinOpBehaviour("||"), new BinOpBehaviour("&&") },
                new IOperatorBehaviour[] { new AssignmentBehaviour("=") },
            };

            LexTokenType[] operatorTokenTypes = new LexTokenType[] {
                LexTokenType.Operator,
                LexTokenType.IndexerStart,
                LexTokenType.Keyword,
            };

            for (int i = 0; i < order.Length; i++)
            {
                var ops = order[i];

                int j = 0;
                while (j < nodes.Count)
                {
                    bool anySymbolMatch = ops.Any(x => x.IsOperatorSymbol(nodes[j].Content));
                    if (operatorTokenTypes.Any(x => x == nodes[j].LexicalType) && anySymbolMatch)
                    {
                        var  m    = ops.First(x => x.IsOperatorSymbol(nodes[j].Content));
                        bool pre  = j - 1 >= 0;
                        bool post = j + 1 < nodes.Count;
                        if (m.IsLegalWhen(pre, post) && m.IsLegalPreAndPostCondition(nodes, j))
                        {
                            if (m.ApplyBehaviour(nodes, j, this.ApplyOrderOfOperations))
                            {
                                j += m.GetAdvancement();
                                continue;
                            }
                        }
                    }
                    j++;
                }
            }

            // Run recursively
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i] is IGroupedASTNode groupedNode)
                {
                    this.ApplyOrderOfOperations(groupedNode.Nodes);
                }
            }
        }