astreum 0.1.4__py3-none-any.whl → 0.1.6__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/__init__.py +1 -0
- astreum/lispeum/__init__.py +0 -0
- astreum/lispeum/expression.py +95 -0
- astreum/{machine → lispeum}/parser.py +1 -1
- astreum/lispeum/special/__init__.py +0 -0
- astreum/lispeum/special/definition.py +27 -0
- astreum/lispeum/special/list/__init__.py +0 -0
- astreum/lispeum/special/list/all.py +32 -0
- astreum/lispeum/special/list/any.py +32 -0
- astreum/lispeum/special/list/fold.py +29 -0
- astreum/lispeum/special/list/get.py +20 -0
- astreum/lispeum/special/list/insert.py +23 -0
- astreum/lispeum/special/list/map.py +30 -0
- astreum/lispeum/special/list/position.py +33 -0
- astreum/lispeum/special/list/remove.py +22 -0
- astreum/lispeum/special/number/__init__.py +0 -0
- astreum/lispeum/special/number/addition.py +0 -0
- astreum/machine/__init__.py +186 -53
- astreum/machine/environment.py +10 -3
- astreum/node/__init__.py +416 -0
- astreum/node/models.py +96 -0
- astreum/node/relay/__init__.py +248 -0
- astreum/node/relay/bucket.py +80 -0
- astreum/node/relay/envelope.py +280 -0
- astreum/node/relay/message.py +105 -0
- astreum/node/relay/peer.py +171 -0
- astreum/node/relay/route.py +125 -0
- astreum/utils/__init__.py +0 -0
- astreum/utils/bytes_format.py +75 -0
- {astreum-0.1.4.dist-info → astreum-0.1.6.dist-info}/METADATA +3 -3
- astreum-0.1.6.dist-info/RECORD +36 -0
- {astreum-0.1.4.dist-info → astreum-0.1.6.dist-info}/WHEEL +1 -1
- astreum/machine/expression.py +0 -50
- astreum-0.1.4.dist-info/RECORD +0 -12
- /astreum/{machine → lispeum}/tokenizer.py +0 -0
- {astreum-0.1.4.dist-info → astreum-0.1.6.dist-info}/LICENSE +0 -0
- {astreum-0.1.4.dist-info → astreum-0.1.6.dist-info}/top_level.txt +0 -0
astreum/__init__.py
CHANGED
|
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}")'
|
|
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
|
|
File without changes
|
|
@@ -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
|
+
])
|
|
File without changes
|
|
File without changes
|
astreum/machine/__init__.py
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
import threading
|
|
2
|
-
from typing import
|
|
2
|
+
from typing import Dict, Optional
|
|
3
3
|
import uuid
|
|
4
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
|
|
5
14
|
from astreum.machine.environment import Environment
|
|
6
|
-
from astreum.
|
|
7
|
-
from astreum.
|
|
8
|
-
from astreum.
|
|
15
|
+
from astreum.lispeum.expression import Expr
|
|
16
|
+
from astreum.lispeum.tokenizer import tokenize
|
|
17
|
+
from astreum.lispeum.parser import parse
|
|
9
18
|
|
|
10
19
|
class AstreumMachine:
|
|
11
|
-
def __init__(self):
|
|
12
|
-
self.global_env = Environment()
|
|
13
|
-
|
|
20
|
+
def __init__(self, node: 'Node' = None):
|
|
21
|
+
self.global_env = Environment(node=node)
|
|
14
22
|
self.sessions: Dict[str, Environment] = {}
|
|
15
|
-
|
|
16
23
|
self.lock = threading.Lock()
|
|
24
|
+
|
|
17
25
|
|
|
18
26
|
def create_session(self) -> str:
|
|
19
27
|
session_id = str(uuid.uuid4())
|
|
@@ -33,36 +41,42 @@ class AstreumMachine:
|
|
|
33
41
|
with self.lock:
|
|
34
42
|
return self.sessions.get(session_id, None)
|
|
35
43
|
|
|
36
|
-
def evaluate_code(self, code: str, session_id: str) ->
|
|
44
|
+
def evaluate_code(self, code: str, session_id: str) -> Expr:
|
|
37
45
|
session_env = self.get_session_env(session_id)
|
|
38
46
|
if session_env is None:
|
|
39
|
-
|
|
47
|
+
raise ValueError(f"Session ID {session_id} not found.")
|
|
40
48
|
|
|
41
49
|
try:
|
|
42
50
|
tkns = tokenize(input=code)
|
|
43
51
|
expr, _ = parse(tokens=tkns)
|
|
44
52
|
result = self.evaluate_expression(expr, session_env)
|
|
45
|
-
return result
|
|
53
|
+
return result
|
|
54
|
+
|
|
46
55
|
except Exception as e:
|
|
47
|
-
|
|
56
|
+
raise ValueError(e)
|
|
48
57
|
|
|
49
58
|
def evaluate_expression(self, expr: Expr, env: Environment) -> Expr:
|
|
50
|
-
if isinstance(expr, Expr.Integer):
|
|
51
|
-
return expr
|
|
52
|
-
|
|
53
|
-
elif isinstance(expr, Expr.String):
|
|
59
|
+
if isinstance(expr, Expr.Boolean) or isinstance(expr, Expr.Integer) or isinstance(expr, Expr.String) or isinstance(expr, Expr.Error):
|
|
54
60
|
return expr
|
|
55
61
|
|
|
56
62
|
elif isinstance(expr, Expr.Symbol):
|
|
57
63
|
value = env.get(expr.value)
|
|
58
|
-
|
|
64
|
+
|
|
65
|
+
if value:
|
|
59
66
|
return value
|
|
60
67
|
else:
|
|
61
|
-
|
|
68
|
+
return Expr.Error(
|
|
69
|
+
category="NameError",
|
|
70
|
+
message=f"Variable '{expr.value}' not found in environments."
|
|
71
|
+
)
|
|
62
72
|
|
|
63
73
|
elif isinstance(expr, Expr.ListExpr):
|
|
64
|
-
|
|
65
|
-
|
|
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)
|
|
66
80
|
|
|
67
81
|
first = expr.elements[0]
|
|
68
82
|
|
|
@@ -72,50 +86,169 @@ class AstreumMachine:
|
|
|
72
86
|
|
|
73
87
|
if first_symbol_value and not isinstance(first_symbol_value, Expr.Function):
|
|
74
88
|
evaluated_elements = [self.evaluate_expression(e, env) for e in expr.elements]
|
|
75
|
-
return Expr.ListExpr(evaluated_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":
|
|
76
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
|
+
])
|
|
77
120
|
|
|
78
|
-
|
|
79
|
-
|
|
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
|
+
])
|
|
80
134
|
|
|
81
|
-
|
|
82
|
-
|
|
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
|
+
])
|
|
83
147
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
+
])
|
|
88
154
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
+
)
|
|
94
202
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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)
|
|
114
245
|
|
|
115
246
|
else:
|
|
116
247
|
evaluated_elements = [self.evaluate_expression(e, env) for e in expr.elements]
|
|
117
248
|
return Expr.ListExpr(evaluated_elements)
|
|
249
|
+
|
|
118
250
|
elif isinstance(expr, Expr.Function):
|
|
119
251
|
return expr
|
|
252
|
+
|
|
120
253
|
else:
|
|
121
254
|
raise ValueError(f"Unknown expression type: {type(expr)}")
|