astreum 0.1.4__tar.gz → 0.1.6__tar.gz

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.

Files changed (43) hide show
  1. {astreum-0.1.4 → astreum-0.1.6}/PKG-INFO +3 -3
  2. {astreum-0.1.4 → astreum-0.1.6}/README.md +1 -1
  3. {astreum-0.1.4 → astreum-0.1.6}/pyproject.toml +1 -1
  4. {astreum-0.1.4 → astreum-0.1.6}/src/astreum/__init__.py +1 -0
  5. astreum-0.1.6/src/astreum/lispeum/__init__.py +0 -0
  6. astreum-0.1.6/src/astreum/lispeum/expression.py +95 -0
  7. {astreum-0.1.4/src/astreum/machine → astreum-0.1.6/src/astreum/lispeum}/parser.py +1 -1
  8. astreum-0.1.6/src/astreum/lispeum/special/__init__.py +0 -0
  9. astreum-0.1.6/src/astreum/lispeum/special/definition.py +27 -0
  10. astreum-0.1.6/src/astreum/lispeum/special/list/__init__.py +0 -0
  11. astreum-0.1.6/src/astreum/lispeum/special/list/all.py +32 -0
  12. astreum-0.1.6/src/astreum/lispeum/special/list/any.py +32 -0
  13. astreum-0.1.6/src/astreum/lispeum/special/list/fold.py +29 -0
  14. astreum-0.1.6/src/astreum/lispeum/special/list/get.py +20 -0
  15. astreum-0.1.6/src/astreum/lispeum/special/list/insert.py +23 -0
  16. astreum-0.1.6/src/astreum/lispeum/special/list/map.py +30 -0
  17. astreum-0.1.6/src/astreum/lispeum/special/list/position.py +33 -0
  18. astreum-0.1.6/src/astreum/lispeum/special/list/remove.py +22 -0
  19. astreum-0.1.6/src/astreum/lispeum/special/number/__init__.py +0 -0
  20. astreum-0.1.6/src/astreum/lispeum/special/number/addition.py +0 -0
  21. astreum-0.1.6/src/astreum/machine/__init__.py +254 -0
  22. {astreum-0.1.4 → astreum-0.1.6}/src/astreum/machine/environment.py +10 -3
  23. astreum-0.1.6/src/astreum/node/__init__.py +416 -0
  24. astreum-0.1.6/src/astreum/node/models.py +96 -0
  25. astreum-0.1.6/src/astreum/node/relay/__init__.py +248 -0
  26. astreum-0.1.6/src/astreum/node/relay/bucket.py +80 -0
  27. astreum-0.1.6/src/astreum/node/relay/envelope.py +280 -0
  28. astreum-0.1.6/src/astreum/node/relay/message.py +105 -0
  29. astreum-0.1.6/src/astreum/node/relay/peer.py +171 -0
  30. astreum-0.1.6/src/astreum/node/relay/route.py +125 -0
  31. astreum-0.1.6/src/astreum/utils/__init__.py +0 -0
  32. astreum-0.1.6/src/astreum/utils/bytes_format.py +75 -0
  33. {astreum-0.1.4 → astreum-0.1.6}/src/astreum.egg-info/PKG-INFO +3 -3
  34. astreum-0.1.6/src/astreum.egg-info/SOURCES.txt +38 -0
  35. astreum-0.1.4/src/astreum/machine/__init__.py +0 -121
  36. astreum-0.1.4/src/astreum/machine/expression.py +0 -50
  37. astreum-0.1.4/src/astreum.egg-info/SOURCES.txt +0 -14
  38. {astreum-0.1.4 → astreum-0.1.6}/LICENSE +0 -0
  39. {astreum-0.1.4 → astreum-0.1.6}/setup.cfg +0 -0
  40. {astreum-0.1.4/src/astreum/machine → astreum-0.1.6/src/astreum/lispeum}/tokenizer.py +0 -0
  41. {astreum-0.1.4 → astreum-0.1.6}/src/astreum/machine/error.py +0 -0
  42. {astreum-0.1.4 → astreum-0.1.6}/src/astreum.egg-info/dependency_links.txt +0 -0
  43. {astreum-0.1.4 → astreum-0.1.6}/src/astreum.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: astreum
