pineforge-codegen 1.0.1__py3-none-any.whl → 1.2.0__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.
@@ -6,6 +6,7 @@ from .lexer import Lexer
6
6
  from .parser import Parser
7
7
  from .analyzer import Analyzer
8
8
  from .codegen import CodeGen
9
+ from .diagnostic_codes import diagnostics_catalog, render_diagnostic
9
10
  from .errors import CompileError, Level, Phase
10
11
  from .external_requests import lower_no_data_requests
11
12
  from .builtin_keywords import bind_builtin_keywords
@@ -13,6 +14,7 @@ from .finite_ta_length import expand_finite_choice_extrema_lengths
13
14
  from .library_inline import inline_libraries
14
15
  from .limits import TimeBudget, check_ast_depth, check_source_size, ensure_recursion_headroom
15
16
  from .pragmas import extract_pf_trace_pragmas
17
+ from .request_discovery import discover_requests, request_sites
16
18
  from .security_contexts import specialize_security_contexts
17
19
  from .block_locals import rename_block_locals
18
20
  from .support_checker import check_support as _support_diagnostics
@@ -52,7 +54,9 @@ def _generate(pine_source: str, check_support: bool, filename: str,
52
54
  the member of its name cannot hold gets a name of its own, and the
53
55
  pipeline runs again (``block_locals``).
54
56
 
55
- Returns ``(codegen, ctx, cpp, support_diagnostics)``."""
57
+ Returns ``(codegen, ctx, cpp, support_diagnostics, sites)``: ``sites``
58
+ are the requests of another symbol's data the support checker lowered
59
+ (``request_discovery.request_sites``)."""
56
60
  budget = None
57
61
  clones: frozenset[str] = frozenset()
58
62
  renamed: frozenset = frozenset()
@@ -67,6 +71,7 @@ def _generate(pine_source: str, check_support: bool, filename: str,
67
71
  if any(d.level == Level.ERROR for d in support_diagnostics):
68
72
  raise CompileError(support_diagnostics)
69
73
  budget.check(phase=Phase.ANALYZER)
74
+ sites = request_sites(ast)
70
75
  ast = lower_no_data_requests(ast)
71
76
  ast = specialize_security_contexts(ast, filename=filename)
72
77
  ast = bind_builtin_keywords(expand_finite_choice_extrema_lengths(ast))
@@ -86,18 +91,18 @@ def _generate(pine_source: str, check_support: bool, filename: str,
86
91
  apart = gen.block_locals_needing_names - renamed
87
92
  if apart:
88
93
  renamed |= apart
89
- del ast, ctx, gen, cpp
94
+ del ast, ctx, gen, cpp, sites
90
95
  continue
91
96
  needed = gen.session_functions_needing_clones
92
97
  if not needed:
93
- return gen, ctx, cpp, support_diagnostics
98
+ return gen, ctx, cpp, support_diagnostics, sites
94
99
  # Only uncloned functions ask, so the set grows each time and the
95
100
  # loop ends by the time every function that reads a flag at an
96
101
  # offset is cloned.
97
102
  if needed <= clones:
98
103
  raise AssertionError("a cloned function asked for session clones again")
99
104
  clones |= needed
100
- del ast, ctx, gen, cpp
105
+ del ast, ctx, gen, cpp, sites
101
106
 
102
107
 
103
108
  def transpile(pine_source: str, *, check_support: bool = True, filename: str = "<input>",
@@ -140,7 +145,8 @@ def transpile(pine_source: str, *, check_support: bool = True, filename: str = "
140
145
  Returns:
141
146
  Generated C++ source string.
142
147
  """
143
- _gen, _ctx, cpp, _support = _generate(pine_source, check_support, filename, libraries)
148
+ _gen, _ctx, cpp, _support, _sites = _generate(pine_source, check_support, filename,
149
+ libraries)
144
150
  return cpp
145
151
 
146
152
 
@@ -161,26 +167,36 @@ def transpile_full(pine_source: str, *, check_support: bool = True,
161
167
  source order). Each has ``title`` / ``type`` /
162
168
  ``default`` and optionally ``min`` / ``max`` / ``step`` / ``options``
163
169
  (omitted when the corresponding signature argument is absent or
164
- references a non-const value). See
165
- :meth:`CodeGen.extract_input_manifest`.
170
+ references a non-const value); an ``input.symbol`` entry also has
171
+ ``kind: "symbol"``. See :meth:`CodeGen.extract_input_manifest`.
166
172
  - ``strategyParams``: the literal ``strategy(...)`` kwargs the analyzer
167
173
  surfaced (e.g. ``initial_capital``, ``pyramiding``).
168
174
  - ``diagnostics``: the warnings (:class:`~pineforge_codegen.errors.Diagnostic`,
169
175
  ``Level.WARNING``) the support checker and the analyzer raised for a
170
176
  script that transpiled -- e.g. an approximated ``ta.vwap`` anchor. An
171
177
  error still raises ``CompileError``, which carries the warnings too.
178
+ - ``requests``: every request site that reads another symbol's feed,
179
+ by line: its symbol and timeframe as registration computes them
180
+ before the first bar (``literal`` / ``input`` / ``computed`` /
181
+ ``unresolvable``; ``literal`` / ``chart`` / ``input`` / ``computed``),
182
+ ``lookahead``, ``gaps`` and ``ignore_invalid_symbol``. A site lowered
183
+ to ``na`` (its value reaches display sinks only) or in a helper
184
+ nothing reaches is not listed. See
185
+ :mod:`pineforge_codegen.request_discovery`.
172
186
 
173
187
  Args mirror :func:`transpile`.
174
188
 
175
189
  Returns:
176
190
  ``{"cpp": str, "inputs": list[dict], "strategyParams": dict,
177
- "diagnostics": list[Diagnostic]}``.
191
+ "diagnostics": list[Diagnostic], "requests": list[dict]}``.
178
192
  """
179
- gen, ctx, cpp, support_diagnostics = _generate(pine_source, check_support, filename, libraries)
193
+ gen, ctx, cpp, support_diagnostics, sites = _generate(
194
+ pine_source, check_support, filename, libraries)
180
195
  return {
181
196
  "cpp": cpp,
182
197
  "inputs": gen.extract_input_manifest(),
183
198
  "strategyParams": dict(ctx.strategy_params),
184
199
  "diagnostics": [d for d in (*support_diagnostics, *ctx.diagnostics)
185
200
  if d.level == Level.WARNING],
201
+ "requests": discover_requests(gen, ctx, sites),
186
202
  }
@@ -3797,6 +3797,7 @@ class Analyzer(CallHandlers, DiagnosticsHelper, TypeHelper):
3797
3797
  symbol=sec.symbol,
3798
3798
  foreign=sec.foreign,
3799
3799
  ignore_invalid=sec.ignore_invalid,
3800
+ loc=sec.loc,
3800
3801
  )
3801
3802
  new_calls.append(clone)
3802
3803
  next_sec_id += 1
@@ -876,6 +876,7 @@ class CallHandlers:
876
876
  dead=bool(notes.get(DEAD_ANNOTATION)),
877
877
  foreign=notes.get(LOWERING_ANNOTATION) == FEED_LOWERING,
878
878
  ignore_invalid=node.kwargs.get("ignore_invalid_symbol"),
879
+ loc=node.loc,
879
880
  ))
880
881
 
881
882
  return PineType.STRING if string_result else PineType.FLOAT
@@ -998,6 +999,7 @@ class CallHandlers:
998
999
  is_lower_tf_array=True,
999
1000
  context_resolved=context is not None,
1000
1001
  dead=bool(notes.get(DEAD_ANNOTATION)),
1002
+ loc=node.loc,
1001
1003
  ))
