Skip to content

Instantly share code, notes, and snippets.

@colomon
Created October 4, 2011 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colomon/1260760 to your computer and use it in GitHub Desktop.
Save colomon/1260760 to your computer and use it in GitHub Desktop.
diff --git a/lib/CLRBackend.cs b/lib/CLRBackend.cs
index bc0078d..e87aa05 100644
--- a/lib/CLRBackend.cs
+++ b/lib/CLRBackend.cs
@@ -3329,6 +3329,7 @@ namespace Niecza.CLRBackend {
namtypes["cursor"] = Tokens.Cursor;
namtypes["strbuf"] = typeof(StringBuilder);
namtypes["treader"] = typeof(TextReader);
+ namtypes["twriter"] = typeof(TextWriter);
handlers = new Dictionary<string, Func<NamProcessor,object[],CpsOp>>();
thandlers = new Dictionary<string, Func<CpsOp[], CpsOp>>();
@@ -4007,6 +4008,12 @@ dynamic:
thandlers["treader_open"] = delegate(CpsOp[] z) {
return CpsOp.Widen(typeof(TextReader),
CpsOp.ConstructorCall(treader_open, z)); };
+
+ ConstructorInfo twriter_open = typeof(StreamWriter).GetConstructor(new Type[1] { Tokens.String });
+ thandlers["twriter_open"] = delegate(CpsOp[] z) {
+ return CpsOp.Widen(typeof(TextWriter),
+ CpsOp.ConstructorCall(twriter_open, z)); };
+ thandlers["twriter_puts"] = Methody(null, typeof(TextWriter).GetMethod("Write", new Type[] { Tokens.String }));
foreach (KeyValuePair<string, Func<CpsOp[], CpsOp>> kv
in thandlers) {
diff --git a/lib/CORE.setting b/lib/CORE.setting
index 7b77e1b..1661bd5 100644
--- a/lib/CORE.setting
+++ b/lib/CORE.setting
@@ -2004,11 +2004,6 @@ augment class Str {
method IO() { IO.new(path => self) }
}
-my class TextWriter {
- method say(*@bits) { say @bits }
- method print(*@bits) { print @bits }
-}
-
my class TextReader {
method get() {
Q:CgOp {
@@ -2035,6 +2030,21 @@ my class TextReader {
}
}
+my class TextWriter {
+ # sub say(|$c) { Q:CgOp { (rnull (say (obj_getstr {Niecza::gistcat(|$c)}))) }; True }
+
+ method say(|$c) {
+ # $PROCESS::OUTPUT_USED := True;
+ Q:CgOp { (twriter_puts (unbox twriter (@ {self})) (obj_getstr {Niecza::gistcat(|$c)})) }; True
+ }
+
+ method print(*@bits) { print @bits }
+ # method print(|$c) {
+ # # $PROCESS::OUTPUT_USED := True;
+ # Q:CgOp { (rnull (say (obj_getstr {Niecza::gistcat(|$c)}))) }; True
+ # }
+}
+
# the following class stolen from Rakudo, but butchered
class IO::ArgFiles {
has $!args;
@@ -2059,10 +2069,15 @@ class IO::ArgFiles {
}
}
}
-sub open($filename) is unsafe {
+
+multi sub open($filename) is unsafe {
Q:CgOp { (box TextReader (treader_open (obj_getstr {$filename}))) }
}
+multi sub open($filename, :$w) is unsafe {
+ Q:CgOp { (box TextWriter (twriter_open (obj_getstr {$filename}))) }
+}
+
# TODO $*ARGFILES, multi
sub get($handle = $*IN) { $handle.get }
sub lines($filehandle = $*IN) { $filehandle.lines }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment