Пример #1
0
        private void LexString()
        {
            while (Peek() != '"' && !IsAtEnd())
            {
                if (Peek() == '\n')
                {
                    line++;
                }
                Advance();
            }

            // Unterminated string.
            if (IsAtEnd())
            {
                VenaError.LexicalError(line, "Unterminated string.");
                return;
            }

            // The closing ".
            Advance();

            // Trim the surrounding quotes.
            String value = source.Substring(start + 1, (current - 1) - (start + 1));

            AddToken(TokenType.STRING, value);
        }
Пример #2
0
 public bool Define(Token name, VType type)
 {
     if (!Values.TryAdd(name.Lexeme, type))
     {
         string definedType = Enum.GetName(typeof(VType), Values[name.Lexeme]);
         VenaError.ParseError(name, $"Variable '{name.Lexeme}' already defined with type '{definedType}'.");
         return(false);
     }
     return(true);
 }
Пример #3
0
        public bool ValidAssignment(Token name, VType assignType)
        {
            // Check to see if the token has already been defined.
            if (!Values.ContainsKey(name.Lexeme))
            {
                VenaError.ParseError(name, $"Undefined variable '{name.Lexeme}'.");
                return(false);
            }

            // Check to see if the defined token matches the assignee type.
            if (Values[name.Lexeme] == assignType)
            {
                return(true);
            }

            VenaError.ParseError(name,
                                 $"Assigning type '{Enum.GetName(typeof(VType), assignType)}' to already defined type '{Enum.GetName(typeof(VType), Values[name.Lexeme])}'.");
            return(false);
        }
Пример #4
0
        private void ScanToken()
        {
            char c = Advance();

            switch (c)
            {
            case '(': AddToken(TokenType.LEFT_PAREN); break;

            case ')': AddToken(TokenType.RIGHT_PAREN); break;

            case '{': AddToken(TokenType.LEFT_BRACE); break;

            case '}': AddToken(TokenType.RIGHT_BRACE); break;

            case ',': AddToken(TokenType.COMMA); break;

            case '.': AddToken(TokenType.DOT); break;

            case '-': AddToken(TokenType.MINUS); break;

            case '+': AddToken(TokenType.PLUS); break;

            case ';': AddToken(TokenType.SEMICOLON); break;

            case '*': AddToken(TokenType.STAR); break;

            case '%': AddToken(TokenType.PERCENT); break;

            case '!': AddToken(Match('=') ? TokenType.BANG_EQUAL : TokenType.BANG); break;

            case '=': AddToken(Match('=') ? TokenType.EQUAL_EQUAL : TokenType.EQUAL); break;

            case '<': AddToken(Match('=') ? TokenType.LESS_EQUAL : TokenType.LESS); break;

            case '>': AddToken(Match('=') ? TokenType.GREATER_EQUAL : TokenType.GREATER); break;

            // Comments
            case '/':
                if (Match('/'))
                {
                    // A comment goes until the end of the line.
                    while (Peek() != '\n' && !IsAtEnd())
                    {
                        Advance();
                    }
                }
                else
                {
                    AddToken(TokenType.SLASH);
                }
                break;

            // Ignore whitespace.
            case ' ':
            case '\r':
            case '\t':
                break;

            case '\n':
                if (column == 2)
                {
                    AddToken(TokenType.NEW_LINE);
                }
                line++;
                column = 0;
                break;

            // Strings
            case '"': LexString(); break;

            default:
                if (IsDigit(c))
                {
                    Number();
                }
                else if (IsAlpha(c))
                {
                    Identifier();
                }
                else
                {
                    VenaError.LexicalError(line, "Unexpected character.");
                }
                break;
            }
        }
Пример #5
0
 ParseError Error(Token token, String message)
 {
     VenaError.ParseError(token, message);
     return(new ParseError());
 }