astreum 0.2.29__py3-none-any.whl → 0.2.30__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of astreum might be problematic. Click here for more details.

astreum/node.py CHANGED
@@ -463,7 +463,7 @@ class Node:
463
463
  elif first.value == "def":
464
464
  args = expr.elements[1:]
465
465
  if len(args) != 2:
466
- return Expr.Error(message=f"'def' expects exactly 2 arguments, got {len(args)}", origin=expr)
466
+ return Expr.Error("def expects key value", origin=expr)
467
467
  if not isinstance(args[0], Expr.Symbol):
468
468
  return Expr.Error(message="first argument to 'def' must be a symbol", origin=args[0])
469
469
  result = self.machine_expr_eval(env_id=env_id, expr=args[1])
@@ -473,13 +473,191 @@ class Node:
473
473
  self.machine_expr_put(env_id=env_id, name=args[0].value, expr=result)
474
474
  return result
475
475
 
476
- # # List
477
- elif first.value == "list.each":
478
- internal_function = expr.elements[1]
476
+ ## List: ints -> (1 2)
477
+ # push: (list.push 3 ints) -> (1 2 3) / (list.push 0 0 ints) -> (0 1 2)
478
+ elif first.value == "list.push":
479
+ args = expr.elements[1:]
480
+ if len(args) == 2:
481
+ val_expr, list_expr = args
482
+ idx = None
483
+ elif len(args) == 3:
484
+ idx_expr, val_expr, list_expr = args
485
+ idx = self.machine_expr_eval(env_id, idx_expr)
486
+ if isinstance(idx, Expr.Error): return idx
487
+ if not isinstance(idx, Expr.IntExpr):
488
+ return Expr.Error("index must be int", origin=idx_expr)
489
+ idx = idx.value
490
+ else:
491
+ return Expr.Error("list.push expects (value list) or (index value list)", origin=expr)
492
+
493
+ lst = self.machine_expr_eval(env_id, list_expr)
494
+ if isinstance(lst, Expr.Error): return lst
495
+ if not isinstance(lst, Expr.ListExpr):
496
+ return Expr.Error("last arg to list.push must be a list", origin=list_expr)
497
+
498
+ val = self.machine_expr_eval(env_id, val_expr)
499
+ if isinstance(val, Expr.Error): return val
479
500
 
480
-
501
+ elems = list(lst.elements)
502
+ if idx is None:
503
+ elems.append(val)
504
+ else:
505
+ if idx < 0 or idx > len(elems):
506
+ return Expr.Error("index out of range", origin=idx_expr)
507
+ elems.insert(idx, val)
508
+ return Expr.ListExpr(elems)
509
+
510
+ # pop: (list.pop 1 ints) -> 2
511
+ elif first.value == "list.pop":
512
+ if len(expr.elements) < 3:
513
+ return Expr.Error("list.pop expects index list", origin=expr)
514
+
515
+ idx_expr, list_expr = expr.elements[1], expr.elements[2]
516
+ idx = self.machine_expr_eval(env_id, idx_expr)
517
+ if isinstance(idx, Expr.Error): return idx
518
+ if not isinstance(idx, Expr.IntExpr):
519
+ return Expr.Error("index must be int", origin=idx_expr)
520
+ idx = idx.value
521
+
522
+ lst = self.machine_expr_eval(env_id, list_expr)
523
+ if isinstance(lst, Expr.Error): return lst
524
+ if not isinstance(lst, Expr.ListExpr):
525
+ return Expr.Error("second arg to list.pop must be a list", origin=list_expr)
526
+
527
+ elems = list(lst.elements)
528
+ if idx < 0 or idx >= len(elems):
529
+ return Expr.Error("index out of range", origin=idx_expr)
530
+ del elems[idx]
531
+ return Expr.ListExpr(elems)
532
+
533
+ # get: (list.get 1 ints) -> 2
534
+ elif first.value == "list.get":
535
+ if len(expr.elements) < 3:
536
+ return Expr.Error("list.get expects index list", origin=expr)
537
+
538
+ idx_expr, list_expr = expr.elements[1], expr.elements[2]
539
+ idx = self.machine_expr_eval(env_id, idx_expr)
540
+ if isinstance(idx, Expr.Error): return idx
541
+ if not isinstance(idx, Expr.IntExpr):
542
+ return Expr.Error("index must be int", origin=idx_expr)
543
+ idx = idx.value
544
+
545
+ lst = self.machine_expr_eval(env_id, list_expr)
546
+ if isinstance(lst, Expr.Error): return lst
547
+ if not isinstance(lst, Expr.ListExpr):
548
+ return Expr.Error("second arg to list.get must be a list", origin=list_expr)
549
+
550
+ if idx < 0 or idx >= len(lst.elements):
551
+ return Expr.Error("index out of range", origin=idx_expr)
552
+ return lst.elements[idx]
553
+
554
+ # set: (list.set 1 3 ints) -> (1 3)
555
+ elif first.value == "list.set":
556
+ if len(expr.elements) < 4:
557
+ return Expr.Error("list.set expects index value list", origin=expr)
558
+ idx_expr, val_expr, list_expr = expr.elements[1], expr.elements[2], expr.elements[3]
559
+ idx = self.machine_expr_eval(env_id, idx_expr)
560
+ if isinstance(idx, Expr.Error): return idx
561
+ if not isinstance(idx, Expr.IntExpr):
562
+ return Expr.Error("index must be int", origin=idx_expr)
563
+ idx = idx.value
564
+
565
+ val = self.machine_expr_eval(env_id, val_expr)
566
+ if isinstance(val, Expr.Error): return val
567
+
568
+ lst = self.machine_expr_eval(env_id, list_expr)
569
+ if isinstance(lst, Expr.Error): return lst
570
+ if not isinstance(lst, Expr.ListExpr):
571
+ return Expr.Error("third arg to list.set must be a list", origin=list_expr)
481
572
 
