Ejemplo n.º 1
0
Archivo: ASTNode.cs Proyecto: AxFab/amy
 public ASTNode(Token token, ASTOperand operand)
 {
     Token = token;
     Priority = 0;
     Operand = operand;
     Type = operand.Type;
     Operator = ASTOperator.None;
 }
Ejemplo n.º 2
0
Archivo: ASTNode.cs Proyecto: AxFab/amy
 public ASTNode(Token token, ASTType type, long value)
 {
     Token = token;
     Type = type;
     Operator = ASTOperator.None;
     Operand = null;
     IValue = value;
     Computed = true;
 }
Ejemplo n.º 3
0
 public ASTStatement CloseParenthesis(Token token)
 {
     ASTNode node = inFixStack.Pop();
     while (node.Operator != ASTOperator.Parenthesis)
     {
         addPostFixOperator(node);
         node = inFixStack.Pop();
         if (node == null)
         {
             throw new Exception("Too much parenthesis");
         }
     }
     return this;
 }
Ejemplo n.º 4
0
Archivo: ASTNode.cs Proyecto: AxFab/amy
 public ASTNode(Token token, ASTOperator op)
 {
     Token = token;
     Operator = op;
     Priority = Priorities[(int)op];
 }
Ejemplo n.º 5
0
 public static void RegError(Token token, string message)
 {
     Console.ForegroundColor = ConsoleColor.Red;
       Console.WriteLine("\nAt " + token.File + ":" + token.Start.Row + " - " + message, token.Litteral);
       Console.ForegroundColor = ConsoleColor.Gray;
 }
Ejemplo n.º 6
0
        /*
        protected int ReadChar()
        {
        int ch;
        if (lastChar == '\n')
        {
            cursorRow++;
            column = 1;
        }
        else if (lastChar > 0)
        {
            column++;
        }

        if (peekChar >= 0)
        {
            ch = peekChar;
            peekChar = 0;
            lastChar = peekChar;
            return peekChar;
        }

        ch = reader.Read();
        peekChar = -1;
        if (ch <= 0)
        {
            ch = 0;
        }

        if (ch == '\r')
        {
            ch = reader.Read();
        }

        lastChar = ch;
        return ch;
        }

        protected int PeekChar()
        {
        if (peekChar < 0)
        {
            peekChar = ReadChar();
        }

        return peekChar;
        }
        */
        public Token ReadToken()
        {
            Token token = null;
              if (string.IsNullOrEmpty(textData))
            return token;

              // Read delimiters
              if (token == null) {
            TokenDelimiter tkDelSelected = null;
            foreach (TokenDelimiter tkdel in lang.Delimiters) {
              if (textData.StartsWith(tkdel.Start)) {
            if (lang.isOrdered) {
              tkDelSelected = tkdel;
              break;
            } else {
              if (tkDelSelected == null) {
                tkDelSelected = tkdel;

              } else if (tkDelSelected.Start.Length <= tkdel.Start.Length) {

                int idxEndP = tkDelSelected.GetLength(textData);
                int idxEndN = tkdel.GetLength(textData);

                if (idxEndP >= 0 && idxEndN >= 0 && idxEndN < idxEndP) {
                  tkDelSelected = tkdel;
                }
                /*
            else if (idxEndP >= 0 && idxEndN >= 0)
            {
                // tkDelSelected = tkdel;

            }
            else if (idxEndN > idxEndP)
            {
                tkDelSelected = tkdel;
            }*/
              }
            }
              }
            }

            if (tkDelSelected != null) {
              int idxEnd = tkDelSelected.GetLength(textData);
              string value = textData.Substring(0, idxEnd);
              token = new Token()
              {
            Start = position,
            Type = tkDelSelected.Type,
            Litteral = value
              };
            }
              }

              // Read operators
              if (token == null) {
            TokenKeyword tkOprSelected = null;
            foreach (TokenKeyword tkopr in lang.Operators) {
              if (textData.StartsWith(tkopr.Litteral)) {
            if (lang.isOrdered) {
              tkOprSelected = tkopr;
              break;
            } else {
              if (tkOprSelected == null ||
                  tkOprSelected.Litteral.Length < tkopr.Litteral.Length) {
                tkOprSelected = tkopr;
              }
            }
              }
            }

            if (tkOprSelected != null) {
              token = new Token()
              {
            Start = position,
            Type = tkOprSelected.Type,
            Litteral = tkOprSelected.Litteral
              };
            }
              }

              // Read field
              if (token == null) {
            TokenExpr tkExSelected = null;
            foreach (TokenExpr tkEx in lang.Groups) {
              Match m = tkEx.Expression.Match(textData);
              if (m.Index == 0 && m.Length != 0) {
            tkExSelected = tkEx;
            token = new Token()
            {
              Start = position,
              Type = tkEx.Type,
              Litteral = textData.Substring(0, m.Length)
            };
            break;
              }
            }
              }

              if (token == null) {
            string stray = textData.Substring(0, textData.IndexOfAny(Tokenizer.WhiteChars));
            throw new Exception("Lexical error, found stray '" + stray + "' at " + ":" + position.Row);
            // GCC throw new Exception("error: stray '?' in program");
              }

              token.End = ConsumeCharacters(token.Litteral.Length);

              // Keyword
              if (token.Type == 1) {
            foreach (TokenKeyword tkw in lang.Keywords)
              if (tkw.Litteral == token.Litteral) {
            token.Type = tkw.Type;
            break;
              }

              }
              return token;
        }
Ejemplo n.º 7
0
        public ASTStatement PushOperator(Token token, ASTOperator opcode)
        {
            ASTNode node = new ASTNode(token, opcode);
            while (inFixStack.Count > 0 && inFixStack.Peek().Priority > node.Priority)
            {
                ASTNode nd = inFixStack.Pop();
                addPostFixOperator(nd);
            }

            inFixStack.Push(node);
            return this;
        }
Ejemplo n.º 8
0
 public ASTStatement PushOperand(Token token, ASTOperand operand)
 {
     ASTNode node = new ASTNode(token, operand);
     addPostFixOperand(node);
     return this;
 }
Ejemplo n.º 9
0
 public ASTStatement PushInteger(Token token, ASTType type, long value)
 {
     ASTNode node = new ASTNode(token, type, value);
     addPostFixOperand(node);
     return this;
 }
Ejemplo n.º 10
0
 public ASTStatement OpenParenthesis(Token token)
 {
     ASTNode node = new ASTNode(token, ASTOperator.Parenthesis);
     inFixStack.Push(node);
     return this;
 }
Ejemplo n.º 11
0
 public int SetUpMacro(Macro macro, Token token)
 {
     currentMacro_ = macro;
       // currentArgs_ = null;
       // currentIdx_ = 0;
       return macro.Arguments.Count;
 }