예제 #1
0
        public static char *_I(char *s)
        {
            var len = strlen(s);

            return(Intern.InternRange(s, s + len));
        }
예제 #2
0
        void next_token()
        {
repeat:
            token.start  = stream;
            token.suffix = 0;
            token.mod    = 0;
            switch (*stream)
            {
            case ' ':
            case '\n':
            case '\r':
            case '\t':
            case '\v':
                while (char.IsWhiteSpace(*stream))
                {
                    if (*stream++ == '\n')
                    {
                        line_start = stream;
                        token.pos.line++;
                    }
                }

                goto repeat;

            case '\'':
                scan_char();
                break;

            case '"':
                scan_str();
                break;

            case '.':
                if (char.IsDigit(stream[1]))
                {
                    scan_float();
                }
                else if (stream[1] == '.' && stream[2] == '.')
                {
                    token.kind = TOKEN_ELLIPSIS;
                    stream    += 3;
                }
                else
                {
                    token.kind = TOKEN_DOT;
                    stream++;
                }

                break;

            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                while (char.IsDigit(*stream))
                {
                    stream++;
                }

                var c = *stream;
                stream = token.start;
                if (c == '.' || char.ToLower(c) == 'e')
                {
                    scan_float();
                }
                else
                {
                    scan_int();
                }

                break;

            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
            case 'g':
            case 'h':
            case 'i':
            case 'j':
            case 'k':
            case 'l':
            case 'm':
            case 'n':
            case 'o':
            case 'p':
            case 'q':
            case 'r':
            case 's':
            case 't':
            case 'u':
            case 'v':
            case 'w':
            case 'x':
            case 'y':
            case 'z':
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
            case 'G':
            case 'H':
            case 'I':
            case 'J':
            case 'K':
            case 'L':
            case 'M':
            case 'N':
            case 'O':
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
            case 'V':
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
            case '_':
                while (char.IsLetterOrDigit(*stream) || *stream == '_')
                {
                    stream++;
                }

                token.name = Intern.InternRange(token.start, stream);
                token.kind = is_keyword_name(token.name) ? TOKEN_KEYWORD : TOKEN_NAME;
                break;

            case '<':
                stream++;
                if (*stream == '<')
                {
                    token.kind = TOKEN_LSHIFT;
                    stream++;
                    if (*stream == '=')
                    {
                        token.kind = TOKEN_LSHIFT_ASSIGN;
                        stream++;
                    }
                }
                else if (*stream == '=')
                {
                    token.kind = TOKEN_LTEQ;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_LT;
                }

                break;

            case '>':
                stream++;
                if (*stream == '>')
                {
                    token.kind = TOKEN_RSHIFT;
                    stream++;
                    if (*stream == '=')
                    {
                        token.kind = TOKEN_RSHIFT_ASSIGN;
                        stream++;
                    }
                }
                else if (*stream == '=')
                {
                    token.kind = TOKEN_GTEQ;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_GT;
                }

                break;

            case '\0':
                token.kind = TOKEN_EOF;
                stream++;
                break;

            case '(':
                token.kind = TOKEN_LPAREN;
                stream++;
                break;

            case ')':
                token.kind = TOKEN_RPAREN;
                stream++;
                break;

            case '{':
                token.kind = TOKEN_LBRACE;
                stream++;
                break;

            case '}':
                token.kind = TOKEN_RBRACE;
                stream++;
                break;

            case '[':
                token.kind = TOKEN_LBRACKET;
                stream++;
                break;

            case ']':
                token.kind = TOKEN_RBRACKET;
                stream++;
                break;

            case ',':
                token.kind = TOKEN_COMMA;
                stream++;
                break;

            case '?':
                token.kind = TOKEN_QUESTION;
                stream++;
                break;

            case ';':
                token.kind = TOKEN_SEMICOLON;
                stream++;
                break;

            case '~':
                token.kind = TOKEN_NEG;
                stream++;
                break;

            case '@':
                token.kind = TOKEN_AT;
                stream++;
                break;

            case '#':
                token.kind = TOKEN_POUND;
                stream++;
                break;


            // CASE2
            case ':':
                stream++;
                if (*stream == '=')
                {
                    token.kind = TOKEN_COLON_ASSIGN;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_COLON;
                }

                break;

            case '!':
                stream++;
                if (*stream == '=')
                {
                    token.kind = TOKEN_NOTEQ;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_NOT;
                }

                break;

            case '=':
                stream++;
                if (*stream == '=')
                {
                    token.kind = TOKEN_EQ;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_ASSIGN;
                }

                break;

            case '^':
                stream++;
                if (*stream == '=')
                {
                    token.kind = TOKEN_XOR_ASSIGN;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_XOR;
                }

                break;

            case '*':
                stream++;
                if (*stream == '=')
                {
                    token.kind = TOKEN_MUL_ASSIGN;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_MUL;
                }

                break;

            case '/':
                if (*++stream == '=')
                {
                    token.kind = TOKEN_DIV_ASSIGN;
                    stream++;
                }
                else if (*stream == '/')
                {
                    stream++;
                    while (*stream != 0 && *stream != '\n')
                    {
                        stream++;
                    }
                    goto repeat;
                }
                else if (*stream == '*')
                {
                    stream++;
                    int level = 1;
                    while (*stream != 0 && level > 0)
                    {
                        if (stream[0] == '/' && stream[1] == '*')
                        {
                            level++;
                            stream += 2;
                        }
                        else if (stream[0] == '*' && stream[1] == '/')
                        {
                            level--;
                            stream += 2;
                        }
                        else
                        {
                            if (*stream == '\n')
                            {
                                token.pos.line++;
                            }
                            stream++;
                        }
                    }
                    goto repeat;
                }
                else
                {
                    token.kind = TOKEN_DIV;
                }

                break;

            case '%':
                stream++;
                if (*stream == '=')
                {
                    token.kind = TOKEN_MOD_ASSIGN;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_MOD;
                }

                break;

            // CASE3 Types
            case '+':
                stream++;
                if (*stream == '=')
                {
                    token.kind = TOKEN_ADD_ASSIGN;
                    stream++;
                }
                else if (*stream == '+')
                {
                    token.kind = TOKEN_INC;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_ADD;
                }

                break;

            case '-':
                stream++;
                if (*stream == '=')
                {
                    token.kind = TOKEN_SUB_ASSIGN;
                    stream++;
                }
                else if (*stream == '-')
                {
                    token.kind = TOKEN_DEC;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_SUB;
                }

                break;

            case '&':
                stream++;
                if (*stream == '=')
                {
                    token.kind = TOKEN_AND_ASSIGN;
                    stream++;
                }
                else if (*stream == '&')
                {
                    token.kind = TOKEN_AND_AND;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_AND;
                }

                break;

            case '|':
                stream++;
                if (*stream == '=')
                {
                    token.kind = TOKEN_OR_ASSIGN;
                    stream++;
                }
                else if (*stream == '|')
                {
                    token.kind = TOKEN_OR_OR;
                    stream++;
                }
                else
                {
                    token.kind = TOKEN_OR;
                }

                break;

            default:
                error_here("Invalid '{0}' token, skipping", *stream);
                stream++;
                goto repeat;
            }

            token.end     = stream;
            token.pos.col = token.start - line_start + 1;
        }
예제 #3
0
        public static char *_I(string s)
        {
            var c = s.ToPtr();

            return(Intern.InternRange(c, c + s.Length));
        }