Skip to content
This repository was archived by the owner on Oct 14, 2021. It is now read-only.

Commit 7adb491

Browse files
authored
Add print function to the REPL. (#33)
print() is in the specification but the specification is leaving to the application the decision on how it should be print (which make sense), hence it make no sense to have the implementation of that function in the library but the REPL should still have it. Fixes #32.
1 parent 2879e27 commit 7adb491

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

starlark-repl/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
extern crate codemap;
4040
extern crate codemap_diagnostic;
4141
extern crate linefeed;
42+
#[macro_use]
4243
extern crate starlark;
4344

4445
use codemap_diagnostic::{ColorConfig, Emitter};
@@ -48,6 +49,7 @@ use starlark::eval::eval_lexer;
4849
use starlark::eval::simple::SimpleFileLoader;
4950
use starlark::syntax::dialect::Dialect;
5051
use starlark::syntax::lexer::{BufferedLexer, LexerIntoIter, LexerItem};
52+
use starlark::values::Value;
5153
use std::sync::{Arc, Mutex};
5254

5355
fn print_eval<T1: Iterator<Item = LexerItem>, T2: LexerIntoIter<T1>>(
@@ -76,6 +78,19 @@ fn print_eval<T1: Iterator<Item = LexerItem>, T2: LexerIntoIter<T1>>(
7678
}
7779
}
7880

81+
starlark_module! {print_function =>
82+
/// print: print an object string representation to stderr.
83+
///
84+
/// Examples:
85+
/// ```python
86+
/// print("some message") # Will print "some message" to stderr
87+
/// ```
88+
print(msg) {
89+
eprintln!("{}", msg.to_str());
90+
Ok(Value::new(None))
91+
}
92+
}
93+
7994
/// A REPL (Read-Eval-Print Loop) for Starlark.
8095
///
8196
/// This method run a REPL until the user hit Ctrl+D. It can be used for interactive use where the

starlark-repl/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use getopts::Options;
2222
use starlark::eval::interactive::eval_file;
2323
use starlark::stdlib::global_environment;
2424
use starlark::syntax::dialect::Dialect;
25-
use starlark_repl::repl;
25+
use starlark_repl::{print_function, repl};
2626
use std::env;
2727

2828
macro_rules! print_usage {
@@ -69,7 +69,7 @@ fn main() {
6969
} else {
7070
let build_file = matches.opt_present("b");
7171
let opt_repl = matches.opt_present("r");
72-
let global = global_environment();
72+
let global = print_function(global_environment());
7373
global.freeze();
7474
let dialect = if build_file {
7575
Dialect::Build

0 commit comments

Comments
 (0)