Skip to content

Commit 74172f7

Browse files
committed
add small example programs, update regex pattern display formatter to show escapes
1 parent 00ac597 commit 74172f7

File tree

6 files changed

+48
-11
lines changed

6 files changed

+48
-11
lines changed

examples/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# ClojureRS Examples
2+
3+
These small programs show some implemented functionality as small
4+
programs as that can be interpreted by ClojureRS.
5+
6+

examples/guess_number.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(println "Guess a number between 1 and 3.")
2+
(print "guess > ")
3+
(flush)
4+
(let [answer (read-line)
5+
number (+ (rand-int 3) 1)]
6+
7+
(if (= answer (str number))
8+
(println "correct!")
9+
(println "wrong, the correct was " number)))

examples/strings.clj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
(defn main []
3+
4+
(println "'hello world' ends with 'orld' : " (clojure.string/ends-with? "hello world" "orld"))
5+
(println "'hello world' starts with 'hel' : " (clojure.string/starts-with? "hello world" "hel"))
6+
7+
(println "nil is empty string : " (clojure.string/blank? nil))
8+
(println "whitespace is empty string : " (clojure.string/blank? " "))
9+
(println "hello is empty string : " (clojure.string/blank? "hello"))
10+
11+
(println "hello,world splitted by regex pattern " #"\"" " " (clojure.string/split "hello,world", #","))
12+
13+
(println "murder is backwards : " (clojure.string/reverse "murder"))
14+
15+
(println "upper case hello : " (clojure.string/upper-case "hello"))
16+
(println "lower case HELLO : " (clojure.string/lower-case "HELLO"))
17+
18+
(println "joining array of items with ', ' : " (clojure.string/join ", " [1, "second", true]))
19+
20+
(println "hello world includes 'o wor' : " (clojure.string/includes? "hello world" "o wor")))
21+
22+
(main)
23+

examples/what_is_your_name.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(print "What is your name? " )
2+
(flush)
3+
(let [answer (read-line)]
4+
(println "Hello, " answer "!"))

src/environment.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,6 @@ impl Environment {
402402
//
403403
// @TODO its time for a RT (runtime), which environment seems to be becoming
404404
let _ = Repl::new(Rc::clone(&environment)).try_eval_file("./src/clojure/core.clj");
405-
println!(
406-
"{:#?} {:#?}",
407-
&environment.get_current_namespace(),
408-
&environment.get_current_namespace_name()
409-
);
410405
// TODO: should read into namespace if (ns ..) is given in source file
411406
let _ = Repl::new(Rc::clone(&environment)).try_eval_file("./src/clojure/string.clj");
412407

src/value.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use core::fmt::Display;
21
use crate::environment::Environment;
32
use crate::ifn::IFn;
43
use crate::keyword::Keyword;
@@ -10,6 +9,7 @@ use crate::persistent_list_map::{PersistentListMap, ToPersistentListMapIter};
109
use crate::persistent_vector::PersistentVector;
1110
use crate::symbol::Symbol;
1211
use crate::type_tag::TypeTag;
12+
use core::fmt::Display;
1313

1414
extern crate rand;
1515
use rand::Rng;
@@ -173,9 +173,9 @@ impl fmt::Display for Value {
173173
IfMacro => std::string::String::from("#macro[if*]"),
174174
LetMacro => std::string::String::from("#macro[let*]"),
175175
Value::String(string) => string.clone(),
176-
Pattern(pattern) => {
177-
std::string::String::from("#\"".to_owned() + pattern.as_str().clone() + "\"")
178-
}
176+
Pattern(pattern) => std::string::String::from(
177+
"#\"".to_owned() + &pattern.as_str().escape_default().to_string().clone() + "\"",
178+
),
179179
Nil => std::string::String::from("nil"),
180180
};
181181
write!(f, "{}", str)
@@ -624,11 +624,11 @@ impl ToValue for PersistentListMap {
624624
Value::PersistentListMap(self.clone())
625625
}
626626
}
627-
impl<T: Display> ToValue for Result<Value,T> {
627+
impl<T: Display> ToValue for Result<Value, T> {
628628
fn to_value(&self) -> Value {
629629
match self {
630630
Ok(val) => val.clone(),
631-
Err(err) => Value::Condition(err.to_string())
631+
Err(err) => Value::Condition(err.to_string()),
632632
}
633633
}
634634
}

0 commit comments

Comments
 (0)