From a5e0c13943db1829bb4e290bb5c464d8b7daa395 Mon Sep 17 00:00:00 2001 From: arty Date: Tue, 12 Sep 2023 10:04:57 -0700 Subject: [PATCH] Add continued if --- src/compiler/compiler.rs | 11 ++++++++++- src/tests/classic/run.rs | 30 ++++++++++++++++++++++++++++++ src/tests/compiler/preprocessor.rs | 4 +++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/compiler/compiler.rs b/src/compiler/compiler.rs index fc196fae5..00f5c7bc5 100644 --- a/src/compiler/compiler.rs +++ b/src/compiler/compiler.rs @@ -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)) ()))) diff --git a/src/tests/classic/run.rs b/src/tests/classic/run.rs index 24681f4e7..be2b99733 100644 --- a/src/tests/classic/run.rs +++ b/src/tests/classic/run.rs @@ -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"); +} diff --git a/src/tests/compiler/preprocessor.rs b/src/tests/compiler/preprocessor.rs index 9d8947227..d6ede7f50 100644 --- a/src/tests/compiler/preprocessor.rs +++ b/src/tests/compiler/preprocessor.rs @@ -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)))",