Exemple #1
0
 // === Entry and Evaluation Control Methods ===
 public void interpret(List <Stmt> statements)
 {
     try
     {
         foreach (Stmt statement in statements)
         {
             execute(statement);
         }
     }
     catch (RuntimeError e)
     {
         AutonoCy_Main.runtimeError(e);
     }
 }
Exemple #2
0
 public override object CALL(Interpreter interpreter, List <object> arguments)
 {
     if (arguments[0] is string)
     {
         try
         {
             return(Double.Parse((string)arguments[0]));
         }
         catch (FormatException e)
         {
             AutonoCy_Main.runtimeError(new RuntimeError(new Token(TokenTypes.IDENTIFIER, "stringToNumber", null, -1), "stringToNumber - Unexpected formatting"));
             return(null);
         }
     }
     else
     {
         throw new RuntimeError(new Token(TokenTypes.IDENTIFIER, "stringToNumber", null, -1), "stringToNumber - unexpected argument type '" + arguments[0].GetType().ToString() + "', expecting type 'string'");
     }
 }
Exemple #3
0
 private ParseError error(Token token, string message)
 {
     AutonoCy_Main.error(token, message);
     return(new ParseError());
 }
Exemple #4
0
        void scanToken()
        {
            char c = advance();

            switch (c)
            {
            // Single character lexemes
            case '(': addToken(TokenTypes.LEFT_PAREN); break;

            case ')': addToken(TokenTypes.RIGHT_PAREN); break;

            case '{': addToken(TokenTypes.LEFT_BRACE); break;

            case '}': addToken(TokenTypes.RIGHT_BRACE); break;

            case '[': addToken(TokenTypes.LEFT_BRACKET); break;

            case ']': addToken(TokenTypes.RIGHT_BRACKET); break;

            case ',': addToken(TokenTypes.COMMA); break;

            case '.': addToken(TokenTypes.DOT); break;

            case '-': addToken(TokenTypes.MINUS); break;

            case '+': addToken(TokenTypes.PLUS); break;

            case ';': addToken(TokenTypes.SEMICOLON); break;

            case '*': addToken(TokenTypes.STAR); break;

            case '^': addToken(TokenTypes.CARET); break;

            // Possibly double character lexemes
            case '!': addToken(match('=') ? TokenTypes.BANG_EQUAL : TokenTypes.BANG); break;

            case '=': addToken(match('=') ? TokenTypes.EQUAL_EQUAL : TokenTypes.EQUAL); break;

            case '<': addToken(match('=') ? TokenTypes.LESS_EQUAL : TokenTypes.LESS); break;

            case '>': addToken(match('=') ? TokenTypes.GREATER_EQUAL : TokenTypes.GREATER); break;

            case '&':
                if (match('&'))
                {
                    addToken(TokenTypes.AND);
                }
                else
                {
                    AutonoCy_Main.error(line, "Unsupported operator. Did you mean '&&'?");
                }
                break;

            case '|':
                if (match('|'))
                {
                    addToken(TokenTypes.OR);
                }
                else
                {
                    AutonoCy_Main.error(line, "Unsupported operator. Did you mean '||'?");
                }
                break;

            // Longer lexemes
            case '/':
                if (match('/'))
                {
                    // Ignore line because comment
                    while (peek() != '\n' && !isAtEnd())
                    {
                        advance();
                    }
                }
                else
                {
                    addToken(TokenTypes.SLASH);
                }
                break;

            // Whitespaces
            case ' ':
            case '\r':
            case '\t':
                // Ignore these
                break;

            case '\n':
                // Increment line count, but don't do anything else
                line++;
                break;

            // String Literal
            case '"': stringL(); break;

            default:
                // Catch numbers here
                if (isDigit(c))
                {
                    numberL();
                }
                // Catch all Alphabetical things here
                else if (isAlpha(c))
                {
                    identifierL();
                }
                else
                {
                    // Error from unexpected character
                    AutonoCy_Main.error(line, "Unexpected character.");
                }
                break;
            }
        }
Exemple #5
0
        void stringL()
        {
            char   next;
            string stringLiteral = "";
            bool   escape        = false;

            next = peek();
            while (next != '"' && !isAtEnd())
            {
                if (next == '\\')
                {
                    escape = true;
                }
                if (next == '\n')
                {
                    line++;
                }

                if (!escape)
                {
                    stringLiteral = stringLiteral + next;
                }
                else
                {
                    if (!isAtEnd())
                    {
                        advance();
                        next = peek();
                        switch (next)
                        {
                        case 'n':
                            stringLiteral = stringLiteral + '\n';
                            break;

                        case '"':
                            stringLiteral = stringLiteral + '"';
                            break;

                        case '\\':
                            stringLiteral = stringLiteral + '\\';
                            break;

                        default:
                            AutonoCy_Main.error(line, "Unrecognized escape sequence.");
                            break;
                        }
                        escape = false;
                    }
                }

                advance();
                next = peek();
            }

            if (isAtEnd())
            {
                AutonoCy_Main.error(line, "Unterminated string.");
                return;
            }

            // Move past the closing "
            advance();

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

            addToken(TokenTypes.STRING, stringLiteral);
        }