Skip to content
This repository
tree: 11d0b5dd86
Fetching contributors…

Cannot retrieve contributors at this time

file 234 lines (173 sloc) 5.145 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
/*
 * Copyright (C) 2002-2008, Parrot Foundation.
** io.ops
*/

BEGIN_OPS_PREAMBLE
#include "../io/io_private.h"
END_OPS_PREAMBLE


=head1 NAME

io.ops - I/O Opcodes

=cut

=head1 DESCRIPTION

Parrot's IO API

When making changes to any ops file, run C<make bootstrap-ops> to regenerate
all generated ops files.

=cut

###############################################################################

=head2 Parrot IO API Operations

=over 4

=cut

########################################

=item B<print>(in INT)

=item B<print>(in NUM)

=item B<print>(invar PMC)

=item B<print>(in STR)

Print $1 to standard output.

=cut

inline op print(in INT) :base_io {
    char buf[128];
#ifdef PARROT_HAS_SNPRINTF
    snprintf(buf, 128, INTVAL_FMT, (INTVAL)$1);
#else
    sprintf(buf, INTVAL_FMT, (INTVAL)$1);
#endif
    (void)Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
}

inline op print(in NUM) :base_io {
    char buf[128];
#ifdef PARROT_HAS_SNPRINTF
    snprintf(buf, 128, FLOATVAL_FMT, $1);
#else
    sprintf(buf, FLOATVAL_FMT, $1);
#endif
#ifdef PARROT_HAS_NEGATIVE_ZERO
    (void)Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
#else
    if (!Parrot_is_nzero(NREG(1)))
        (void)Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
    else
        /* Workaround for older msvcrt and openbsd. GH #366 */
        (void)Parrot_io_write_b(interp, _PIO_STDOUT(interp), "-0", 2);
#endif
}

op print(in STR) :base_io {
    STRING * const s = $1;
    if (s && Parrot_str_byte_length(interp, s))
        Parrot_io_putps(interp, _PIO_STDOUT(interp), s);
}

op print(invar PMC) :base_io {
    PMC * const p = $1;
    STRING * const s = (VTABLE_get_string(interp, p));
    if (s)
        Parrot_io_putps(interp, _PIO_STDOUT(interp), s);
}

=item B<say>(in INT)

=item B<say>(in NUM)

=item B<say>(invar PMC)

=item B<say>(in STR)

Print $1 to standard output with a trailing newline.

=cut

inline op say(in INT) :base_io {
    char buf[128];
#ifdef PARROT_HAS_SNPRINTF
    snprintf(buf, 128, INTVAL_FMT "\n", (INTVAL)$1);
#else
    sprintf(buf, INTVAL_FMT "\n", (INTVAL)$1);
#endif
    (void)Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
}

inline op say(in NUM) :base_io {
    char buf[128];
    size_t ignore;
#ifdef PARROT_HAS_SNPRINTF
    snprintf(buf, 128, FLOATVAL_FMT "\n", $1);
#else
    sprintf(buf, 128, FLOATVAL_FMT, $1);
#endif
#ifdef PARROT_HAS_NEGATIVE_ZERO
    (void)Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
#else
    if (!Parrot_is_nzero(NREG(1)))
        (void)Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
    else
        /* Workaround for older msvcrt and openbsd. GH #366 */
        (void)Parrot_io_write_b(interp, _PIO_STDOUT(interp), "-0\n", 2);
#endif
}

op say(in STR) :base_io {
    STRING * const s = $1;
    if (s && Parrot_str_byte_length(interp, s))
        Parrot_io_putps(interp, _PIO_STDOUT(interp), s);
    Parrot_io_putps(interp, _PIO_STDOUT(interp), Parrot_str_new_constant(interp, "\n"));
}

op say(invar PMC) :base_io {
    PMC * const p = $1;

    if (PMC_IS_NULL(p)) {
        opcode_t *handler = Parrot_ex_throw_from_op_args(interp, expr NEXT(),
             EXCEPTION_UNEXPECTED_NULL,
            "Null PMC in say");
        goto ADDRESS(handler);
    }
    else {
        STRING * const s = VTABLE_get_string(interp, p);
        if (s)
            Parrot_io_putps(interp, _PIO_STDOUT(interp), s);
        Parrot_io_putps(interp, _PIO_STDOUT(interp), Parrot_str_new_constant(interp, "\n"));
    }
}




##########################################

=item B<print>(invar PMC, in INT)

=item B<print>(invar PMC, in NUM)

=item B<print>(invar PMC, in STR)

=item B<print>(invar PMC, invar PMC)

Print $2 on the IO stream object $1.

=cut

op print(invar PMC, in INT) :base_io {
    if ($1) {
        STRING * const s = Parrot_str_from_int(interp, $2);
        Parrot_io_putps(interp, $1, s);
    }
}

op print(invar PMC, in NUM) :base_io {
    if ($1) {
        STRING * const s = Parrot_sprintf_c(interp, FLOATVAL_FMT, $2);
        Parrot_io_putps(interp, $1, s);
    }
}

op print(invar PMC, in STR) :base_io {
    if ($2 && $1) {
        Parrot_io_putps(interp, $1, $2);
    }
}

op print(invar PMC, invar PMC) :base_io {
    if ($2 && $1) {
        STRING * const s = VTABLE_get_string(interp, $2);
        Parrot_io_putps(interp, $1, s);
    }
}

=item B<getstdin>(out PMC)

Create a new ParrotIO object for the stdin file descriptor and
store it in $1

=item B<getstdout>(out PMC)

Create a new ParrotIO object for the stdout file descriptor and
store it in $1

=item B<getstderr>(out PMC)

Create a new ParrotIO object for the stderr file descriptor and
store it in $1

=cut

inline op getstdin(out PMC) :base_io {
    $1 = _PIO_STDIN(interp);
}

inline op getstdout(out PMC) :base_io {
    $1 = _PIO_STDOUT(interp);
}

inline op getstderr(out PMC) :base_io {
    $1 = _PIO_STDERR(interp);
}

###############################################################################

/*
 * Local variables:
 * c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
 */
Something went wrong with that request. Please try again.