482
- # Integer arithmetic primitives
573
+ elems = list(lst.elements)
574
+ if idx < 0 or idx >= len(elems):
575
+ return Expr.Error("index out of range", origin=idx_expr)
576
+ elems[idx] = val
577
+ return Expr.ListExpr(elems)
578
+
579
+ ### each: (list.each fn list) -> ()
580
+ elif first.value == "list.each":
581
+ if len(expr.elements) < 3:
582
+ return Expr.Error("list.each expects fn list", origin=expr)
583
+ fn_expr, list_expr = expr.elements[1], expr.elements[2]
584
+ lst = self.machine_expr_eval(env_id, list_expr)
585
+ if isinstance(lst, Expr.Error):
586
+ return lst
587
+ if not isinstance(lst, Expr.ListExpr):
588
+ return Expr.Error("second arg to list.each must be a list", origin=list_expr)
589
+
590
+ for el in lst.elements:
591
+ res = self.machine_expr_eval(env_id, Expr.ListExpr([fn_expr, el]))
592
+ if isinstance(res, Expr.Error):
593
+ return res
594
+ return Expr.ListExpr([])
595
+
596
+ ### fold: (list.fold fn init list) / (list.fold + 0 ints) -> 3
597
+ elif first.value == "list.fold":
598
+ fn_expr, init_expr, list_expr = expr.elements[1], expr.elements[2], expr.elements[3]
599
+ acc = self.machine_expr_eval(env_id, init_expr)
600
+ if isinstance(acc, Expr.Error):
601
+ return acc
602
+
603
+ lst = self.machine_expr_eval(env_id, list_expr)
604
+ if isinstance(lst, Expr.Error):
605
+ return lst
606
+ if not isinstance(lst, Expr.ListExpr):
607
+ return Expr.Error("third arg to list.fold must be a list", origin=list_expr)
608
+
609
+ for el in lst.elements:
610
+ call = Expr.ListExpr([fn_expr, acc, el])
611
+ res = self.machine_expr_eval(env_id, call)
612
+ if isinstance(res, Expr.Error):
613
+ return res
614
+ acc = res
615
+
616
+ return acc
617
+
618
+ ### sort: (list.sort fn list) / (list.sort (fn (a b) (a < b)) ints) -> (2 1)
619
+ elif first.value == "list.sort":
620
+ if len(expr.elements) < 3:
621
+ return Expr.Error("list.sort fn list", origin=expr)
622
+ fn_e, lst_e = expr.elements[1], expr.elements[2]
623
+
624
+ lst = self.machine_expr_eval(env_id, lst_e)
625
+ if isinstance(lst, Expr.Error): return lst
626
+ if not isinstance(lst, Expr.ListExpr):
627
+ return Expr.Error("second arg must be list", origin=lst_e)
628
+
629
+ elems = list(lst.elements)
630
+ for i in range(1, len(elems)):
631
+ j = i
632
+ while j > 0:
633
+ cmp_res = self.machine_expr_eval(
634
+ env_id,
635
+ Expr.ListExpr([fn_e, elems[j-1], elems[j]])
636
+ )
637
+ if isinstance(cmp_res, Expr.Error): return cmp_res
638
+ if not isinstance(cmp_res, Expr.BoolExpr):
639
+ return Expr.Error("comparator must return bool", origin=fn_e)
640
+
641
+ if cmp_res.value:
642
+ elems[j-1], elems[j] = elems[j], elems[j-1]
643
+ j -= 1
644
+ else:
645
+ break
646
+ return Expr.ListExpr(elems)
647
+
648
+ ### len: (list.len list) -> Int / (list.len ints) -> Integer(2)
649
+ elif first.value == "list.len":
650
+ if len(expr.elements) < 2:
651
+ return Expr.Error("list.len list", origin=expr)
652
+ lst_e = expr.elements[1]
653
+ lst = self.machine_expr_eval(env_id, lst_e)
654
+ if isinstance(lst, Expr.Error): return lst
655
+ if not isinstance(lst, Expr.ListExpr):
656
+ return Expr.Error("arg must be list", origin=lst_e)
657
+ return Expr.Integer(len(lst.elements))
658
+
659
+ ## Integer
660
+ ### add
483
661
  elif first.value == "+":
