예제 #1
0
def get_token_number():
    global token
    if token == '.':
        return get_token_real_number()
    elif token == '0':
        token = input_char()
        check_eof()
        if token == 'x':
            ans = 0
            while True:
                token = input_char()
                check_eof()
                if token.isdigit():
                    ans = ans * 16 + ord(token) - ord('0')
                elif ord('A') <= ord(token.upper()) <= ord('F'):
                    ans = ans * 16 + ord(token.upper()) - ord('A') + 10
                else:
                    return srz('constant', ('integer', ans))
        if token == '.':
            return get_token_real_number()

        return srz('constant', ('integer', 0))
    else:
        ans = ord(token) - ord('0')
        while True:
            token = input_char()
            check_eof()
            if token.isdigit():
                ans = ans * 10 + ord(token) - ord('0')
            else:
                if token == '.':
                    ans = ans + get_token_real_number()['data'][1]
                    return srz('constant', ('real', ans))
                else:
                    return srz('constant', ('integer', ans))
예제 #2
0
def get_token_comment_one_line():
    global token
    while True:
        token = input_char()
        check_eof()
        if token == '\n':
            token = input_char()
            return get_token()
예제 #3
0
def get_token_string():
    global token
    ans = ""
    while True:
        token = input_char()
        check_eof()
        if token == '"':
            token = input_char()
            return srz('constant', ('string', ans))
        ans += token
예제 #4
0
def get_token_comment_multiple_lines():
    global token
    a = 0
    while True:
        token = input_char()
        check_eof()
        if token == '-':
            a += 1
        elif token == '>':
            if a >= 2:
                token = input_char()
                return get_token()
            a = 0
        else:
            a = 0
예제 #5
0
def get_token_char():
    global token
    token = input_char()
    check_eof()
    ans = token
    token = input_char()
    print(ans, token)
    check_eof()
    if token != "'" and not (ans == '\\' and token == 'n'):
        raise errors.ScannerException(errors.SCANNER_EXCEPTION)
    if ans == '\\' and token == 'n':
        ans = '\n'
        token = input_char()
    token = input_char()
    return srz('constant', ('character', ans))
예제 #6
0
def get_token_real_number():
    global token
    ans = 0
    mul = 1
    while True:
        token = input_char()
        check_eof()
        if token.isdigit():
            mul *= 0.1
            ans = ans + (ord(token) - ord('0')) * mul
        else:
            return srz('constant', ('real', ans))
예제 #7
0
def get_token_id():
    global token
    ans = "" + token
    while True:
        token = input_char()

        if token is None or (not token.isdigit() and not is_en_alpha(token)
                             and token != "_"):
            if ans in types:
                return srz('type', ans)
            if ans in key_token_words:
                return srz(ans, None)
            if ans in boolean_consts:
                return srz('constant', ('boolean', 1 if ans == "true" else 0))
            return srz('id', create_or_get(ans))

        ans += token
예제 #8
0
def get_token():
    global token
    while token == ' ' or token == '\n':
        token = input_char()

    if token is None:
        return srz('$', None)

    if token == ':':
        token = input_char()
        if token == '=':
            token = input_char()
            return srz(':=', None)
        return srz(':', None)

    if token.isdigit() or token == '.':
        return get_token_number()

    if token == '\'':
        return get_token_char()

    if token == '"':
        return get_token_string()

    if token == '-':
        token = input_char()
        if token == '-':
            return get_token_comment_one_line()
        else:
            return srz('-', None)

    if token == '/':
        token = input_char()
        if token == '/':
            return get_token_comment_one_line()
        elif token == ' ' or token == '\n':
            return srz('/', None)
        else:
            raise errors.ScannerException(errors.SCANNER_EXCEPTION)

    if token == '<':
        token = input_char()

        if token == '-':
            token = input_char()
            if token == '-':
                return get_token_comment_multiple_lines()
            raise errors.ScannerException(errors.SCANNER_EXCEPTION)
        elif token == ' ' or token == '\n':
            return srz('<', None)
        elif token == '>':
            token = input_char()
            return srz('<>', None)
        elif token == '=':
            token = input_char()
            return srz('<=', None)
        else:
            raise errors.ScannerException(errors.SCANNER_EXCEPTION)

    if token == '>':
        token = input_char()
        if token == '=':
            token = input_char()
            return srz('>=', None)
        elif token == ' ' or token == '\n':
            return srz('>', None)
        else:
            raise errors.ScannerException(errors.SCANNER_EXCEPTION)

    if token == '~':
        token = input_char()
        return srz('~', None)

    if is_en_alpha(token):
        return get_token_id()

    if token == '|':
        token = input_char()
        return srz('bor', None)

    if token == ',':
        token = input_char()
        return srz('comma', None)
    if token in single_signs:
        c = token
        token = input_char()
        return srz(c, None)
    print(token)
    raise errors.ScannerException(errors.NO_VALID_TOKEN)
예제 #9
0
def initiate():
    global token
    token = input_char()