viv-compiler 0.1.1__py3-none-any.whl → 0.1.2__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.
@@ -1,5 +1,6 @@
1
1
  """Utility functions used by various components of the Viv DSL compiler."""
2
2
 
3
+ import viv_compiler.config
3
4
  import viv_compiler.types
4
5
  from viv_compiler.types import ExpressionDiscriminator
5
6
  from typing import Any
@@ -14,55 +15,44 @@ def get_all_role_names(action_definition: viv_compiler.types.ActionDefinition) -
14
15
  Returns:
15
16
  A set containing the names of all roles associated with the given action definition.
16
17
  """
17
- return {'hearer', 'this', 'default'} | set(action_definition['roles'])
18
+ return viv_compiler.config.SPECIAL_ROLE_NAMES | set(action_definition['roles'])
18
19
 
19
20
 
20
21
  def get_all_referenced_roles(
21
22
  ast_chunk: Any,
22
23
  ignore_role_unpackings: bool = False,
23
- ignore: Any = None
24
24
  ) -> list[viv_compiler.types.RoleName]:
25
25
  """Return a list of all roles referenced in the given AST chunk.
26
26
 
27
27
  Args:
28
28
  ast_chunk: The full or partial AST to search for role references.
29
29
  ignore_role_unpackings: Whether to search for role references inside role unpackings.
30
- ignore: Only used internally, via recursive calls to this function.
31
30
 
32
31
  Returns:
33
32
  A list containing the names of all roles referenced in the given AST chunk.
34
33
  """
35
- ignore = ignore or ["this"]
36
- roles_referenced_so_far = []
34
+ roles_referenced_so_far: list[viv_compiler.types.RoleName] = []
37
35
  if isinstance(ast_chunk, list):
38
36
  for element in ast_chunk:
39
37
  roles_referenced_so_far.extend(get_all_referenced_roles(
40
38
  ast_chunk=element,
41
39
  ignore_role_unpackings=ignore_role_unpackings,
42
- ignore=ignore
43
40
  ))
44
41
  elif isinstance(ast_chunk, dict):
45
- if ast_chunk.get('type') == ExpressionDiscriminator.ENTITY_REFERENCE:
46
- referenced_role = ast_chunk['value']['anchor']
47
- roles_referenced_so_far.append(referenced_role)
48
- elif not ignore_role_unpackings and ast_chunk.get('type') == ExpressionDiscriminator.ROLE_UNPACKING:
49
- referenced_role = ast_chunk['value']
50
- roles_referenced_so_far.append(referenced_role)
51
- else:
52
- scoped_ignore = ignore
53
- if ast_chunk.get('type') == ExpressionDiscriminator.LOOP:
54
- scoped_ignore = [*ignore, ast_chunk['value']['variable']]
55
- iterable = ast_chunk['value']['iterable']
56
- if iterable.get('type') == ExpressionDiscriminator.ROLE_UNPACKING:
57
- if not ignore_role_unpackings:
58
- roles_referenced_so_far.append(iterable['value'])
59
- for key, value in ast_chunk.items():
60
- roles_referenced_so_far.extend(get_all_referenced_roles(
61
- ast_chunk=value,
62
- ignore_role_unpackings=ignore_role_unpackings,
63
- ignore=scoped_ignore
64
- ))
65
- return list(set(filter(lambda role: role not in ignore, roles_referenced_so_far)))
42
+ ast_chunk_type = ast_chunk.get('type')
43
+ if ast_chunk_type in (ExpressionDiscriminator.ENTITY_REFERENCE, ExpressionDiscriminator.SYMBOL_REFERENCE):
44
+ if not ast_chunk['value']['local']:
45
+ if ast_chunk['value']['anchor'] != viv_compiler.config.ACTION_SELF_REFERENCE_ROLE_NAME:
46
+ roles_referenced_so_far.append(ast_chunk['value']['anchor'])
47
+ elif ast_chunk_type == ExpressionDiscriminator.ROLE_UNPACKING and not ignore_role_unpackings:
48
+ referenced_role_name = ast_chunk['value']
49
+ roles_referenced_so_far.append(referenced_role_name)
50
+ for value in ast_chunk.values():
51
+ roles_referenced_so_far.extend(get_all_referenced_roles(
52
+ ast_chunk=value,
53
+ ignore_role_unpackings=ignore_role_unpackings,
54
+ ))
55
+ return list(set(roles_referenced_so_far))
66
56
 
67
57
 
68
58
  def get_all_referenced_enum_names(ast_chunk: Any) -> list[viv_compiler.types.EnumName]:
@@ -77,9 +67,12 @@ def get_all_referenced_enum_names(ast_chunk: Any) -> list[viv_compiler.types.Enu
77
67
  Returns:
78
68
  A list containing the names of all the enums referenced in the given AST chunk.
79
69
  """