1002
1004
 
1003
1005
  # ``request.security_lower_tf`` returns an array; the value-level
@@ -196,6 +196,9 @@ class SecurityCallInfo:
196
196
  # ``symbol`` string and ``ignore_invalid_symbol`` (the node, or None).
197
197
  foreign: bool = False
198
198
  ignore_invalid: Any = None
199
+ # The request call's source location (``request_discovery`` names the
200
+ # site by its line).
201
+ loc: Any = None
199
202
 
200
203
 
201
204
  @dataclass
@@ -4414,7 +4414,44 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
4414
4414
  return None
4415
4415
 
4416
4416
  def generate(self) -> str:
4417
- """Generate C++ source from the AnalyzerContext."""
4417
+ """Generate C++ source from the AnalyzerContext.
4418
+
4419
+ An error the settings metadata finds in an input's arguments (emitted
4420
+ in the constructor, ahead of the body: ``checked_settings``) is held
4421
+ until the rest is generated; the error raised is the first in source
4422
+ order, as before that metadata visited the arguments."""
4423
+ self._deferred_settings_errors: list[CompileError] | None = []
4424
+ try:
4425
+ try:
4426
+ cpp = self._generate_cpp()
4427
+ except CompileError as error:
4428
+ first = self._first_settings_error_before(error)
4429
+ if first is None:
4430
+ raise
4431
+ raise first from None
4432
+ if self._deferred_settings_errors:
4433
+ raise self._deferred_settings_errors[0]
4434
+ return cpp
4435
+ finally:
4436
+ self._deferred_settings_errors = None
4437
+
4438
+ def _defer_settings_error(self, error: CompileError) -> None:
4439
+ held = getattr(self, "_deferred_settings_errors", None)
4440
+ if held is None: # outside generate(): nothing holds it
4441
+ raise error
4442
+ held.append(error)
4443
+
4444
+ def _first_settings_error_before(self, error: CompileError) -> CompileError | None:
4445
+ """A held settings error located before ``error``, else None."""
4446
+ def where(err: CompileError):
4447
+ loc = err.diagnostics[0].location if err.diagnostics else None
4448
+ return (loc.line, loc.col) if loc is not None else (float("inf"), 0)
4449
+ held = sorted(self._deferred_settings_errors, key=where)
4450
+ if held and where(held[0]) < where(error):
4451
+ return held[0]
4452
+ return None
4453
+
4454
+ def _generate_cpp(self) -> str:
4418
4455
  # Every input is keyed by its title: refuse a non-constant one first,
