Skip to content

Commit

Permalink
Add continued if
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Sep 12, 2023
1 parent 5289f93 commit a5e0c13
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,19 @@ lazy_static! {
};
pub static ref ADVANCED_MACROS: String = {
indoc! {"(
(defmac if (A B C)
(defmac __chia__primitive__if (A B C)
(qq (a (i (unquote A) (com (unquote B)) (com (unquote C))) @))
)
(defun __chia__if (ARGS)
(__chia__primitive__if (r (r (r ARGS)))
(qq (i (unquote (f ARGS)) (com (unquote (f (r ARGS)))) (unquote (__chia__if (r (r ARGS))))))
(qq (i (unquote (f ARGS)) (com (unquote (f (r ARGS)))) (com (unquote (f (r (r ARGS)))))))
)
)
(defmac if ARGS (qq (a (unquote (__chia__if ARGS)) @)))
(defun __chia__compile-list (args)
(if args
(c 4 (c (f args) (c (__chia__compile-list (r args)) ())))
Expand Down
30 changes: 30 additions & 0 deletions src/tests/classic/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,3 +1430,33 @@ fn test_classic_obeys_operator_choice_at_compile_time_version_0() {
.to_string();
assert_eq!(compiled, "FAIL: unimplemented operator 48");
}

#[test]
fn test_continued_if() {
let prog = indoc! {"
(mod X
(include *strict-cl-21*)
(defun bigatom (Xs Ys)
(if
Xs (concat (f Xs) (bigatom (r Xs) Ys))
Ys (concat (f Ys) (bigatom (r Ys) ()))
()
)
)
(bigatom (q . (3 4 5)) X)
)"}
.to_string();
let compiled = do_basic_run(&vec!["run".to_string(), prog])
.trim()
.to_string();
let res = do_basic_brun(&vec![
"brun".to_string(),
compiled,
"(13 99 144)".to_string(),
])
.trim()
.to_string();
assert_eq!(res.to_string(), "0x0304050d630090");
}
4 changes: 3 additions & 1 deletion src/tests/compiler/preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,9 @@ fn test_preprocessor_tours_includes_properly() {
let mut includes = Vec::new();
let res = preprocess(opts, &mut includes, parsed[0].clone()).expect("should preprocess");
let expected_lines = &[
"(defmac if (A B C) (qq (a (i (unquote A) (com (unquote B)) (com (unquote C))) @)))",
"(defmac __chia__primitive__if (A B C) (qq (a (i (unquote A) (com (unquote B)) (com (unquote C))) @)))",
"(defun __chia__if (ARGS) (a (i (r (r (r ARGS))) (com (qq (i (unquote (f ARGS)) (com (unquote (f (r ARGS)))) (unquote (__chia__if (r (r ARGS))))))) (com (qq (i (unquote (f ARGS)) (com (unquote (f (r ARGS)))) (com (unquote (f (r (r ARGS))))))))) @))",
"(defmac if ARGS (qq (a (unquote (__chia__if ARGS)) @)))",
"(defun __chia__compile-list (args) (a (i args (com (c 4 (c (f args) (c (__chia__compile-list (r args)) ())))) (com ())) @))",
"(defmac list ARGS (__chia__compile-list ARGS))",
"(defun-inline / (A B) (f (divmod A B)))",
Expand Down

0 comments on commit a5e0c13

Please sign in to comment.