-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from skx/io-indirection
Allow I/O operations to have a level of indirection.
- Loading branch information
Showing
10 changed files
with
139 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Package config provides an I/O abstraction for our interpreter, | ||
// allowing it to be embedded and used in places where STDIN and STDOUT | ||
// are not necessarily terminal-based. | ||
// | ||
// All input-reading uses the level of indirection provided here, and | ||
// similarly output goes via the writer we hold here. | ||
// | ||
// This abstraction allows a host program to setup a different pair of | ||
// streams prior to initializing the interpreter. | ||
package config | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
// Config is a holder for configuration which is used for interfacing | ||
// the interpreter with the outside world. | ||
type Config struct { | ||
|
||
// STDIN is an input-reader used for the (read) function, when | ||
// called with no arguments. | ||
STDIN io.Reader | ||
|
||
// STDOUT is the writer which is used for "(print)". | ||
STDOUT io.Writer | ||
} | ||
|
||
// New returns a new configuration object | ||
func New() *Config { | ||
|
||
e := &Config{} | ||
return e | ||
} | ||
|
||
// DefaultIO returns a configuration which uses the default | ||
// input and output streams - i.e. STDIN and STDOUT work as | ||
// expected | ||
func DefaultIO() *Config { | ||
e := New() | ||
|
||
// Setup default input/output streams | ||
e.STDIN = os.Stdin | ||
e.STDOUT = os.Stdout | ||
|
||
return e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters