Skip to content

Commit

Permalink
Fix #160: WriteString["stdout", ...] support
Browse files Browse the repository at this point in the history
  • Loading branch information
Cory Walker authored and Cory Walker committed Sep 2, 2019
1 parent 9690846 commit 0a346d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -4,6 +4,8 @@ y.output
test_rubi
*.expred
.DS_Store
__debug_bin
.vscode

.idea/expreduce.iml

Expand Down
9 changes: 8 additions & 1 deletion expreduce/builtin_system.go
Expand Up @@ -234,14 +234,21 @@ func applyModuleFn(this expreduceapi.ExpressionInterface, es expreduceapi.EvalSt
}

func parseOutputStream(outputStreamDef expreduceapi.Ex) (string, int64, bool) {
// Handle string-only stream names like "stdout".
streamName, ok := outputStreamDef.(*atoms.String)
if ok {
// Use -1 as a placeholder for a missing index.
return streamName.GetValue(), -1, true
}

outputStream, isOutputStream := atoms.HeadAssertion(outputStreamDef, "System`OutputStream")
if !isOutputStream {
return "", -1, false
}
if outputStream.Len() != 2 {
return "", -1, false
}
streamName, ok := outputStream.GetPart(1).(*atoms.String)
streamName, ok = outputStream.GetPart(1).(*atoms.String)
if !ok {
return "", -1, false
}
Expand Down
11 changes: 11 additions & 0 deletions expreduce/streammanager/streammanager.go
Expand Up @@ -29,9 +29,20 @@ func NewStreamManager() expreduceapi.StreamManager {
return &sm
}

func (sm streamManagerImpl) fillMissingIndex(key *streamKey) {
if key.id == -1 {
if key.name == "stdout" {
key.id = 1
} else if key.name == "stdin" {
key.id = 2
}
}
}

// WriteString writes the toWrite string into the stream defined by streamName and streamIndex.
func (sm streamManagerImpl) WriteString(streamName string, streamIndex int64, toWrite string) bool {
key := streamKey{id: streamIndex, name: streamName}
sm.fillMissingIndex(&key)
writer, hasWriter := sm.openStreams[key]
if !hasWriter {
return false
Expand Down

0 comments on commit 0a346d0

Please sign in to comment.