484
662
  args = expr.elements[1:]
485
663
  if not args:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: astreum
3
- Version: 0.2.29
3
+ Version: 0.2.30
4
4
  Summary: Python library to interact with the Astreum blockchain and its Lispeum virtual machine.
5
5
  Author-email: "Roy R. O. Okello" <roy@stelar.xyz>
6
6
  Project-URL: Homepage, https://github.com/astreum/lib
@@ -1,6 +1,6 @@
1
1
  astreum/__init__.py,sha256=y2Ok3EY_FstcmlVASr80lGR_0w-dH-SXDCCQFmL6uwA,28
2
2
  astreum/format.py,sha256=X4tG5GGPweNCE54bHYkLFiuLTbmpy5upO_s1Cef-MGA,2711
3
- astreum/node.py,sha256=OhiApRqQcPc6-CWbk2NeKdOiXQapy0mbl3uLK_xjmYU,28971
3
+ astreum/node.py,sha256=hBux-hMj2vLtXsWaEiMd9y8PgKCh3Sp1-tmaKs0B5ZU,38163
4
4
  astreum/crypto/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  astreum/crypto/ed25519.py,sha256=FRnvlN0kZlxn4j-sJKl-C9tqiz_0z4LZyXLj3KIj1TQ,1760
6
6
  astreum/crypto/quadratic_form.py,sha256=pJgbORey2NTWbQNhdyvrjy_6yjORudQ67jBz2ScHptg,4037
@@ -26,8 +26,8 @@ astreum/relay/setup.py,sha256=ynvGaJdlDtw_f5LLiow2Wo7IRzUjvgk8eSr1Sv4_zTg,2090
26
26
  astreum/storage/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
27
27
  astreum/storage/object.py,sha256=knFlvw_tpcC4twSu1DGNpHX31wlANN8E5dgEqIfU--Q,2041
28
28
  astreum/storage/setup.py,sha256=1-9ztEFI_BvRDvAA0lAn4mFya8iq65THTArlj--M3Hg,626
29
- astreum-0.2.29.dist-info/licenses/LICENSE,sha256=gYBvRDP-cPLmTyJhvZ346QkrYW_eleke4Z2Yyyu43eQ,1089
30
- astreum-0.2.29.dist-info/METADATA,sha256=JSIc8N98KW4Wmm89uMMFxyJKiEFxIU-zGRjIXf9JL24,5478
31
- astreum-0.2.29.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
32
- astreum-0.2.29.dist-info/top_level.txt,sha256=1EG1GmkOk3NPmUA98FZNdKouhRyget-KiFiMk0i2Uz0,8
33
- astreum-0.2.29.dist-info/RECORD,,
29
+ astreum-0.2.30.dist-info/licenses/LICENSE,sha256=gYBvRDP-cPLmTyJhvZ346QkrYW_eleke4Z2Yyyu43eQ,1089
30
+ astreum-0.2.30.dist-info/METADATA,sha256=LqByNjd5zu9TMcCuIw6j-YuRAEYqCQHMP1prBdjFZaM,5478
31
+ astreum-0.2.30.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
32
+ astreum-0.2.30.dist-info/top_level.txt,sha256=1EG1GmkOk3NPmUA98FZNdKouhRyget-KiFiMk0i2Uz0,8
33
+ astreum-0.2.30.dist-info/RECORD,,