4419
4456
  # then flag inputs one override key would reach together.
4420
4457
  self._check_input_titles()
@@ -0,0 +1,186 @@
1
+ """Immutable execution metadata; it does not alter generated computation."""
2
+
3
+ import json
4
+ import math
5
+
6
+ from ..ast_nodes import BoolLiteral, FuncCall, Identifier, MemberAccess, NumberLiteral, StrategyDecl, StringLiteral, UnaryOp, VarDecl
7
+ from ..external_requests import CAPABILITY_UNPINNED_ANNOTATION
8
+ from ..limits import iter_ast_nodes
9
+
10
+
11
+ STRATEGY_PARAMETERS = (
12
+ "title", "shorttitle", "overlay", "format", "precision", "scale", "pyramiding",
13
+ "calc_on_order_fills", "calc_on_every_tick", "max_bars_back",
14
+ "backtest_fill_limits_assumption", "default_qty_type", "default_qty_value",
15
+ "initial_capital", "currency", "slippage", "commission_type", "commission_value",
16
+ "process_orders_on_close", "close_entries_rule", "margin_long", "margin_short",
17
+ "explicit_plot_zorder", "max_lines_count", "max_labels_count", "max_boxes_count",
18
+ "calc_bars_count", "risk_free_rate", "use_bar_magnifier", "fill_orders_on_standard_ohlc",
19
+ "max_polylines_count", "dynamic_requests", "behind_chart", "calc_on_every_history_tick",
20
+ )
21
+
22
+
23
+ DECLARATION_DEFAULTS = {
24
+ "calc_on_every_tick": False,
25
+ "calc_on_order_fills": False,
26
+ "process_orders_on_close": False,
27
+ "use_bar_magnifier": False,
28
+ "fill_orders_on_standard_ohlc": False,
29
+ "backtest_fill_limits_assumption": 0,
30
+ "currency": "currency.NONE",
31
+ "timeframe": "",
32
+ "timeframe_gaps": True,
33
+ "dynamic_requests": True,
34
+ "calc_on_every_history_tick": False,
35
+ }
36
+
37
+
38
+ def _expression(node):
39
+ if isinstance(node, (StringLiteral, NumberLiteral, BoolLiteral)):
40
+ return node.value
41
+ if isinstance(node, Identifier):
42
+ return node.name
43
+ if isinstance(node, MemberAccess):
44
+ return f"{_expression(node.object)}.{node.member}"
45
+ if isinstance(node, FuncCall):
46
+ return f"{_expression(node.callee)}({','.join(str(_expression(arg)) for arg in node.args)})"
47
+ return None
48
+
49
+
50
+ def _literal(node):
51
+ if isinstance(node, (StringLiteral, NumberLiteral, BoolLiteral)):
52
+ return node.value, True
53
+ if isinstance(node, UnaryOp) and node.op in ("+", "-"):
54
+ value, valid = _literal(node.operand)
55
+ if valid and type(value) in (int, float):
56
+ return value if node.op == "+" else -value, True
57
+ if isinstance(node, MemberAccess):
58
+ value = _expression(node)
59
+ if value and value.split(".")[0] in ("currency", "format", "scale", "strategy"):
60
+ return value, True
61
+ return _expression(node), False
62
+
63
+
64
+ def capabilities_document(emitter) -> str:
65
+ declarations = dict(DECLARATION_DEFAULTS)
66
+ unresolved = []
67
+ requests = []
68
+ lower_symbols = {}
69
+ intrabar = False
70
+ recorded = False
71
+ for node, _depth in iter_ast_nodes(emitter.ctx.ast):
72
+ if isinstance(node, StrategyDecl):
73
+ arguments = {STRATEGY_PARAMETERS[index] if index < len(STRATEGY_PARAMETERS)
74
+ else f"strategy() positional argument {index + 1}": argument
75
+ for index, argument in enumerate(node.args)}
76
+ unresolved.extend(arguments.keys() & node.kwargs.keys())
77
+ arguments.update(node.kwargs)
78
+ for name, argument in arguments.items():
79
+ value, valid = _literal(argument)
80
+ default = DECLARATION_DEFAULTS.get(name)
81
+ if name in DECLARATION_DEFAULTS:
82
+ valid &= type(value) is type(default)
83
+ if name == "backtest_fill_limits_assumption":
84
+ valid = (type(value) is int and value >= 0) or (
85
+ type(value) is float and math.isfinite(value) and value >= 0 and value == int(value))
86
+ if valid:
87
+ value = int(value)
88
+ declarations[name] = value
89
+ if not valid or name not in STRATEGY_PARAMETERS and name not in DECLARATION_DEFAULTS:
90
+ unresolved.append(name)
91
+ elif isinstance(node, VarDecl):
92
+ intrabar |= node.is_varip
93
+ elif isinstance(node, FuncCall):
94
+ name, namespace = emitter._resolve_callee(node.callee)
95
+ if namespace == "request" and name == "security_lower_tf":
96
+ expression = node.args[2] if len(node.args) > 2 else node.kwargs.get("expression")
97
+ lower_symbols[id(expression)] = node.args[0] if node.args else node.kwargs.get("symbol")
98
+ parts = (node.annotations or {}).get("pf_recorded_key")
99
+ if parts is not None:
100
+ recorded = True
101
+ requests.append({
102
+ "function": f"{namespace}.{name}",
103
+ "symbol": _expression(node.args[0]) if node.args else None,
104
+ "timeframe": "",
105
+ "lookahead": f"barmerge.lookahead_{parts.get('lookahead', 'off')}",
106
+ "gaps": f"barmerge.gaps_{parts.get('gaps', 'off')}",
107
+ "heikinashi": False,
108
+ "feed": "recorded",
109
+ })
110
+ if isinstance(node, Identifier) and node.name == "timenow":
111
+ unresolved.append("timenow (bar timestamp approximation)")
112
+ if isinstance(node, Identifier) and node.name in ("last_bar_index", "last_bar_time"):
113
+ unresolved.append(f"{node.name} (stream endpoint is not batch-equivalent)")
114
+ if isinstance(node, MemberAccess) and _expression(node) == "barstate.isrealtime":
115
+ unresolved.append("barstate.isrealtime (compiled historical-only approximation)")
116
+ if isinstance(node, MemberAccess) and _expression(node) in (
117
+ "barstate.islast", "barstate.islastconfirmedhistory"):
118
+ unresolved.append(f"{_expression(node)} (stream endpoint is not batch-equivalent)")
119
+
120
+ auxiliary = False
121
+ for site in emitter._security_calls:
122
+ symbol_node = site.get("symbol_node")
123
+ if site.get("is_lower_tf_array"):
124
+ symbol_node = lower_symbols.get(id(site.get("expr_node")), symbol_node)
125
+ if isinstance(symbol_node, FuncCall) and site.get("heikinashi") and symbol_node.args:
126
+ symbol_node = symbol_node.args[0]
127
+ symbol = _expression(symbol_node) if symbol_node is not None else "syminfo.tickerid"
128
+ timeframe = _expression(site.get("tf_node"))
129
+ lookahead = _expression(site.get("lookahead_node")) or "barmerge.lookahead_off"
130
+ gaps = _expression(site.get("gaps_node")) or "barmerge.gaps_off"
131
+ if isinstance(symbol_node, Identifier) and symbol_node.name not in emitter._global_mutable_infos:
132
+ symbol = _expression((getattr(emitter.ctx, "global_expr_map", {}) or {}).get(
133
+ symbol_node.name, symbol_node))
134
+ feed = "chart" if symbol in ("syminfo.tickerid", "syminfo.ticker", "") else "auxiliary"
135
+ auxiliary |= feed == "auxiliary"
136
+ if not isinstance(site.get("tf_node"), StringLiteral) and timeframe != "timeframe.period":
137
+ unresolved.append(f"request.security[{site['sec_id']}].timeframe")
138
+ if lookahead not in ("barmerge.lookahead_off", "barmerge.lookahead_on"):
139
+ unresolved.append(f"request.security[{site['sec_id']}].lookahead")
140
+ requests.append({
141
+ "function": "request.security_lower_tf" if site.get("is_lower_tf_array") else "request.security",
142
+ "symbol": symbol,
143
+ "timeframe": timeframe,
144
+ "lookahead": lookahead,
145
+ "gaps": gaps,
146
+ "heikinashi": bool(site.get("heikinashi")),
147
+ "feed": feed,
148
+ })
149
+ for index, site in enumerate((emitter.ctx.ast.annotations or {}).get(CAPABILITY_UNPINNED_ANNOTATION, ())):
150
+ requests.append({
151
+ "function": site["function"],
152
+ "symbol": _expression(site["symbol_node"]),
153
+ "timeframe": _expression(site["tf_node"]) if site["function"] in (
154
+ "request.security", "request.security_lower_tf") else "",
155
+ "lookahead": _expression(site["lookahead_node"]) or "barmerge.lookahead_off",
156
+ "gaps": _expression(site["gaps_node"]) or "barmerge.gaps_off",
157
+ "heikinashi": False,
158
+ "feed": "unpinned",
159
+ })
160
+ unresolved.append(f"{site['function']}[unpinned:{index}]")
161
+ requirements = {
162
+ "auxiliary_security_feeds": auxiliary,
163
+ "native_security_feeds": False,
164
+ "fx_curve": declarations["currency"] != "currency.NONE",
165
+ "recorded_series": recorded,
166
+ "historical_probe_overrides": False,
167
+ "intrabar_persistence": intrabar,
168
+ }
169
+ return json.dumps({"version": 1, "declarations": declarations, "requests": requests,
170
+ "requirements": requirements, "unresolved": sorted(set(unresolved))},
171
+ sort_keys=True, separators=(",", ":"), allow_nan=False)
172
+
173
+
174
+ def emit_capabilities_exports(emitter, lines: list[str]) -> None:
175
+ literal = json.dumps(capabilities_document(emitter))
176
+ lines.extend([
177
+ "#ifdef PF_SETTINGS_API_VERSION",
178
+ "#ifdef PF_CAPABILITIES_API_VERSION",
179
+ " uint32_t strategy_capabilities_api_version(void) { return PF_CAPABILITIES_API_VERSION; }",
180
+ " int strategy_capabilities_receipt(void* s, char* json, size_t capacity, size_t* required, char* error, size_t error_capacity) {",
181
+ " if (required) *required = 0;",
182
+ " return ::pineforge::checked_settings::boundary(error, error_capacity, [&] {",
183
+ ' ::pineforge::checked_settings::require(s != nullptr, "null strategy");',
184
+ f" ::pineforge::checked_settings::receipt({literal}, json, capacity, required);",
185
+ " });", " }", "#endif", "#endif",
186
+ ])