3
- Version: 0.1.4
3
+ Version: 0.1.6
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
@@ -20,5 +20,5 @@ Python library to interact with the Astreum blockchain and its Lispeum virtual m
20
20
 
21
21
  ## Testing
22
22
 
23
- python -m unittest discover -s tests
23
+ python3 -m unittest discover -s tests
24
24
 
@@ -6,5 +6,5 @@ Python library to interact with the Astreum blockchain and its Lispeum virtual m
6
6
 
7
7
  ## Testing
8
8
 
9
- python -m unittest discover -s tests
9
+ python3 -m unittest discover -s tests
10
10
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "astreum"
3
- version = "0.1.4"
3
+ version = "0.1.6"
4
4
  authors = [
5
5
  { name="Roy R. O. Okello", email="roy@stelar.xyz" },
6
6
  ]
@@ -1 +1,2 @@
1
1
  from .machine import AstreumMachine
2
+ from .node import Node
File without changes
@@ -0,0 +1,95 @@
1
+ from typing import List, Union
2
+
3
+ class Expr:
4
+ class ListExpr:
5
+ def __init__(self, elements: List['Expr']):
6
+ self.elements = elements
7
+
8
+ def __eq__(self, other):
9
+ if not isinstance(other, Expr.ListExpr):
10
+ return NotImplemented
11
+ return self.elements == other.elements
12
+
13
+ def __ne__(self, other):
14
+ return not self.__eq__(other)
15
+
16
+ @property
17
+ def value(self):
18
+ inner = " ".join(str(e) for e in self.elements)
19
+ return f"({inner})"
20
+
21
+
22
+ def __repr__(self):
23
+ if not self.elements:
24
+ return "()"
25
+
26
+ inner = " ".join(str(e) for e in self.elements)
27
+ return f"({inner})"
28
+
29
+ def __iter__(self):
30
+ return iter(self.elements)
31
+
32
+ def __getitem__(self, index: Union[int, slice]):
33
+ return self.elements[index]
34
+
35
+ def __len__(self):
36
+ return len(self.elements)
37
+
38
+ class Symbol:
39
+ def __init__(self, value: str):
40
+ self.value = value
41
+
42
+ def __repr__(self):
43
+ return self.value
44
+
45
+ class Integer:
46
+ def __init__(self, value: int):
47
+ self.value = value
48
+
49
+ def __repr__(self):
50
+ return str(self.value)
51
+
52
+ class String:
53
+ def __init__(self, value: str):
54
+ self.value = value
55
+
56
+ def __repr__(self):
57
+ return f'"{self.value}"'
58
+
59
+ class Boolean:
60
+ def __init__(self, value: bool):
61
+ self.value = value
62
+
63
+ def __repr__(self):
64
+ return "true" if self.value else "false"
65
+
66
+ class Function:
67
+ def __init__(self, params: List[str], body: 'Expr'):
68
+ self.params = params
69
+ self.body = body
70
+
71
+ def __repr__(self):
72
+ params_str = " ".join(self.params)
73
+ body_str = str(self.body)
74
+ return f"(fn ({params_str}) {body_str})"
75
+
76
+ class Error:
77
+ """
78
+ Represents an error with a freeform category and message.
79
+ - `category`: A string identifying the type of error (e.g., 'SyntaxError').
80
+ - `message`: A human-readable description of the error.
81
+ - `details`: Optional, additional information about the error.
82
+ """
83
+ def __init__(self, category: str, message: str, details: str = None):
84
+ if not category:
85
+ raise ValueError("Error category must be provided.")
86
+ if not message:
87
+ raise ValueError("Error message must be provided.")
88
+ self.category = category
89
+ self.message = message
90
+ self.details = details
91
+
92
+ def __repr__(self):
93
+ if self.details:
94
+ return f'({self.category} "{self.message}" {self.details})'
95
+ return f'({self.category} "{self.message}")'
@@ -1,6 +1,6 @@
1
1
  from typing import List, Tuple
