cvsuser 03/07/08 06:01:29
Modified: examples/assembly cat.pasm slurp.pasm uniq.pasm
. io.ops
languages/bf bf.pasm bfc.imc
t/op hacks.t interp.t macro.t
t/pmc nci.t
Log:
#22899 by Juergen Boemmels; replace some old IO ops
Revision Changes Path
1.2 +5 -3 parrot/examples/assembly/cat.pasm
Index: cat.pasm
===================================================================
RCS file: /cvs/public/parrot/examples/assembly/cat.pasm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- cat.pasm 23 Mar 2002 18:44:22 -0000 1.1
+++ cat.pasm 8 Jul 2003 13:01:19 -0000 1.2
@@ -1,7 +1,9 @@
# Simple cat util to test PIO read/write
# This does not use STDIO
+ getstdin P0
+ getstdout P1
REDO:
- read S0, 256
- puts S0
- branch REDO
+ readline S0, P0
+ print S0
+ if S0, REDO
end
1.2 +3 -2 parrot/examples/assembly/slurp.pasm
Index: slurp.pasm
===================================================================
RCS file: /cvs/public/parrot/examples/assembly/slurp.pasm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- slurp.pasm 23 Mar 2002 21:31:18 -0000 1.1
+++ slurp.pasm 8 Jul 2003 13:01:19 -0000 1.2
@@ -1,14 +1,15 @@
# Slurp a file line by line, concat to S1 and
# at end, print the whole buffer.
+ getstdin P0
set S1, ""
AGAIN:
- read S0, 256
+ readline S0, P0
length I1, S0
le I1, 0, MAINLOOP
concat S1, S0
branch AGAIN
MAINLOOP:
- puts S1
+ print S1
end
1.2 +4 -4 parrot/examples/assembly/uniq.pasm
Index: uniq.pasm
===================================================================
RCS file: /cvs/public/parrot/examples/assembly/uniq.pasm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- uniq.pasm 16 Mar 2003 16:06:50 -0000 1.1
+++ uniq.pasm 8 Jul 2003 13:01:19 -0000 1.2
@@ -1,4 +1,4 @@
-# $Id: uniq.pasm,v 1.1 2003/03/16 16:06:50 dan Exp $
+# $Id: uniq.pasm,v 1.2 2003/07/08 13:01:19 leo Exp $
# uniq - Remove duplicate lines from a sorted file
#
# % ./assemble.pl uniq.pasm -o uniq.pbc
@@ -49,11 +49,11 @@
set I1, 1 # count
# Read the file into S1
- open I0, S0
- readline S2, I0
+ open P1, S0, "<"
+ readline S2, P1
SOURCE_LOOP:
- readline S1, I0
+ readline S1, P1
eq S1, S2, MATCH
1.25 +124 -148 parrot/io.ops
Index: io.ops
===================================================================
RCS file: /cvs/public/parrot/io.ops,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -w -r1.24 -r1.25
--- io.ops 1 Jul 2003 15:41:00 -0000 1.24
+++ io.ops 8 Jul 2003 13:01:21 -0000 1.25
@@ -55,7 +55,6 @@
#ifdef PIO_OS_UNIX
/* These char * need to go away soon */
const char * mode;
- ParrotIO *io;
mode = string_to_cstring(interpreter, $3);
$1 = PIO_fdopen(interpreter, $2, mode);
@@ -73,6 +72,52 @@
goto NEXT();
}
+=item B<getfd>(out INT, in PMC)
+
+Get the file descriptor out of the ParrotIO object $2 and store it in $1
+
+XXX: integral file descriptors may not exist outside of the UNIX
+ platform.
+
+=cut
+
+inline op getfd(out INT, in PMC) {
+ $1 = PIO_getfd(interpreter, $2);
+ goto NEXT();
+}
+
+=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) {
+ $1 = new_io_pmc(interpreter, PIO_STDIN(interpreter));
+ goto NEXT();
+}
+
+inline op getstdout(out PMC) {
+ $1 = new_io_pmc(interpreter, PIO_STDOUT(interpreter));
+ goto NEXT();
+}
+
+inline op getstderr(out PMC) {
+ $1 = new_io_pmc(interpreter, PIO_STDERR(interpreter));
+ goto NEXT();
+}
+
#########################################
=item B<open>(out PMC, in STR, in STR)
@@ -80,6 +125,11 @@
Open URL (file, address, database, in core image) named $2 with
Perl style mode string in $3 and create an IO object in $1.
+=item B<open>(out PMC, in STR)
+
+Open URL (file, address, database, in core image) named $2 with
+read/write mode and create an IO object in $1.
+
=cut
inline op open(out PMC, in STR, in STR) {
@@ -98,63 +148,16 @@
goto NEXT();
}
-=item B<open>(out INT, in STR)
-
-Open file named $2 for reading and writing and save the file
-descriptor into $1.
-
-=item B<open>(out INT, in STR, in STR)
-
-Open file named $2 with flags $3 and mode 0644 (rw-r--r--), and save the file
-descriptor into $1.
-
-=cut
+inline op open(out PMC, in STR) {
+ /* These char * need to go away soon */
+ const char * path;
-op open(out INT, in STR) {
- char *path = string_to_cstring(interpreter, $2);
- PMC *io = PIO_open(interpreter, path, "+<");
- /* string_cstring_free(path); */
- if (io) {
- $1 = PIO_getfd(interpreter, io);
- }
- else {
- $1 = -1;
- }
- goto NEXT();
-}
+ path = string_to_cstring(interpreter, $2);
-op open(out INT, in STR, in STR) {
- char *path = string_to_cstring(interpreter, $2);
- char *mode = string_to_cstring(interpreter, $3);
- PMC *io = PIO_open(interpreter, path, mode);
- /* string_cstring_free(mode); */
+ $1 = PIO_open(interpreter, path, "+<");
/* string_cstring_free(path); */
- if (io) {
- $1 = PIO_getfd(interpreter, io);
- }
- else {
- $1 = -1;
- }
- goto NEXT();
-}
-
-########################################
-
-=item B<close>(inout INT)
-
-Close file opened on file descriptor $1.
-
-=cut
-
-inline op close(inout INT) {
- ParrotIOTable table;
- ParrotIO *io;
-
- if ($1 >= 0) {
- table = ((ParrotIOData*)interpreter->piodata)->table;
- io = table[$1];
- table[$1] = NULL;
- PIO_close(interpreter, new_io_pmc(interpreter, io));
+ if(!$1) {
+ $1 = pmc_new(interpreter, enum_class_PerlUndef);
}
goto NEXT();
}
@@ -171,17 +174,6 @@
Print $1 to standard output.
-=item B<print>(in INT, in INT)
-
-=item B<print>(in INT, in NUM)
-
-=item B<print>(in INT, in STR)
-
-=item B<print>(in INT, in PMC)
-
-Print $2 to the file specified by file descriptor $1; for $1 equal to
-0, 1 or 2, we use stdin, stdout or stderr respectively.
-
=cut
inline op print(in INT) {
@@ -213,113 +205,107 @@
goto NEXT();
}
+##########################################
+
+=item B<printerr>(in INT)
-op print(in INT, in INT) {
- ParrotIO *io = ((ParrotIOData*)interpreter->piodata)->table[$1];
- STRING *s = Parrot_sprintf_c(interpreter, INTVAL_FMT, $2);
- PIO_putps(interpreter, new_io_pmc(interpreter, io), s);
+=item B<printerr>(in NUM)
+
+=item B<printerr>(in STR)
+
+=item B<printerr>(in PMC)
+
+Print $1 to standard error.
+
+=cut
+
+op printerr(in INT) {
+ PIO_eprintf(interpreter, INTVAL_FMT, (double)$1);
goto NEXT();
}
-op print(in INT, in NUM) {
- ParrotIO *io = ((ParrotIOData*)interpreter->piodata)->table[$1];
- STRING *s = Parrot_sprintf_c(interpreter, "%f", (double)$2);
- PIO_putps(interpreter, new_io_pmc(interpreter, io), s);
+op printerr(in NUM) {
+ PIO_eprintf(interpreter, "%f", (double)$1);
goto NEXT();
}
-op print(in INT, in STR) {
- STRING *s = $2;
- ParrotIO *io = ((ParrotIOData*)interpreter->piodata)->table[$1];
+op printerr(in STR) {
+ STRING *s = $1;
if (s && string_length(s)) {
- PIO_putps(interpreter, new_io_pmc(interpreter, io), s);
+ PIO_putps(interpreter, new_io_pmc(interpreter, PIO_STDERR(interpreter)),
+ s);
}
goto NEXT();
}
-op print(in INT, in PMC) {
- PMC *p = $2;
- ParrotIO *io = ((ParrotIOData*)interpreter->piodata)->table[$1];
- STRING *s = (p->vtable->get_string(interpreter, p));
+op printerr(in PMC) {
+ PMC *p = $1;
+ STRING *s = (VTABLE_get_string(interpreter, p));
if (s) {
- PIO_putps(interpreter, new_io_pmc(interpreter, io), s);
- }
- goto NEXT();
+ PIO_putps(interpreter, new_io_pmc(interpreter, PIO_STDOUT(interpreter)),
+ s);
}
-
-op flush(in INT) {
- ParrotIO *io = ((ParrotIOData*)interpreter->piodata)->table[$1];
- PIO_flush(interpreter, new_io_pmc(interpreter, io));
goto NEXT();
}
##########################################
-=item B<print>(in PMC, in STR)
+=item B<print>(in PMC, in INT)
-Print String $2 on the IO stream object $1.
-
-=cut
+=item B<print>(in PMC, in NUM)
-op print(in PMC, in STR) {
- if ($2 && $1) {
- PIO_write(interpreter, $1, ($2)->strstart, string_length($2));
- }
- goto NEXT();
-}
-
-##########################################
+=item B<print>(in PMC, in STR)
-=item B<printerr>(in STR)
+=item B<print>(in PMC, in PMC)
-Print $1 to interp.stderr IO stream (unbuffered)
+Print $2 on the IO stream object $1.
=cut
-op printerr(in STR) {
+op print(in PMC, in INT) {
if ($1) {
- PIO_putps(interpreter, new_io_pmc(interpreter, PIO_STDERR(interpreter)),
- $1);
+ STRING *s = Parrot_sprintf_c(interpreter, INTVAL_FMT, $2);
+ PIO_putps(interpreter, $1, s);
+ }
+ else {
+ /* Handle error here */
}
goto NEXT();
}
-########################################
-
-=item B<puts>(in STR)
-
-=item B<puts>(in INT)
-
-=item B<puts>(in NUM)
-
-Print $1 to standard output stream
-This will go away when print ops are all migrated to
-use ParrotIO instead of STDIO. Right now ParrotIO is
-not stable enough to replace STDIO.
-
-=cut
-
-op puts(in STR) {
+op print(in PMC, in NUM) {
if ($1) {
- PIO_putps(interpreter, new_io_pmc(interpreter, PIO_STDOUT(interpreter)),
- $1);
+ STRING *s = Parrot_sprintf_c(interpreter, "%f", (double)$2);
+ PIO_putps(interpreter, $1, s);
+ }
+ else {
+ /* Handle error here */
}
goto NEXT();
}
-op puts(in INT) {
- STRING * s = string_from_int(interpreter, $1);
- PIO_putps(interpreter, new_io_pmc(interpreter, PIO_STDOUT(interpreter)), s);
+op print(in PMC, in STR) {
+ if ($2 && $1) {
+ PIO_putps(interpreter, $1, $2);
+ }
+ else {
+ /* Handle error here */
+ }
goto NEXT();
}
-op puts(in NUM) {
- STRING * s = Parrot_sprintf_c(interpreter, "%f", $1);
- PIO_putps(interpreter, new_io_pmc(interpreter, PIO_STDOUT(interpreter)), s);
+op print(in PMC, in PMC) {
+ if ($2 && $1) {
+ STRING *s = VTABLE_get_string(interpreter, $2);
+ PIO_putps(interpreter, $1, s);
+ }
+ else {
+ /* Handle error here */
+ }
goto NEXT();
}
-########################################
+##########################################
=item B<read>(out STR, in INT)
@@ -372,7 +358,7 @@
goto NEXT();
}
-=item B<readline>(out STR, in INT)
+=item B<readline>(out STR, in PMC)
Read a line up to EOL from filehandle $2.
This switches the filehandle to linebuffer-mode.
@@ -381,18 +367,15 @@
=cut
-inline op readline(out STR, in INT) {
+inline op readline(out STR, in PMC) {
ParrotIO *io;
size_t len = 0;
$1 = string_make(interpreter, NULL, 65535, NULL, 0, NULL);
memset(($1)->strstart, 0, 65535);
- if ($2 >= 0) {
- PMC *pmc;
- io = ((ParrotIOData*)interpreter->piodata)->table[$2];
- pmc = new_io_pmc(interpreter, io);
- PIO_setlinebuf(interpreter, pmc);
- len = PIO_read(interpreter, pmc, ($1)->strstart, 65534);
+ if ($2) {
+ PIO_setlinebuf(interpreter, $2);
+ len = PIO_read(interpreter, $2, ($1)->strstart, 65534);
($1)->strlen = ($1)->bufused = len;
}
goto NEXT();
@@ -428,14 +411,7 @@
goto NEXT();
}
-
########################################
-
-
-
-
-
-
=back
1.2 +7 -6 parrot/languages/bf/bf.pasm
Index: bf.pasm
===================================================================
RCS file: /cvs/public/parrot/languages/bf/bf.pasm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- bf.pasm 9 Dec 2002 04:00:27 -0000 1.1
+++ bf.pasm 8 Jul 2003 13:01:23 -0000 1.2
@@ -1,4 +1,4 @@
-# $Id: bf.pasm,v 1.1 2002/12/09 04:00:27 sfink Exp $
+# $Id: bf.pasm,v 1.2 2003/07/08 13:01:23 leo Exp $
# A Brainfuck interpreter
# By Leon Brocard <[EMAIL PROTECTED]>
#
@@ -16,12 +16,12 @@
# Read the file into S1
SOURCE:
- open I0, S0
+ open P1, S0, "<"
SOURCE_LOOP:
- readline S2, I0
+ readline S2, P1
concat S1, S2
if S2, SOURCE_LOOP
- close I0
+ close P1
length I30, S1
@@ -101,7 +101,8 @@
NOTDOT:
ne S0, ",", NEXT
- readline S31, 0
+ getstdin P30
+ readline S31, P30
ord I2, S31
set P0[I1], I2
branch NEXT
1.3 +2 -1 parrot/languages/bf/bfc.imc
Index: bfc.imc
===================================================================
RCS file: /cvs/public/parrot/languages/bf/bfc.imc,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -w -r1.2 -r1.3
--- bfc.imc 8 Jul 2003 09:44:39 -0000 1.2
+++ bfc.imc 8 Jul 2003 13:01:23 -0000 1.3
@@ -1,4 +1,4 @@
-# $Id: bfc.imc,v 1.2 2003/07/08 09:44:39 leo Exp $
+# $Id: bfc.imc,v 1.3 2003/07/08 13:01:23 leo Exp $
# A Brainfuck compiler
# By Leon Brocard <[EMAIL PROTECTED]>
#
@@ -148,7 +148,8 @@
NOTDOT:
if char != "," goto NEXT
- code = code . "readline S31, 0\n"
+ code = code . "getstdin P30\n"
+ code = code . "readline S31, P30\n"
code = code . "ord I2, S31\n"
code = code . "set P0[I1], I2\n"
goto NEXT
1.8 +41 -28 parrot/t/op/hacks.t
Index: hacks.t
===================================================================
RCS file: /cvs/public/parrot/t/op/hacks.t,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -w -r1.7 -r1.8
--- hacks.t 20 May 2003 11:11:11 -0000 1.7
+++ hacks.t 8 Jul 2003 13:01:26 -0000 1.8
@@ -1,6 +1,6 @@
#! perl -w
-use Parrot::Test tests => 9;
+use Parrot::Test tests => 10;
use Test::More;
# It would be very embarrassing if these didn't work...
@@ -8,11 +8,11 @@
print FOO "2\n1\n";
close FOO;
output_is(<<'CODE', <<'OUTPUT', "open and readline");
- open I0, "temp.file"
+ open P0, "temp.file"
set S0, ""
set S1, ""
- readline S0, I0
- readline S1, I0
+ readline S0, P0
+ readline S1, P0
print S1
print S0
end
@@ -31,16 +31,16 @@
new P0, .PerlString
set P0, "Bar\n"
- open I1, "temp.file"
- print I1, I0
- print I1, N0
- print I1, S0
- print I1, P0
- close I1
-
- open I2, "temp.file"
- readline S1, I2
- close I2
+ open P1, "temp.file"
+ print P1, I0
+ print P1, N0
+ print P1, S0
+ print P1, P0
+ close P1
+
+ open P2, "temp.file"
+ readline S1, P2
+ close P2
print S1
end
@@ -53,13 +53,13 @@
# This one passes, but for the wrong reason
output_is(<<'CODE', <<'OUTPUT', "3-arg open");
- open I1, "temp.file", "<"
+ open P1, "temp.file", "<"
print "Foobar\n"
- close I1
+ close P1
- open I3, "temp.file", "r"
- readline S1, I3
- close I3
+ open P3, "temp.file", "<"
+ readline S1, P3
+ close P3
print S1
end
@@ -70,9 +70,9 @@
unlink("temp.file");
output_is(<<'CODE', <<'OUTPUT', 'open and close');
- open I1, "temp.file"
- print I1, "Hello, World!\n"
- close I1
+ open P1, "temp.file"
+ print P1, "Hello, World!\n"
+ close P1
print "done\n"
end
CODE
@@ -89,9 +89,9 @@
close FOO;
output_is(<<'CODE', '', 'append');
- open I1, "temp.file", ">>"
- print I1, "Parrot flies\n"
- close I1
+ open P1, "temp.file", ">>"
+ print P1, "Parrot flies\n"
+ close P1
end
CODE
@@ -105,9 +105,9 @@
close FOO;
output_is(<<'CODE', '', 'write to file');
- open I1, "temp.file", ">"
- print I1, "Parrot overwrites\n"
- close I1
+ open P1, "temp.file", ">"
+ print P1, "Parrot overwrites\n"
+ close P1
end
CODE
@@ -118,5 +118,18 @@
OUTPUT
unlink("temp.file");
+
+output_is(<<'CODE', '012', 'standard file descriptors');
+ getstdin P0
+ getfd I0, P0
+ print I0
+ getstdout P1
+ getfd I1, P1
+ print I1
+ getstderr P2
+ getfd I2, P2
+ print I2
+ end
+CODE
1; # HONK
1.10 +5 -5 parrot/t/op/interp.t
Index: interp.t
===================================================================
RCS file: /cvs/public/parrot/t/op/interp.t,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -w -r1.9 -r1.10
--- interp.t 28 Jun 2003 11:25:24 -0000 1.9
+++ interp.t 8 Jul 2003 13:01:26 -0000 1.10
@@ -19,20 +19,20 @@
ending
OUTPUT
output_like(<<'CODE', <<'OUTPUT', "restart trace");
- print 2, "ok 1\n"
+ printerr "ok 1\n"
set I0, 1
trace I0
- print 2, "ok 2\n"
+ printerr "ok 2\n"
dec I0
trace I0
- print 2, "ok 3\n"
+ printerr "ok 3\n"
end
CODE
/^ok\s1\n
-(?:PC=8.*)?\n
+(?:PC=7.*)?\n
ok\s2\n
+(?:PC=9.*)?\n
(?:PC=11.*)?\n
-(?:PC=13.*)?\n
ok\s3\n$/x
OUTPUT
1.13 +2 -4 parrot/t/op/macro.t
Index: macro.t
===================================================================
RCS file: /cvs/public/parrot/t/op/macro.t,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -w -r1.12 -r1.13
--- macro.t 3 Jul 2003 11:15:19 -0000 1.12
+++ macro.t 8 Jul 2003 13:01:26 -0000 1.13
@@ -192,13 +192,11 @@
OUTPUT
##############################
-output_is(<<'CODE', <<'OUT', "find file in runtime includes");
+output_is(<<'CODE', '1', "find file in runtime includes");
.include "stdio.pasm"
- print .PIO_STDOUT_FILENO, "ok\n"
+ print .PIO_STDOUT_FILENO
end
CODE
-ok
-OUT
open FOO, ">macro.tempfile"; # Clobber previous
close FOO;
1.12 +7 -7 parrot/t/pmc/nci.t
Index: nci.t
===================================================================
RCS file: /cvs/public/parrot/t/pmc/nci.t,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -w -r1.11 -r1.12
--- nci.t 23 May 2003 08:02:17 -0000 1.11
+++ nci.t 8 Jul 2003 13:01:29 -0000 1.12
@@ -182,9 +182,9 @@
output_is(gen_test(<<'CODE'), <<'OUTPUT', "nci_i_t");
loadlib P1, "libnci.so"
- print 2, "loaded\n"
+ printerr "loaded\n"
dlfunc P0, P1, "nci_it", "it"
- print 2, "dlfunced\n"
+ printerr "dlfunced\n"
set I0, 1 # prototype used - unchecked
set I1, 0 # items on stack - unchecked
set S5, "ko\n"
@@ -195,13 +195,13 @@
ne I2, 0, nok_2
ne I3, 0, nok_2
ne I4, 0, nok_2
- print 2, "ok 2\n"
+ printerr "ok 2\n"
end
-nok_1: print 2, "nok 1\n"
- print 2, I5
- print 2, "\n"
+nok_1: printerr "nok 1\n"
+ printerr I5
+ printerr "\n"
end
-nok_2: print 2, "nok 2\n"
+nok_2: printerr "nok 2\n"
end
CODE
loaded