clvm_tools_rs 0.4.0

tools for working with chialisp language; compiler, repl, python and wasm bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
use crate::compiler::compiler::DefaultCompilerOpts;
use crate::compiler::comptypes::CompilerOpts;
use crate::compiler::dialect::AcceptedDialect;
use crate::compiler::preprocessor::preprocess;
use crate::compiler::sexp::parse_sexp;
use crate::compiler::srcloc::Srcloc;
use std::rc::Rc;

use crate::tests::compiler::compiler::run_string_strict;

#[test]
fn test_defmac_basic_0() {
    let prog = indoc! {"
    (mod (X)
      (defmac double-arg (A) (list (string->symbol (string-append (symbol->string A) \"1\")) (string->symbol (string-append (symbol->string A) \"2\"))))
      (defun strange (double-arg X) (+ X1 X2))
      (strange X (* 2 X))
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3)".to_string()).unwrap();
    assert_eq!(res.to_string(), "9");
}

#[test]
fn test_defmac_basic_shared_constant() {
    let prog = indoc! {"
    (mod (X)
      (defconstant twostring \"2\")
      (defmac double-arg (A) (list (string->symbol (string-append (symbol->string A) \"1\")) (string->symbol (string-append (symbol->string A) twostring))))
      (defun strange (double-arg X) (+ X1 X2))
      (c twostring (strange X (* 2 X)))
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3)".to_string()).unwrap();
    assert_eq!(res.to_string(), "(\"2\" . 9)");
}

#[test]
fn test_defmac_basic_shared_constant_not_string_with_string_operator() {
    let prog = indoc! {"
    (mod (X)
      (defconstant twostring 2)
      (defmac double-arg (A) (list (string->symbol (string-append (symbol->string A) \"1\")) (string->symbol (string-append (symbol->string A) twostring))))
      (defun strange (double-arg X) (+ X1 X2))
      (c twostring (strange X (* 2 X)))
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3)".to_string());
    assert!(res.is_err());
}

#[test]
fn test_defmac_basic_shared_constant_not_string_with_string_operator_fun() {
    let prog = indoc! {"
    (mod (X)
      (defconstant twostring \"2\")
      (defun make-arg-list (A) (list (string->symbol (string-append (symbol->string A) \"1\")) (string->symbol (string-append (symbol->string A) twostring))))
      (defmac double-arg (A) (make-arg-list A))
      (defun strange (double-arg X) (+ X1 X2))
      (c twostring (strange X (* 2 X)))
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3)".to_string()).unwrap();
    assert_eq!(res.to_string(), "(\"2\" . 9)");
}

#[test]
fn test_defmac_basic_test_is_string_pos() {
    let prog = indoc! {"
    (mod (X)
      (defmac classify (S)
        (if (string? S)
          (qq (c 1 (unquote S)))
          (qq (c 2 (unquote S)))
          )
        )
      (c X (classify \"test\"))
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3)".to_string()).unwrap();
    assert_eq!(res.to_string(), "(3 1 . \"test\")");
}

#[test]
fn test_defmac_basic_test_is_string_neg() {
    let prog = indoc! {"
    (mod (X)
      (defmac classify (S)
        (if (string? S)
          (qq (c 1 (unquote S)))
          (qq (c 2 (unquote S)))
          )
        )
      (c X (classify 99))
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3)".to_string()).unwrap();
    assert_eq!(res.to_string(), "(3 2 . 99)");
}

#[test]
fn test_defmac_basic_test_is_symbol_pos() {
    let prog = indoc! {"
    (mod (X)
      (defmac classify (S)
        (if (symbol? S)
          (qq (c 1 (unquote (symbol->string S))))
          (qq (c 2 (unquote S)))
          )
        )
      (c X (classify test))
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3)".to_string()).unwrap();
    assert_eq!(res.to_string(), "(3 1 . \"test\")");
}

#[test]
fn test_defmac_basic_test_is_symbol_neg() {
    let prog = indoc! {"
    (mod (X)
      (defmac classify (S)
        (if (symbol? S)
          (qq (c 1 (unquote S)))
          (qq (c 2 (unquote S)))
          )
        )
      (c X (classify \"test\"))
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3)".to_string()).unwrap();
    assert_eq!(res.to_string(), "(3 2 . \"test\")");
}

#[test]
fn test_defmac_basic_test_is_number_pos() {
    let prog = indoc! {"
    (mod (X)
      (defmac classify (S)
        (if (number? S)
          (qq (c 1 (unquote S)))
          (qq (c 2 (unquote S)))
          )
        )
      (c X (classify 7))
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3)".to_string()).unwrap();
    assert_eq!(res.to_string(), "(3 1 . 7)");
}

#[test]
fn test_defmac_basic_test_is_number_neg() {
    let prog = indoc! {"
    (mod (X)
      (defmac classify (S)
        (if (number? S)
          (qq (c 1 (unquote S)))
          (qq (c 2 (unquote S)))
          )
        )
      (c X (classify \"test\"))
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3)".to_string()).unwrap();
    assert_eq!(res.to_string(), "(3 2 . \"test\")");
}

#[test]
fn test_defmac_extension_from_function() {
    let prog = indoc! {"
    (mod (X)
      (defun FX (X) (symbol->string X))
      (defmac F (X) (FX X))
      (c 3 (F X))
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3)".to_string()).unwrap();
    assert_eq!(res.to_string(), "(3 . \"X\")");
}

#[test]
fn test_defmac_if_extension() {
    let prog = indoc! {"
    (mod (X)
      (defun FX (X) (if X (number->string 1) 2))
      (defmac F (X) (c 1 (FX X)))
      (F X)
      )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(9)".to_string()).unwrap();
    assert_eq!(res.to_string(), "\"1\"");
}

#[test]
fn test_defmac_create_match_form() {
    let prog = indoc! {"
    ;; Make a simple list match ala ocaml/elm/haskell.
    ;;
    ;; The real version will be more elaborate.  This is a test case and a demo.
    (mod X
        (include *standard-cl-21*)
        (defun list-nth (L N)
          (if N
            (list-nth (r L) (- N 1))
            (f L)
            )
          )

        (defun list-len (L N)
          (if L
            (list-len (r L) (+ N 1))
            N
            )
          )

        (defun funcall (name args) (c (string->symbol name) args))
        (defun quoted (X) (c 1 X))
        (defun equals (A B) (funcall \"=\" (list A B)))
        (defun emit-list-nth (L N) (funcall \"list-nth\" (list L N)))
        (defun emit-list-len (L N) (funcall \"list-len\" (list L N)))
        (defun emit-if (C T E) (funcall \"if\" (list C T E)))
        (defun emit-let (bindings body) (funcall \"let\" (list bindings body)))

        ;; Determine the size of each list as well as the constants and bindings
        ;; Return either a list of (number-of-elts matches bindings) or symbol.
        (defun build-matches-and-bindings (n pattern matches bindings)
            (if (not pattern)
                (list n matches bindings)
                (if (l pattern)
                    (if (symbol? (f pattern))
                        (build-matches-and-bindings (+ n 1) (r pattern) matches (c (c n (f pattern)) bindings))
                        (build-matches-and-bindings (+ n 1) (r pattern) (c (c n (f pattern)) matches) bindings)
                        )
                    pattern
                    )
                )
            )

        ;; Emit code that matches each match list for length and constants.
        (defun write-match-code (expr matches)
            (if (not matches)
                (quoted 1)
                (if (l (f matches))
                    (let*
                        (
                         (offset (f (f matches)))
                         (desire (r (f matches)))
                         (this-match (equals (quoted desire) (emit-list-nth expr (quoted offset))))
                         )
                      (if (not (r matches))
                          (list this-match)
                          (c this-match (write-match-code expr (r matches)))
                          )
                      )
                    (quoted 1)
                    )
                )
            )

        ;; Generate let bindings for the bindings indicated in the match.
        (defun let-bindings (expr bindings)
            (if (not bindings)
                ()
                (let
                    ((n (f (f bindings)))
                     (binding (r (f bindings)))
                     )
                  (c (list binding (emit-list-nth expr n)) (let-bindings expr (r bindings)))
                  )
                )
            )

        ;; Generate if expressions that match the indicates matches and return
        ;; the indicated code with bindings installed.
        (defun match-if (expr clauses)
            (if (not clauses)
                (list 8)
                (let
                    ((extracted-clause-data (build-matches-and-bindings 0 (f (f clauses)) () ()))
                     (code (f (r (f clauses))))
                     )
                  (if (l extracted-clause-data)
                      (let
                          (
                           (number-of-elts (f extracted-clause-data))
                           (matches (list-nth extracted-clause-data 1))
                           (bindings (list-nth extracted-clause-data 2))
                           )
                        (emit-if
                          (emit-if
                            (equals (emit-list-len expr 0) (quoted number-of-elts))
                            ;; then
                            (c (string->symbol \"logand\") (write-match-code expr matches))
                            ;; else
                            ()
                            )
                          ;; then
                          (emit-let (let-bindings expr bindings) code)
                          ;; else
                          (match-if expr (r clauses))
                          )
                        )
                      (emit-let (list (list extracted-clause-data expr)) code)
                      )
                     )
                )
            )

        ;; match as below.
        (defmac match (expr . matches) (match-if expr matches))

        (match X
          ((16 x y) (c 1 (+ x y)))
          ((3 () b c) c)
          ((3 1 b c) b)
          (x x)
          )
        )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(3 () 1001 1002)".to_string()).unwrap();
    assert_eq!(res.to_string(), "1002");
}

#[test]
fn test_defmac_stringq() {
    let prog = indoc! {"
      (mod ()
         (defmac is-string (X) (string? X))
         (list (is-string X) (is-string \"X\") (is-string 3))
         )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"()".to_string()).unwrap();
    assert_eq!(res.to_string(), "(() 1 ())");
}

#[test]
fn test_defmac_numberq() {
    let prog = indoc! {"
      (mod ()
         (defmac is-number (X) (number? X))
         (list (is-number X) (is-number \"X\") (is-number 3))
         )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"()".to_string()).unwrap();
    assert_eq!(res.to_string(), "(() () 1)");
}

#[test]
fn test_defmac_symbolq() {
    let prog = indoc! {"
      (mod ()
         (defmac is-symbol (X) (symbol? X))
         (list (is-symbol X) (is-symbol \"X\") (is-symbol 3))
         )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"()".to_string()).unwrap();
    assert_eq!(res.to_string(), "(1 () ())");
}

#[test]
fn test_defmac_string_to_symbol() {
    let prog = indoc! {"
      (mod ()
         (defmac is-symbol (X) (symbol? X))
         (list (is-symbol X) (is-symbol \"X\") (is-symbol 3))
         )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"()".to_string()).unwrap();
    assert_eq!(res.to_string(), "(1 () ())");
}

#[test]
fn test_defmac_string_to_symbol_converts() {
    let prog = indoc! {"
      (mod (X)
        (defmac let_pi (code) (qq (let (((unquote (string->symbol \"pi\")) 31415)) (unquote code))))
        (let_pi (+ pi X))
        )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(5)".to_string()).unwrap();
    assert_eq!(res.to_string(), "31420");
}

#[test]
fn test_defmac_string_needs_conversion() {
    let prog = indoc! {"
      (mod (X)
        (defmac let_pi (code) (qq (let ((\"pi\" 31415)) (unquote code))))
        (let_pi (+ pi X))
        )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(5)".to_string());
    eprintln!("res {res:?}");
    assert!(res.is_err());
}

#[test]
fn test_defmac_string_substr_0() {
    let prog = indoc! {"
      (mod (X)
        (defmac first-letter-of (Q)
          (let ((first-character (substring (symbol->string Q) 0 1)))
            (qq (c (unquote first-character) (unquote (string->symbol first-character))))
            )
          )
        (first-letter-of Xanadu)
        )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(5999)".to_string()).unwrap();
    assert_eq!(res.to_string(), "(\"X\" . 5999)");
}

#[test]
fn test_defmac_string_substr_bad() {
    let prog = indoc! {"
      (mod (test_variable_name)
        (defmac bind-tail-of-symbol (N Q CODE)
          (let*
            ((stringified (symbol->string Q))
             (slen (string-length stringified))
             (suffix (string->symbol (substring stringified N slen))))
            (qq (let (((unquote suffix) (r (unquote Q)))) (unquote CODE)))
            )
          )
        (bind-tail-of-symbol 100 test_variable_name (c 9999 variable_name))
        )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"((87 89 91))".to_string());
    assert!(res.is_err());
}

#[test]
fn test_defmac_string_to_number_0() {
    let prog = indoc! {"
      (mod (X_7)
        (defmac add-n-to (X)
          (let*
            ((stringified (symbol->string X))
             (slen (string-length stringified))
             (number-part (substring stringified (- slen 1) slen))
             (numeric-value (string->number number-part)))
            (qq (+ (unquote numeric-value) (unquote X)))
            )
          )
        (add-n-to X_7)
        )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(31)".to_string()).unwrap();
    assert_eq!(res.to_string(), "38");
}

#[test]
fn test_defmac_string_to_number_bad() {
    let prog = indoc! {"
      (mod (X_A)
        (defmac add-n-to (X)
          (let*
            ((stringified (symbol->string X))
             (slen (string-length stringified))
             (number-part (substring stringified (- slen 1) slen))
             (numeric-value (string->number number-part)))
            (qq (+ (unquote numeric-value) (unquote X)))
            )
          )
        (add-n-to X_A)
        )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(31)".to_string());
    assert!(res.is_err());
}

#[test]
fn test_defmac_number_to_string() {
    let prog = indoc! {"
      (mod (Q)
        (defmac with-my-length (X)
          (let*
            ((stringified (symbol->string X))
             (slen (string-length stringified)))
            (string->symbol (string-append stringified \"-\" (number->string slen)))
            )
          )
        (defun F (Xanadu-6) (+ (with-my-length Xanadu) 99))
        (F Q)
        )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"(37)".to_string()).unwrap();
    assert_eq!(res.to_string(), "136");
}

#[test]
fn test_preprocess_basic_list() {
    let file = "*test*";
    let input_form_set = indoc! {"(
         (include *strict-cl-21*)
         (list 1 2 3)
    )"};
    let parsed_forms =
        parse_sexp(Srcloc::start(file), input_form_set.bytes()).expect("should parse");
    let opts: Rc<dyn CompilerOpts> =
        Rc::new(DefaultCompilerOpts::new(file)).set_dialect(AcceptedDialect {
            stepping: Some(21),
            strict: true,
            int_fix: false,
        });
    let mut includes = Vec::new();
    let pp = preprocess(opts.clone(), &mut includes, parsed_forms[0].clone())
        .expect("should preprocess");
    assert_eq!(pp[pp.len() - 1].to_string(), "(4 1 (4 2 (4 3 ())))");
}

#[test]
fn test_preprocess_expansion_makes_numeric_operators() {
    let prog = indoc! {"
      (mod ()
         (include *strict-cl-21*)
         (defmac G () (com (4 \"test\" ())))
         (G)
         )
    "}
    .to_string();
    let res = run_string_strict(&prog, &"()".to_string()).unwrap();
    assert_eq!(res.to_string(), "(\"test\")");
}

#[test]
fn test_preprocessor_tours_includes_properly() {
    let prog = indoc! {"
      ( ;; Note: preprocessing is run in the list of the body forms.
        (include *strict-cl-21*)
        (include condition_codes.clvm)
        (include curry-and-treehash.clinc)
        ()
      )
    "}
    .to_string();
    let pname = "*test*";
    let opts: Rc<dyn CompilerOpts> = Rc::new(DefaultCompilerOpts::new(pname))
        .set_search_paths(&["resources/tests".to_string()])
        .set_dialect(AcceptedDialect {
            stepping: Some(21),
            strict: true,
            int_fix: false,
        });
    let parsed = parse_sexp(Srcloc::start(pname), prog.bytes()).expect("should parse");
    let mut includes = Vec::new();
    let res = preprocess(opts, &mut includes, parsed[0].clone()).expect("should preprocess");
    let expected_lines = &[
        "(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 (a (i (unquote (f ARGS)) (com (unquote (f (r ARGS)))) (com (unquote (__chia__if (r (r ARGS)))))) @))) (com (qq (a (i (unquote (f ARGS)) (com (unquote (f (r ARGS)))) (com (unquote (f (r (r ARGS)))))) @)))) @))",
        "(defmac if ARGS (__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)))",
        "(defconstant *chialisp-version* 22)",
        "(defconstant AGG_SIG_UNSAFE 49)",
        "(defconstant AGG_SIG_ME 50)",
        "(defconstant CREATE_COIN 51)",
        "(defconstant RESERVE_FEE 52)",
        "(defconstant CREATE_COIN_ANNOUNCEMENT 60)",
        "(defconstant ASSERT_COIN_ANNOUNCEMENT 61)",
        "(defconstant CREATE_PUZZLE_ANNOUNCEMENT 62)",
        "(defconstant ASSERT_PUZZLE_ANNOUNCEMENT 63)",
        "(defconstant ASSERT_MY_COIN_ID 70)",
        "(defconstant ASSERT_MY_PARENT_ID 71)",
        "(defconstant ASSERT_MY_PUZZLEHASH 72)",
        "(defconstant ASSERT_MY_AMOUNT 73)",
        "(defconstant ASSERT_SECONDS_RELATIVE 80)",
        "(defconstant ASSERT_SECONDS_ABSOLUTE 81)",
        "(defconstant ASSERT_HEIGHT_RELATIVE 82)",
        "(defconstant ASSERT_HEIGHT_ABSOLUTE 83)",
        "(defconstant ONE 1)",
        "(defconstant TWO 2)",
        "(defconstant A_KW 2)",
        "(defconstant Q_KW 1)",
        "(defconstant C_KW 4)",
        "(defun-inline update-hash-for-parameter-hash (parameter-hash environment-hash) (sha256 TWO (sha256 ONE C_KW) (sha256 TWO (sha256 TWO (sha256 ONE Q_KW) parameter-hash) (sha256 TWO environment-hash (sha256 ONE ())))))",
        "(defun build-curry-list (reversed-curry-parameter-hashes environment-hash) (a (i reversed-curry-parameter-hashes (com (build-curry-list (r reversed-curry-parameter-hashes) (update-hash-for-parameter-hash (f reversed-curry-parameter-hashes) environment-hash))) (com environment-hash)) @))",
        "(defun-inline tree-hash-of-apply (function-hash environment-hash) (sha256 TWO (sha256 ONE A_KW) (sha256 TWO (sha256 TWO (sha256 ONE Q_KW) function-hash) (sha256 TWO environment-hash (sha256 ONE ())))))",
        "(defun puzzle-hash-of-curried-function (function-hash . reversed-curry-parameter-hashes) (tree-hash-of-apply function-hash (build-curry-list reversed-curry-parameter-hashes (sha256 ONE ONE))))",
        "()",
    ];
    for (i, r) in res.iter().enumerate() {
        assert_eq!(r.to_string(), expected_lines[i]);
    }
    assert_eq!(res.len(), expected_lines.len());
}