Exemple #1
0
        static void Main(string[] args)
        {
            if (args.Length > 1)
            {
                Console.WriteLine("Usage: loxsharp [script]");
            }
            else if (args.Length == 1)
            {
                if (File.Exists(args[0]))
                {
                    RunFile(args[0]);
                }
                else
                {
                    LoxError.ThrowError("[ERROR] The specified script could not be found.");
                    _exitCode = 1;
                }
            }
            else
            {
                RunPrompt();
            }

            Environment.Exit(_exitCode);
        }
Exemple #2
0
        private void ParseString()
        {
            while (Peek() != '"' && !IsAtEnd())
            {
                if (Peek() == '\n')
                {
                    _line++;
                }
                Advance();
            }

            // Unterminated string
            if (IsAtEnd())
            {
                LoxError.ThrowError(_line, "Unterminated string.");
                return;
            }

            // Closed string
            Advance();

            // Trim the surrounding quotes
            int    length = _current - _start;
            string value  = _source.Substring(_start + 1, length - 2);

            // Implementation specific: unescape string, so '\n' etc are supported
            value = Regex.Unescape(value);

            AddToken(TokenType.STRING, value);
        }
Exemple #3
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.ASTERISK); 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_THAN); break;

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

            case '/':
                if (Match('/'))
                {
                    while (Peek() != '\n' && !IsAtEnd())
                    {
                        Advance();
                    }
                }
                else if (Match('*'))
                {
                    int commentDepth = 0;

                    while ((!(Peek() == '*' && PeekNext() == '/') && !IsAtEnd()) || commentDepth > 0)
                    {
                        if (Peek() == '\n')
                        {
                            _line++;
                        }

                        if (Peek() == '/' && PeekNext() == '*')
                        {
                            commentDepth++;
                        }

                        if (commentDepth > 0 && Peek() == '*' && PeekNext() == '/')
                        {
                            commentDepth--;
                        }

                        Advance();
                    }

                    // Eat the closing comment tokens
                    Advance();
                    Advance();
                }
                else
                {
                    AddToken(TokenType.SLASH);
                }
                break;

            case ' ':
            case '\r':
            case '\t':
                break;

            case '\n': _line++; break;

            case '"': ParseString(); break;

            default:
                if (IsDigit(c))
                {
                    ParseNumber();
                }
                else if (IsAlpha(c))
                {
                    ParseIdentifier();
                }
                else
                {
                    LoxError.ThrowError(_line, "Unexpected character.");
                }
                break;
            }
        }