80
- all_enum_expressions = get_all_expressions_of_type(expression_type="enum", ast_chunk=ast_chunk)
81
- all_referenced_enum_names = [expression['name'] for expression in all_enum_expressions]
82
- return all_referenced_enum_names
70
+ all_enum_expressions = get_all_expressions_of_type(
71
+ expression_type=ExpressionDiscriminator.ENUM,
72
+ ast_chunk=ast_chunk
73
+ )
74
+ all_referenced_enum_names = {expression['name'] for expression in all_enum_expressions}
75
+ return sorted(all_referenced_enum_names)
83
76
 
84
77
 
85
78
  def get_all_referenced_adapter_function_names(ast_chunk: Any) -> list[viv_compiler.types.AdapterFunctionName]:
@@ -95,11 +88,78 @@ def get_all_referenced_adapter_function_names(ast_chunk: Any) -> list[viv_compil
95
88
  A list containing the names of all the adapter functions referenced in the given AST chunk.
96
89
  """
97
90
  all_adapter_function_references = get_all_expressions_of_type(
98
- expression_type="adapterFunctionCall",
91
+ expression_type=ExpressionDiscriminator.ADAPTER_FUNCTION_CALL,
92
+ ast_chunk=ast_chunk
93
+ )
94
+ all_referenced_adapter_function_names = {expression['name'] for expression in all_adapter_function_references}
95
+ return sorted(all_referenced_adapter_function_names)
96
+
97
+
98
+ def get_all_assigned_scratch_variable_names(ast_chunk: Any) -> list[viv_compiler.types.VariableName]:
99
+ """Return a list of the names of all scratch variables that are assigned anywhere in the given AST chunk.
100
+
101
+ Args:
102
+ ast_chunk: The full or partial AST to search for scratch variables being assigned.
103
+
104
+ Returns:
105
+ A list containing the names of all scratch variables that are assigned anywhere in the given AST chunk.
106
+ """
107
+ all_assigned_scratch_variable_names = set()
108
+ all_assignments = get_all_expressions_of_type(
109
+ expression_type=ExpressionDiscriminator.ASSIGNMENT,
110
+ ast_chunk=ast_chunk
111
+ )
112
+ for assignment in all_assignments:
113
+ # Move on if the effective prefix is not `@this.scratch.`
114
+ lhs = assignment['left']
115
+ if lhs['type'] != ExpressionDiscriminator.ENTITY_REFERENCE:
116
+ continue
117
+ if lhs['value']['local']:
118
+ continue
119
+ if lhs['value']['anchor'] != viv_compiler.config.SCRATCH_VARIABLE_REFERENCE_ANCHOR:
120
+ continue
121
+ if lhs['value']['path'][0] != viv_compiler.config.SCRATCH_VARIABLE_REFERENCE_PATH_PREFIX[0]:
122
+ continue
123
+ # Now determine if the LHS of the assignment is a bare scratch variable, i.e., an effective `@this.scratch.var`
124
+ head, *tail = lhs['value']['path'][1:]
125
+ if tail:
126
+ continue
127
+ if 'name' not in head:
128
+ # Move on if we somehow have a pointer or lookup after an effective `@this.scratch` (not idiomatic)
129
+ continue
130
+ all_assigned_scratch_variable_names.add(head['name'])
131
+ return sorted(all_assigned_scratch_variable_names)
132
+
133
+
134
+ def get_all_referenced_scratch_variable_names(ast_chunk: Any) -> list[viv_compiler.types.VariableName]:
135
+ """Return a list of the names of all scratch variables that are referenced anywhere in the given AST chunk.
136
+
137
+ Args:
138
+ ast_chunk: The full or partial AST to search for scratch variables being referenced.
139
+
140
+ Returns:
141
+ A list containing the names of all scratch variables that are referenced anywhere in the given AST chunk.
142
+ """
143
+ all_referenced_scratch_variable_names = set()
144
+ all_entity_references = get_all_expressions_of_type(
145
+ expression_type=ExpressionDiscriminator.ENTITY_REFERENCE,
99
146
  ast_chunk=ast_chunk
100
147
  )
101
- all_referenced_adapter_function_names = [expression['name'] for expression in all_adapter_function_references]
102
- return all_referenced_adapter_function_names
148
+ for entity_reference in all_entity_references:
149
+ # Move on if the effective prefix is not `@this.scratch.`
150
+ if entity_reference['local']:
151
+ continue
152
+ if entity_reference['anchor'] != viv_compiler.config.SCRATCH_VARIABLE_REFERENCE_ANCHOR:
153
+ continue
154
+ if entity_reference['path'][0] != viv_compiler.config.SCRATCH_VARIABLE_REFERENCE_PATH_PREFIX[0]:
155
+ continue
156
+ # Now determine if the LHS of the assignment is a bare scratch variable, i.e., an effective `@this.scratch.var`
157
+ head, *_ = entity_reference['path'][1:]
158
+ if 'name' not in head:
159
+ # Move on if we somehow have a pointer or lookup after an effective `@this.scratch` (not idiomatic)
160
+ continue
161
+ all_referenced_scratch_variable_names.add(head['name'])
162
+ return sorted(all_referenced_scratch_variable_names)
103
163
 
104
164
 
105
165
  def get_all_expressions_of_type(expression_type: str, ast_chunk: Any) -> list[Any]:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: viv-compiler
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: Compiler for the DSL of Viv, an action system for emergent narrative.
5
5
  Author-email: James Ryan <mail@jamesryan.ai>
6
6
  License: MIT License
@@ -45,7 +45,7 @@ Dynamic: license-file
45
45
 
46
46
  This package contains the reference compiler for the domain-specific language (DSL) at the heart of [Viv](https://github.com/james-owen-ryan/viv), an action system for emergent narrative.
47
47
 
48
- The Viv compiler accepts a *Viv source file* (`.viv`) and produces a *Viv content bundle* in a JSON-serializable format conforming to the `CompiledContentBundle` schema defined [here](https://github.com/james-owen-ryan/viv/blob/main/compiler/src/viv_compiler/types/dsl_public_schemas.py), making it ready for usage in any Viv runtime.
48
+ The Viv compiler accepts a **Viv source file** (`.viv`) and produces a **Viv content bundle** in a JSON-serializable format conforming to the `CompiledContentBundle` schema defined [here](https://github.com/james-owen-ryan/viv/blob/main/compiler/src/viv_compiler/types/dsl_public_schemas.py), making it ready for usage in any Viv runtime with the same version number as the compiler.
49
49
 
50
50
  Once you've installed this package, you'll have access to the two compiler interfaces that are documented below:
51
51
 
@@ -65,17 +65,17 @@ Once you've installed this package, you'll have access to the two compiler inter
65
65
 
66
66
  ## Installation
67
67
 
68
- Install from PyPI:
68
+ * Install from PyPI:
69
69
 
70
- ```
71
- pip install viv-compiler
72
- ```
70
+ ```
71
+ pip install viv-compiler
72
+ ```
73
73
 
74
- Smoke test to confirm your installation looks good:
74
+ * Run a smoke test to confirm your installation looks good:
75
75
 
76
- ```
77
- vivc --test
78
- ```
76
+ ```
77
+ vivc --test
78
+ ```
79
79
 
80
80
 
81
81
  ## Command-Line Interface (CLI)
@@ -226,13 +226,13 @@ Once you've installed `viv-compiler`, the Viv compiler Python API can be invoked
226
226
 
227
227
  ```python
228
228
  from pathlib import Path
229
- from viv_compiler import compile_from_path
229
+ from viv_compiler import compile_from_path, VivCompileError
230
230
 
231
231
  try:
232
- content_bundle = compile_from_path(source_file_path=Path("my-actions.viv"))
233
- print("Compilation succeeded:", content_bundle)
232
+ content_bundle = compile_from_path(source_file_path=Path("my-actions.viv"))
233
+ print("Compilation succeeded:", content_bundle)
234
234
  except VivCompileError as e:
235
- print("Compilation failed:", e)
235
+ print("Compilation failed:", e)
236
236
  ```
237
237
 
238
238
 
@@ -1,33 +1,33 @@
1
1
  viv_compiler/__init__.py,sha256=qYCGYC46CCguVk7LybHbWlV1b4dPY7K0UOfrEF8k0E8,592
2
2
  viv_compiler/__main__.py,sha256=dyCH7r86O9Nw516E38Gm0gQjcSYDd57dhLGRBPoCi7s,60
3
+ viv_compiler/_version.py,sha256=VZZTZ-YxtTb-5y0wP8QpegYjFIf2mRkpqDjOGa99DAM,88
3
4
  viv_compiler/api.py,sha256=8vr_WoLEzPSOMRNmovewE2f4b8_-r5xp5PdYc7S2rgc,2164
4
- viv_compiler/cli.py,sha256=BEMXRpOfPal7sri_fzY2zPdeE6QmxgITatfMZoh3HVM,8448
5
+ viv_compiler/cli.py,sha256=RLyeQFKts85vLVcVy2dzcsG8U4efRhB_CBIotko50H8,8481
5
6
  viv_compiler/py.typed,sha256=qrkHrYJvGoZpU2BpVLNxJB44LlhqVSKyYOwD_L_1m3s,10
6
7
  viv_compiler/_samples/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
8
  viv_compiler/_samples/smoke-test.viv,sha256=hBgnuOvO7A78iDZPTM6Z6xlkaLAA9zgmkuxPVwCLUGQ,152
8
9
  viv_compiler/backports/__init__.py,sha256=OozGI7C0wkn6Xjn19DPE_SSdSKX-gRiOQu8yooJ382E,25
9
10
  viv_compiler/backports/backports.py,sha256=NkZcknH0-iuZczNfGSlL-3yy9FpL9-Y9j_H65TQs_FA,243
10
11
  viv_compiler/config/__init__.py,sha256=A00lpnUKg-q3y_b-cN3jKF9urkGTGjb2Yz_gGc9WCLs,22
11
- viv_compiler/config/config.py,sha256=y8R2W2mctP6lbyB_aqIAmJINDIqAAXtzPJrA6lYv-bk,3981
12
+ viv_compiler/config/config.py,sha256=mS9QHlpB1mw_PDADp7utOuaGE0a2cBEyzUPDHQXAjFI,3802
12
13
  viv_compiler/core/__init__.py,sha256=ZAU-F0_tXtRWRmw-anqCdZZDabN42nCrIfm_74RdBpE,123
13
- viv_compiler/core/core.py,sha256=bJzNpf6DM8F1ITn7vmCRxsCELnZ2FgY_23eCxULOGao,7828
14
+ viv_compiler/core/core.py,sha256=mkaroUrEyN9MFucvB5GOS-8E5gKAApPdyRp8U0yfAjQ,7747
14
15
  viv_compiler/core/includes.py,sha256=Peth-YttHzbyTB9NXoyBKSDKBJ_NX6MEwOTGEUcHvIg,4879
15
- viv_compiler/core/metadata.py,sha256=QZToKtisN-ZYNckJsejMaGwXsMSR_TI11MbIuAl8_No,2894
16
+ viv_compiler/core/metadata.py,sha256=8Oa3PtGDk94pBYr_f6CkqlebHvfYXxgtIluaoGGiluA,2988
16
17
  viv_compiler/core/postprocessing.py,sha256=fgogeBR4U92_dH-GBNDFrZqv9CuzCI1nlx7xHs3j6Iw,39073
17
- viv_compiler/core/validation.py,sha256=_fYvYxl2keAdF0KFx41c3JIl4EchrI9feVSM8VXj-fE,41000
18
- viv_compiler/core/visitor.py,sha256=ZF6h_Ebj8nujUbxDjNSxK-U2st_Bl6ZX3UFYeu5jBdE,47931
18
+ viv_compiler/core/validation.py,sha256=EqTRDJ8TKSElDCTzDgKGDmlXHrdN_jzteF0_XYPfcRM,48380
19
+ viv_compiler/core/visitor.py,sha256=tzJzk_t8qDqaWjioEqSraz8r4nl34KtDqx3KUmr9qek,48257
19
20
  viv_compiler/grammar/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
20
- viv_compiler/grammar/viv.peg,sha256=9xGvGl_TgNp0oX66um3Z2RQzG8MOJGBrDEiHoW6sP0I,11152
21
+ viv_compiler/grammar/viv.peg,sha256=QTROPPHWj9h7kVD91ozZo-OHxzno0Q9PsIx1sHU9nx0,11161
21
22
  viv_compiler/types/__init__.py,sha256=FLwJ0wV_vdR95waeHfZ4YnUIvkK7v8RqR139lpnP1RE,102
22
- viv_compiler/types/content_public_schemas.py,sha256=WOHhgjIhoB-jiqHXDjqmnbY0V-QtxMQO9TBo_vOneo4,23422
23
- viv_compiler/types/dsl_public_schemas.py,sha256=Jzy3zxtjyrnorgD52Lb39t3GCMd-vSRGKYXdBkG2dQA,22556
23
+ viv_compiler/types/content_public_schemas.py,sha256=mDM4iDQdBeqhN38r6wK58jXBzSej3pspkpNjPOiG8b8,23768
24
+ viv_compiler/types/dsl_public_schemas.py,sha256=8J243Pball02p0CmAYey_ikFsSOXFWTxc0oqLdGQCwA,22566
24
25
  viv_compiler/types/internal_types.py,sha256=LOU4kT_tjxjU0CXqnqfluV-EBgX2KkhmUOa07B0i1xo,7816
25
26
  viv_compiler/utils/__init__.py,sha256=alIDGBnxWH4JvP-UW-7N99seBBi0r1GV1h8f1ERFBec,21
26
- viv_compiler/utils/_version.py,sha256=B40VyT6r1P2PNwgWWItzAYhh25_deCqUcl4EaXq0Beg,88
27
- viv_compiler/utils/utils.py,sha256=TwT9VKV1fXVFLToSXO-Cu046Gu2siA_Y7xGog_Zx9pA,7257
28
- viv_compiler-0.1.1.dist-info/licenses/LICENSE,sha256=_N1SL7eyeeqRS4HHgl_n85kuwUSw22qWP6IpQZ9F_M4,1066
29
- viv_compiler-0.1.1.dist-info/METADATA,sha256=MZmgisVHYAOH3B9lZTgkaZgienTQYfAWrRL_f5e_fTU,8427
30
- viv_compiler-0.1.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
31
- viv_compiler-0.1.1.dist-info/entry_points.txt,sha256=YluMB1bLcPmnfaeAaMAtxXsKCkIEvyhb2dVgkVp55Ko,84
32
- viv_compiler-0.1.1.dist-info/top_level.txt,sha256=fd_4ocrOmLnp5oEnwxv2_Yt_JkluHdKD-Jr5eN__iaE,13
33
- viv_compiler-0.1.1.dist-info/RECORD,,
27
+ viv_compiler/utils/utils.py,sha256=FWIFPFR8Xgub1nXc179qsvpfOW8JnGkpYywVf93PkSg,10070
28
+ viv_compiler-0.1.2.dist-info/licenses/LICENSE,sha256=_N1SL7eyeeqRS4HHgl_n85kuwUSw22qWP6IpQZ9F_M4,1066
29
+ viv_compiler-0.1.2.dist-info/METADATA,sha256=LjOlZXzAm-8-6IpgmL-hFE7xRpyDfE2w4oracAAshas,8500
30
+ viv_compiler-0.1.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
31
+ viv_compiler-0.1.2.dist-info/entry_points.txt,sha256=YluMB1bLcPmnfaeAaMAtxXsKCkIEvyhb2dVgkVp55Ko,84
32
+ viv_compiler-0.1.2.dist-info/top_level.txt,sha256=fd_4ocrOmLnp5oEnwxv2_Yt_JkluHdKD-Jr5eN__iaE,13
33
+ viv_compiler-0.1.2.dist-info/RECORD,,