2
2
  from astreum.machine.error import ParseError
3
- from astreum.machine.expression import Expr
3
+ from astreum.lispeum.expression import Expr
4
4
 
5
5
 
6
6
  def parse(tokens: List[str]) -> Tuple[Expr, List[str]]:
File without changes
@@ -0,0 +1,27 @@
1
+ from astreum.lispeum.expression import Expr
2
+ from astreum.machine.environment import Environment
3
+
4
+
5
+ def handle_definition(machine, args: Expr.ListExpr, env: Environment) -> Expr:
6
+ if len(args) != 2:
7
+ return Expr.Error(
8
+ category="SyntaxError",
9
+ message="def expects exactly two arguments: a symbol and an expression"
10
+ )
11
+
12
+ if not isinstance(args[0], Expr.Symbol):
13
+ return Expr.Error(
14
+ category="TypeError",
15
+ message="First argument to def must be a symbol"
16
+ )
17
+
18
+ var_name = args[0].value
19
+
20
+ result = machine.evaluate_expression(args[1], env)
21
+
22
+ if isinstance(result, Expr.Error):
23
+ return result
24
+
25
+ env.set(var_name, result)
26
+
27
+ return result
@@ -0,0 +1,32 @@
1
+ from astreum.lispeum.expression import Expr
2
+ from astreum.machine.environment import Environment
3
+
4
+ def handle_list_all(machine, list: Expr.ListExpr, predicate: Expr.Function, env: Environment) -> Expr:
5
+ if not isinstance(list, Expr.ListExpr):
6
+ return Expr.ListExpr([
7
+ Expr.ListExpr([]),
8
+ Expr.String("First argument must be a list")
9
+ ])
10
+
11
+ if not isinstance(predicate, Expr.Function):
12
+ return Expr.ListExpr([
13
+ Expr.ListExpr([]),
14
+ Expr.String("Second argument must be a function")
15
+ ])
16
+
17
+ for elem in list.elements:
18
+ new_env = Environment(parent=env)
19
+ new_env.set(predicate.params[0], elem)
20
+
21
+ result, _ = machine.evaluate_expression(predicate, new_env)
22
+
23
+ if result == Expr.Boolean(False):
24
+ return Expr.ListExpr([
25
+ Expr.Boolean(False),
26
+ Expr.ListExpr([])
27
+ ])
28
+
29
+ return Expr.ListExpr([
30
+ Expr.Boolean(True),
31
+ Expr.ListExpr([])
32
+ ])
@@ -0,0 +1,32 @@
1
+ from astreum.lispeum.expression import Expr
2
+ from astreum.machine.environment import Environment
3
+
4
+ def handle_list_any(machine, list: Expr.ListExpr, predicate: Expr.Function, env: Environment) -> Expr:
5
+ if not isinstance(list, Expr.ListExpr):
6
+ return Expr.ListExpr([
7
+ Expr.ListExpr([]),
8
+ Expr.String("First argument must be a list")
9
+ ])
10
+
11
+ if not isinstance(predicate, Expr.Function):
12
+ return Expr.ListExpr([
13
+ Expr.ListExpr([]),
14
+ Expr.String("Second argument must be a function")
15
+ ])
16
+
17
+ for elem in list.elements:
18
+ new_env = Environment(parent=env)
19
+ new_env.set(predicate.params[0], elem)
20
+
21
+ result, _ = machine.evaluate_expression(predicate, new_env)
22
+
23
+ if result == Expr.Boolean(True):
24
+ return Expr.ListExpr([
25
+ Expr.Boolean(True),
26
+ Expr.ListExpr([])
27
+ ])
28
+
29
+ return Expr.ListExpr([
30
+ Expr.Boolean(False),
31
+ Expr.ListExpr([])
32
+ ])
@@ -0,0 +1,29 @@
1
+ from astreum.lispeum.expression import Expr
2
+ from astreum.machine.environment import Environment
3
+
4
+
5
+ def handle_list_fold(machine, list: Expr.ListExpr, initial: Expr, func: Expr.Function, env: Environment) -> Expr:
6
+ if not isinstance(list, Expr.ListExpr):
7
+ return Expr.ListExpr([
8
+ Expr.ListExpr([]),
9
+ Expr.String("First argument must be a list")
10
+ ])
11
+
12
+ if not isinstance(func, Expr.Function):
13
+ return Expr.ListExpr([
14
+ Expr.ListExpr([]),
15
+ Expr.String("Third argument must be a function")
16
+ ])
17
+
18
+ result = initial
19
+ for elem in list.elements:
20
+ new_env = Environment(parent=env)
21
+ new_env.set(func.params[0], result)
22
+ new_env.set(func.params[1], elem)
23
+
24
+ result = machine.evaluate_expression(func, new_env)
25
+
26
+ return Expr.ListExpr([
27
+ result,
28
+ Expr.ListExpr([])
29
+ ])
@@ -0,0 +1,20 @@
1
+
2
+
3
+ from astreum.lispeum.expression import Expr
4
+ from astreum.machine.environment import Environment
5
+
6
+
7
+ def handle_list_get(machine, list: Expr.ListExpr, index: Expr.Integer, env: Environment) -> Expr:
8
+ if not isinstance(list, Expr.ListExpr):
9
+ return Expr.ListExpr([
10
+ Expr.ListExpr([]),
11
+ Expr.String("First argument must be a list")
12
+ ])
13
+
14
+ if index.value < 0 or index.value >= len(list.elements):
15
+ return Expr.ListExpr([
16
+ Expr.ListExpr([]),
17
+ Expr.String("Index out of bounds")
18
+ ])
19
+
20
+ return machine.evaluate_expression(list.elements[index])
@@ -0,0 +1,23 @@
1
+ from astreum.lispeum.expression import Expr
2
+
3
+
4
+ def handle_list_insert(list: Expr.ListExpr, index: Expr.Integer, value: Expr) -> Expr:
5
+ if not isinstance(list, Expr.ListExpr):
6
+ return Expr.ListExpr([
7
+ Expr.ListExpr([]),
8
+ Expr.String("First argument must be a list")
9
+ ])
10
+
11
+ if index.value < 0 or index.value > len(list.elements):
12
+ return Expr.ListExpr([
13
+ Expr.ListExpr([]),
14
+ Expr.String("Index out of bounds")
15
+ ])
16
+
17
+ new_elements = list.elements[:index.value] + [value] + list.elements[index.value:]
18
+
19
+ return Expr.ListExpr([
20
+ Expr.ListExpr(new_elements),
21
+ Expr.ListExpr([])
22
+ ])
23
+
@@ -0,0 +1,30 @@
1
+ from astreum.lispeum.expression import Expr
2
+ from astreum.machine.environment import Environment
3
+
4
+
5
+ def handle_list_map(machine, list: Expr.ListExpr, func: Expr.Function, env: Environment) -> Expr:
6
+ if not isinstance(list, Expr.ListExpr):
7
+ return Expr.ListExpr([
8
+ Expr.ListExpr([]),
9
+ Expr.String("First argument must be a list")
10
+ ])
11
+
12
+ if not isinstance(func, Expr.Function):
13
+ return Expr.ListExpr([
14
+ Expr.ListExpr([]),
15
+ Expr.String("Second argument must be a function")
16
+ ])
17
+
18
+ mapped_elements = []
19
+ for elem in list.elements:
20
+ new_env = Environment(parent=env)
21
+ new_env.set(func.params[0], elem)
22
+
23
+ mapped_elem = machine.evaluate_expression(func.body, new_env)
24
+
25
+ mapped_elements.append(mapped_elem)
26
+
27
+ return Expr.ListExpr([
28
+ Expr.ListExpr(mapped_elements),
29
+ Expr.ListExpr([])
30
+ ])
@@ -0,0 +1,33 @@
1
+ from astreum.lispeum.expression import Expr
2
+ from astreum.machine.environment import Environment
3
+
4
+
5
+ def handle_list_position(machine, list: Expr.ListExpr, predicate: Expr.Function, env: Environment) -> Expr:
6
+ if not isinstance(list, Expr.ListExpr):
7
+ return Expr.ListExpr([
8
+ Expr.ListExpr([]),
9
+ Expr.String("First argument must be a list")
10
+ ])
11
+
12
+ if not isinstance(predicate, Expr.Function):
13
+ return Expr.ListExpr([
14
+ Expr.ListExpr([]),
15
+ Expr.String("Second argument must be a function")
16
+ ])
17
+
18
+ for idx, elem in enumerate(list.elements):
19
+ new_env = Environment(parent=env)
20
+ new_env.set(predicate.params[0], elem)
21
+
22
+ result, _ = machine.evaluate_expression(predicate, new_env)
23
+
24
+ if result == Expr.Boolean(True):
25
+ return Expr.ListExpr([
26
+ Expr.Integer(idx),
27
+ Expr.ListExpr([])
28
+ ])
29
+
30
+ return Expr.ListExpr([
31
+ Expr.ListExpr([]),
32
+ Expr.ListExpr([])
33
+ ])
@@ -0,0 +1,22 @@
1
+ from astreum.lispeum.expression import Expr
2
+
3
+
4
+ def handle_list_remove(list: Expr.ListExpr, index: Expr.Integer) -> Expr:
5
+ if not isinstance(list, Expr.ListExpr):
6
+ return Expr.ListExpr([
7
+ Expr.ListExpr([]),
8
+ Expr.String("First argument must be a list")
9
+ ])
10
+
11
+ if index.value < 0 or index.value >= len(list.elements):
12
+ return Expr.ListExpr([
13
+ Expr.ListExpr([]),
14
+ Expr.String("Index out of bounds")
15
+ ])
16
+
17
+ new_elements = list.elements[:index.value] + list.elements[index.value + 1:]
18
+
19
+ return Expr.ListExpr([
20
+ Expr.ListExpr(new_elements),
21
+ Expr.ListExpr([])
22
+ ])
@@ -0,0 +1,254 @@
1
+ import threading
2
+ from typing import Dict, Optional
3
+ import uuid
4
+
5
+ from astreum.lispeum.special.definition import handle_definition
6
+ from astreum.lispeum.special.list.all import handle_list_all
7
+ from astreum.lispeum.special.list.any import handle_list_any
8
+ from astreum.lispeum.special.list.fold import handle_list_fold
9
+ from astreum.lispeum.special.list.get import handle_list_get
10
+ from astreum.lispeum.special.list.insert import handle_list_insert
11
+ from astreum.lispeum.special.list.map import handle_list_map
12
+ from astreum.lispeum.special.list.position import handle_list_position
13
+ from astreum.lispeum.special.list.remove import handle_list_remove
14
+ from astreum.machine.environment import Environment
15
+ from astreum.lispeum.expression import Expr
16
+ from astreum.lispeum.tokenizer import tokenize
17
+ from astreum.lispeum.parser import parse
18
+
19
+ class AstreumMachine:
20
+ def __init__(self, node: 'Node' = None):
21
+ self.global_env = Environment(node=node)
22
+ self.sessions: Dict[str, Environment] = {}
23
+ self.lock = threading.Lock()
24
+
25
+
26
+ def create_session(self) -> str:
27
+ session_id = str(uuid.uuid4())
28
+ with self.lock:
29
+ self.sessions[session_id] = Environment(parent=self.global_env)
30
+ return session_id
31
+
32
+ def terminate_session(self, session_id: str) -> bool:
33
+ with self.lock:
34
+ if session_id in self.sessions:
35
+ del self.sessions[session_id]
36
+ return True
37
+ else:
38
+ return False
39
+
40
+ def get_session_env(self, session_id: str) -> Optional[Environment]:
41
+ with self.lock:
42
+ return self.sessions.get(session_id, None)
43
+
44
+ def evaluate_code(self, code: str, session_id: str) -> Expr:
45
+ session_env = self.get_session_env(session_id)
46
+ if session_env is None:
47
+ raise ValueError(f"Session ID {session_id} not found.")
48
+
49
+ try:
50
+ tkns = tokenize(input=code)
51
+ expr, _ = parse(tokens=tkns)
52
+ result = self.evaluate_expression(expr, session_env)
53
+ return result
54
+
55
+ except Exception as e:
56
+ raise ValueError(e)
57
+
58
+ def evaluate_expression(self, expr: Expr, env: Environment) -> Expr:
59
+ if isinstance(expr, Expr.Boolean) or isinstance(expr, Expr.Integer) or isinstance(expr, Expr.String) or isinstance(expr, Expr.Error):
60
+ return expr
61
+
62
+ elif isinstance(expr, Expr.Symbol):
63
+ value = env.get(expr.value)
64
+
65
+ if value:
66
+ return value
67
+ else:
68
+ return Expr.Error(
69
+ category="NameError",
70
+ message=f"Variable '{expr.value}' not found in environments."
71
+ )
72
+
73
+ elif isinstance(expr, Expr.ListExpr):
74
+
75
+ if len(expr.elements) == 0:
76
+ return expr
77
+
78
+ if len(expr.elements) == 1:
79
+ return self.evaluate_expression(expr=expr.elements[0], env=env)
80
+
81
+ first = expr.elements[0]
82
+
83
+ if isinstance(first, Expr.Symbol):
84
+
85
+ first_symbol_value = env.get(first.value)
86
+
87
+ if first_symbol_value and not isinstance(first_symbol_value, Expr.Function):
88
+ evaluated_elements = [self.evaluate_expression(e, env) for e in expr.elements]
89
+ return Expr.ListExpr(evaluated_elements)
90
+
91
+ elif first.value == "def":
92
+ return handle_definition(
93
+ machine=self,
94
+ args=expr.elements[1:],
95
+ env=env
96
+ )
97
+
98
+ # List
99
+ elif first.value == "list.new":
100
+ return Expr.ListExpr([self.evaluate_expression(arg, env) for arg in expr.elements[1:]])
101
+
102
+ elif first.value == "list.get":
103
+ args = expr.elements[1:]
104
+ if len(args) != 2:
105
+ return Expr.Error(
106
+ category="SyntaxError",
107
+ message="list.get expects exactly two arguments: a list and an index"
108
+ )
109
+ list_obj = self.evaluate_expression(args[0], env)
110
+ index = self.evaluate_expression(args[1], env)
111
+ return handle_list_get(self, list_obj, index, env)
112
+
113
+ elif first.value == "list.insert":
114
+ args = expr.elements[1:]
115
+ if len(args) != 3:
116
+ return Expr.ListExpr([
117
+ Expr.ListExpr([]),
118
+ Expr.String("list.insert expects exactly three arguments: a list, an index, and a value")
119
+ ])
120
+
121
+ return handle_list_insert(
122
+ list=self.evaluate_expression(args[0], env),
123
+ index=self.evaluate_expression(args[1], env),
124
+ value=self.evaluate_expression(args[2], env),
125
+ )
126
+
127
+ elif first.value == "list.remove":
128
+ args = expr.elements[1:]
129
+ if len(args) != 2:
130
+ return Expr.ListExpr([
131
+ Expr.ListExpr([]),
132
+ Expr.String("list.remove expects exactly two arguments: a list and an index")
133
+ ])
134
+
135
+ return handle_list_remove(
136
+ list=self.evaluate_expression(args[0], env),
137
+ index=self.evaluate_expression(args[1], env),
138
+ )
139
+
140
+ elif first.value == "list.length":
141
+ args = expr.elements[1:]
142
+ if len(args) != 1:
143
+ return Expr.ListExpr([
144
+ Expr.ListExpr([]),
145
+ Expr.String("list.length expects exactly one argument: a list")
146
+ ])
147
+
148
+ list_obj = self.evaluate_expression(args[0], env)
149
+ if not isinstance(list_obj, Expr.ListExpr):
150
+ return Expr.ListExpr([
151
+ Expr.ListExpr([]),
152
+ Expr.String("Argument must be a list")
153
+ ])
154
+
155
+ return Expr.ListExpr([
156
+ Expr.Integer(len(list_obj.elements)),
157
+ Expr.ListExpr([])
158
+ ])
159
+
160
+ elif first.value == "list.fold":
161
+ if len(args) != 3:
162
+ return Expr.ListExpr([
163
+ Expr.ListExpr([]),
164
+ Expr.String("list.fold expects exactly three arguments: a list, an initial value, and a function")
165
+ ])
166
+
167
+ return handle_list_fold(
168
+ machine=self,
169
+ list=self.evaluate_expression(args[0], env),
170
+ initial=self.evaluate_expression(args[1], env),
171
+ func=self.evaluate_expression(args[2], env),
172
+ env=env,
173
+ )
174
+
175
+ elif first.value == "list.map":
176
+ if len(args) != 2:
177
+ return Expr.ListExpr([
178
+ Expr.ListExpr([]),
179
+ Expr.String("list.map expects exactly two arguments: a list and a function")
180
+ ])
181
+
182
+ return handle_list_map(
183
+ machine=self,
184
+ list=self.evaluate_expression(args[0], env),
185
+ func=self.evaluate_expression(args[1], env),
186
+ env=env,
187
+ )
188
+
189
+ elif first.value == "list.position":
190
+ if len(args) != 2:
191
+ return Expr.ListExpr([
192
+ Expr.ListExpr([]),
193
+ Expr.String("list.position expects exactly two arguments: a list and a function")
194
+ ])
195
+
196
+ return handle_list_position(
197
+ machine=self,
198
+ list=self.evaluate_expression(args[0], env),
199
+ predicate=self.evaluate_expression(args[1], env),
200
+ env=env,
201
+ )
202
+
203
+ elif first.value == "list.any":
204
+ if len(args) != 2:
205
+ return Expr.ListExpr([
206
+ Expr.ListExpr([]),
207
+ Expr.String("list.any expects exactly two arguments: a list and a function")
208
+ ])
209
+
210
+ return handle_list_any(
211
+ machine=self,
212
+ list=self.evaluate_expression(args[0], env),
213
+ predicate=self.evaluate_expression(args[1], env),
214
+ env=env,
215
+ )
216
+
217
+ elif first.value == "list.all":
218
+ if len(args) != 2:
219
+ return Expr.ListExpr([
220
+ Expr.ListExpr([]),
221
+ Expr.String("list.all expects exactly two arguments: a list and a function")
222
+ ])
223
+
224
+ return handle_list_all(
225
+ machine=self,
226
+ list=self.evaluate_expression(args[0], env),
227
+ predicate=self.evaluate_expression(args[1], env),
228
+ env=env,
229
+ )
230
+
231
+ # Number
232
+ elif first.value == "+":
233
+ evaluated_args = [self.evaluate_expression(arg, env) for arg in expr.elements[1:]]
234
+
235
+ # Check for non-integer arguments
236
+ if not all(isinstance(arg, Expr.Integer) for arg in evaluated_args):
237
+ return Expr.Error(
238
+ category="TypeError",
239
+ message="All arguments to + must be integers"
240
+ )
241
+
242
+ # Sum up the integer values
243
+ result = sum(arg.value for arg in evaluated_args)
244
+ return Expr.Integer(result)
245
+
246
+ else:
247
+ evaluated_elements = [self.evaluate_expression(e, env) for e in expr.elements]
248
+ return Expr.ListExpr(evaluated_elements)
249
+
250
+ elif isinstance(expr, Expr.Function):
251
+ return expr
252
+
253
+ else:
254
+ raise ValueError(f"Unknown expression type: {type(expr)}")