diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c11b388..c2c8bf90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,7 +50,19 @@ supported as exact pairs; on the 0.x line they are independent. See the that overlap collection-history or requested-context storage. Persistent declarations in sibling blocks also retain distinct storage when a script uses their generated spelling. Declarations, calls and history storage use - the same allocated identities. Regenerate C++ and relink affected strategies. + the same allocated identities, including when selecting reachable variants + for emission. Regenerate C++ and relink affected strategies. +- Persistent declarations accept a type qualifier after `var` or `varip`. + Lower-timeframe requests return independently typed arrays for inline and + helper-returned tuple payloads. +- Complete nested indentation levels can combine tabs and spaces. Requested + `na`/`nz` helper parameters retain their bindings beside time operations; + global tuple declarations and their member histories share one requested + sample. Uncalled forwarding helpers no longer allocate unresolved TA buffers + for their unreachable call variants (#372). Bare tuple-element history + payloads retain their located refusal. Tuple histories rendered through the + expression visitor, including reads through globals, retain their chart + series and warning. - A zone name the engine rejects now makes calendar-form `timestamp()` stop the run at its first evaluation, instead of silently calculating a UTC value. diff --git a/docs/reference/support-contracts.md b/docs/reference/support-contracts.md index cf55d2f5..c1278091 100644 --- a/docs/reference/support-contracts.md +++ b/docs/reference/support-contracts.md @@ -1,5 +1,29 @@ ## Support contracts +Persistent typed declarations parse their storage prefix separately from their +qualifier: `var const float`, `varip const int`, and the corresponding `simple` +and `series` forms retain both annotations and their existing initialization. + +`request.security_lower_tf` tuple payloads, written inline or returned by a +helper, produce one array per element. Each intrabar evaluates the tuple once +before distributing values to the independently typed arrays; clearing the +request clears every array together. Tuple-bound arrays retain collection +metadata in global and callable scopes. + +A global tuple declaration read by `request.security` is sampled once in the +requested evaluator, in declaration order. Scalar and history reads share that +sample, including stateful helper results. Member history commits on completed +requested bars even when a lazy payload does not read the member on that bar. +Helper parameter bindings remain active through `na`/`nz` builtin emission in +both evaluator modes. These rules supersede the older tuple refusal limitations +described in the historical helper row below. + +Complete mixed tab/space nested block prefixes use four columns per tab plus +the spaces. Previously accepted incomplete mixed prefixes, skipped first-body +widths and pending logical-line continuations retain their earlier handling +pending additional TradingView observations; they are not newly refused as part +of the nested-block correction. + Builtin `array.slice` on a resolved typed array in the script, a function or method parameter, or a callable-local array opens the view gate and lowers the script's array storage to typed handles: diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 5f99998b..d30159be 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -5627,6 +5627,9 @@ def _selection_tuple_shape( def _visit_TupleAssign(self, node: TupleAssign) -> PineType: val_type = self._visit(node.value) + lower_types = (node.value.annotations or {}).get( + "lower_tf_tuple_element_types", (), + ) loc = node.loc or SourceLocation(file=self._filename, line=1, col=1, end_col=1) is_program_global = ( self._global_scope and self._symbols.current_scope.name == "global" @@ -5741,7 +5744,14 @@ def _visit_TupleAssign(self, node: TupleAssign) -> PineType: self._record_script_block_binding(sym, outer_symbol) self._note_member_writer(sym, name, node) - if (self._collection_scope_stack + if idx < len(lower_types): + inner = lower_types[idx] + spec = TypeSpec.array( + TypeSpec.primitive("int64") if inner == PineType.INT + else self._pine_type_to_spec(inner) + ) + self._record_collection_type(name, spec) + elif (self._collection_scope_stack and self._block_node_stack and self._symbols.current_scope.name != "global"): owner_id = id(self._block_node_stack[-1]) diff --git a/pineforge_codegen/analyzer/call_handlers.py b/pineforge_codegen/analyzer/call_handlers.py index 7f25b6b2..fb44adac 100644 --- a/pineforge_codegen/analyzer/call_handlers.py +++ b/pineforge_codegen/analyzer/call_handlers.py @@ -1001,9 +1001,8 @@ def _handle_request_security_lower_tf(self, node: FuncCall) -> PineType: the codegen knows to emit a ``std::vector`` accumulator that gets cleared on sub-bar 0 and pushed-to per sub-bar. - UDT / color / string element types are deliberately rejected here - with a precise diagnostic; the runtime path only knows how to - accumulate ``double`` / ``int`` / ``bool``.""" + Scalar calls retain their existing numeric/bool arrays. Tuple calls + carry each primitive element's type to its own array accumulator.""" param_names = ["symbol", "timeframe", "expression", "ignore_invalid_symbol", "currency", "ignore_invalid_timeframe", "calc_bars_count"] @@ -1033,13 +1032,6 @@ def _handle_request_security_lower_tf(self, node: FuncCall) -> PineType: node.loc, ) - if isinstance(expr_node, TupleLiteral): - self._error( - "request.security_lower_tf does not support tuple expressions yet. " - "Issue separate request.security_lower_tf calls for each series.", - node.loc, - ) - for arg in node.args: if arg is not None and arg is not expr_node: self._visit(arg) @@ -1054,20 +1046,45 @@ def _handle_request_security_lower_tf(self, node: FuncCall) -> PineType: ta_end = len(self._ta_call_sites) security_ta_range = (ta_start, ta_end) if ta_end > ta_start else None + tuple_types = self._tuple_element_types_by_node.get(id(expr_node), ()) + if (isinstance(expr_node, FuncCall) + and isinstance(expr_node.callee, Identifier) + and self._func_returns_tuple.get(expr_node.callee.name, False)): + tuple_types = self._func_tuple_element_types.get(expr_node.callee.name, ()) + if PineType.UNKNOWN in tuple_types: + tuple_types = self._security_callsite_tuple_types( + expr_node.callee.name, expr_node, tuple_types, + ) + elif (isinstance(expr_node, FuncCall) + and isinstance(expr_node.callee, MemberAccess) + and isinstance(expr_node.callee.object, Identifier) + and expr_node.callee.object.name == "ta"): + count = TA_TUPLE_ELEMENT_COUNTS.get(expr_node.callee.member, 0) + if count: + tuple_types = tuple_types or (PineType.FLOAT,) * count + # Cache the resolved element type on the call node so the # ``_type_spec_from_expr`` pass can map ``request.security_lower_tf`` # to ``array`` without re-visiting the expression (which would # double-allocate TA call sites for expressions like ``ta.ema``). cached_anns = getattr(node, "annotations", None) or {} cached_anns["lower_tf_element_pine_type"] = expr_pine_type + if tuple_types: + cached_anns["lower_tf_tuple_element_types"] = tuple_types + self._security_tuple_shapes[id(node)] = len(tuple_types) node.annotations = cached_anns - if expr_pine_type not in (PineType.FLOAT, PineType.INT, PineType.BOOL, - PineType.NA, PineType.UNKNOWN): + element_types = tuple_types or (expr_pine_type,) + supported = {PineType.FLOAT, PineType.INT, PineType.BOOL, + PineType.NA, PineType.UNKNOWN} + if tuple_types: + supported |= {PineType.STRING, PineType.COLOR} + unsupported = next((item for item in element_types if item not in supported), None) + if unsupported is not None: element_label = { PineType.STRING: "string", PineType.COLOR: "color", - }.get(expr_pine_type, str(expr_pine_type)) + }.get(unsupported, str(unsupported)) self._error( "request.security_lower_tf element type '" + element_label + "' is not yet supported. Supported element types: float, int, bool. " @@ -1085,8 +1102,9 @@ def _handle_request_security_lower_tf(self, node: FuncCall) -> PineType: sec_id=sec_id, timeframe=context["timeframe"] if context is not None else tf_node, expression=expr_node, - returns_tuple=False, - tuple_size=0, + returns_tuple=bool(tuple_types), + tuple_size=len(tuple_types), + tuple_element_types=tuple(tuple_types), gaps=None, lookahead=None, ta_range=security_ta_range, diff --git a/pineforge_codegen/codegen/base.py b/pineforge_codegen/codegen/base.py index 25480bfb..de2b5519 100644 --- a/pineforge_codegen/codegen/base.py +++ b/pineforge_codegen/codegen/base.py @@ -806,6 +806,7 @@ def func_var_storage(owner: str, raw_name: str) -> str: # authority for nested stateful-helper dispatch (see visit_call). self._current_instance_name: str | None = None self._instance_dispatch: dict[tuple[str | None, int], str] = {} + self._live_func_instances: set[str] | None = None self._fresh_instances: list[dict] = [] self._fresh_var_members: list[tuple[str, str, str]] = [] # Fresh fixnan members for context-sensitive helper instances (nested @@ -2075,6 +2076,7 @@ def natural_name(fname: str, cs_idx: int) -> str: interned: dict[tuple, dict] = {} worklist: list[dict] = [] seen_walk: set[str] = set() + instances: dict[str, dict] = {} fresh_counter = 0 # Seed with the natural clones the flat emission loop produces. @@ -2107,6 +2109,7 @@ def natural_name(fname: str, cs_idx: int) -> str: if inst["name"] in seen_walk: continue seen_walk.add(inst["name"]) + instances[inst["name"]] = inst body = func_bodies.get(inst["fname"]) if not body: continue @@ -2267,6 +2270,84 @@ def natural_name(fname: str, cs_idx: int) -> str: worklist.append(ginst) self._instance_dispatch[(inst["name"], id(callnode))] = ginst["name"] + # Definition-time calls reserve variants too. Only paths rooted in + # executable statements instantiate their chart-context TA buffers. + # Preserve all semantic analysis; select emission after composing the + # same natural/fresh dispatch graph that the call visitor uses. + for fi in ctx.func_infos: + instances.setdefault(self._func_cpp_base_name(fi.name), { + "fname": fi.name, "name": self._func_cpp_base_name(fi.name), + "call_site_idx": None, "ta_remap": {}, + }) + methods: dict[int, set[str]] = {} + for call_id, key in ctx.method_call_bindings: + methods.setdefault(call_id, set()).add(key) + + def targets(call, parent): + dispatch = self._instance_dispatch.get( + (parent["name"] if parent is not None else None, id(call)), + ) + if dispatch is not None: + return [dispatch] + bound = ctx.func_call_cs_map.get(id(call)) + if bound is not None: + names = [bound[0]] + elif id(call) in inherited_names: + names = [inherited_names[id(call)]] + elif isinstance(call.callee, Identifier) and call.callee.name in func_bodies: + names = [call.callee.name] + else: + names = sorted(methods.get(id(call), ())) + result = [] + for name in names: + if name in stateful and ctx.func_call_site_counts.get(name, 0) > 0: + index = ( + bound[1] if bound is not None + else (parent.get("call_site_idx") or 0) if parent is not None + else 0 + ) + result.append(natural_name(name, index)) + else: + result.append(self._func_cpp_base_name(name)) + return result + + roots = [ + stmt for stmt in ctx.ast.body + if not isinstance(stmt, (FuncDef, MethodDef)) + ] + pending = [ + target + for call in self._iter_func_calls(roots) + for target in targets(call, None) + ] + live: set[str] = set() + live_owners: set[str] = set() + used_ta: set[str] = set() + while pending: + if self._budget is not None: + self._budget.check(phase=Phase.CODEGEN) + name = pending.pop() + if name in live: + continue + live.add(name) + instance = instances.get(name) + if instance is None: + continue + owner = instance["fname"] + live_owners.add(owner) + used_ta.update(instance.get("ta_remap", {}).values()) + for call in self._iter_func_calls(func_bodies.get(owner, ())): + pending.extend(targets(call, instance)) + self._live_func_instances = live + self._dead_func_names.update(stateful - live_owners) + self._dead_ta_indices = { + index for index, site in enumerate(ctx.ta_call_sites) + if site.owner_func in stateful and site.member_name not in used_ta + } + self._fresh_instances[:] = [ + instance for instance in self._fresh_instances if instance["name"] in live + ] + def _callable_var_collection_spec( self, name: str, owner_func: str | None = None) -> TypeSpec | None: """Exact TypeSpec for a function-scoped persistent collection member. @@ -5657,17 +5738,27 @@ def _generate_cpp(self) -> str: # emits ``std::vector`` and the eval method pushes the # per-sub-bar value. Element type is inferred from the # expression — analyzer constrained it to int / float / bool. + tuple_types = item.get("tuple_element_types", ()) ctype = self._infer_cpp_type_for_security_elem(expr_node) - if ctype not in ("double", "int", "bool"): + if tuple_types: + accumulators = ", ".join( + f"std::vector<{part}>" + for part in self._lower_tf_tuple_cpp_types(tuple_types) + ) + result_type = f"std::tuple<{accumulators}>" + elif ctype not in ("double", "int", "bool"): # Defensive fallback — analyzer should already have # rejected unsupported types, but keep the codegen # well-defined if a future path slips through. ctype = "double" + result_type = f"std::vector<{ctype}>" + else: + result_type = f"std::vector<{ctype}>" self._security_ohlc_hist_fields_by_sec[sec_id] = ( self._collect_security_ohlc_hist_fields(state_expr) ) lines.append( - f" std::vector<{ctype}> _req_sec_lower_tf_{sec_id}{{}};" + f" {result_type} _req_sec_lower_tf_{sec_id}{{}};" ) for field in sorted( self._security_ohlc_hist_fields_by_sec.get(sec_id, ()) @@ -6344,6 +6435,10 @@ def allocate_legacy_flag(base: str) -> str: if (has_ta or has_series or needs_security_clone) and total_cs > 0: # Emit one variant per call site for cs_idx in range(total_cs): + if (self._live_func_instances is not None + and self._func_variant_name(fi.name, cs_idx) + not in self._live_func_instances): + continue self._emit_func_def(fi, lines, call_site_idx=cs_idx) lines.append("") else: diff --git a/pineforge_codegen/codegen/security.py b/pineforge_codegen/codegen/security.py index 056756ab..63f8ef16 100644 --- a/pineforge_codegen/codegen/security.py +++ b/pineforge_codegen/codegen/security.py @@ -786,6 +786,24 @@ def _emit_runtime_security_templates(self, lines: list[str]) -> None: " };", )) + @staticmethod + def _lower_tf_tuple_cpp_types(element_types) -> list[str]: + return [ + "int64_t" if item == PineType.INT + else PINE_TYPE_TO_CPP.get(item, "double") + for item in element_types + ] + + @staticmethod + def _lower_tf_clear_cpp(item: dict) -> str: + name = f"_req_sec_lower_tf_{item['sec_id']}" + if item.get("returns_tuple"): + return ( + "std::apply([](auto&... _pf_arrays) { " + f"(_pf_arrays.clear(), ...); }}, {name});" + ) + return f"{name}.clear();" + def _runtime_security_result(self, item: dict, prefix: str) -> str: sec_id = item["sec_id"] if item.get("is_lower_tf_array"): @@ -4554,6 +4572,14 @@ def _collect_security_expr_hist_subscripts( helpers: list[str] = [] def add(n: Subscript) -> None: + if (n is node + and self._security_global_history_value(n) is n.object + and self._resolve_security_index_literal(n.index) != 0): + # A bare tuple-element history payload keeps its located + # refusal; offset zero reads the current element. Tuple slots + # also serve reads inside builder expressions, without + # widening the bare-history payload boundary. + return key = id(n) if helpers: in_helper.add(key) @@ -4604,7 +4630,11 @@ def walk(n) -> None: walk(x) walk(node) - return [n for n in out if id(n) not in in_helper or reached[id(n)] == 1] + return [ + n for n in out + if id(n) not in in_helper or reached[id(n)] == 1 + or self._security_global_history_value(n) is n.object + ] def _security_global_history_value(self, node) -> ASTNode | None: """The value of ``g`` in a payload's ``g[k]`` when ``g`` is a global @@ -4623,6 +4653,10 @@ def _security_global_history_value(self, node) -> ASTNode | None: return None name = node.object.name global_expr_map = getattr(self.ctx, "global_expr_map", {}) or {} + if (self._security_identifier_is_global_binding(node.object) + and name in self._direct_program_tuple_bindings + and name not in self._global_mutable_infos): + return node.object if ( not self._security_identifier_is_global_binding(node.object) or name not in global_expr_map @@ -4634,6 +4668,10 @@ def _security_global_history_value(self, node) -> ASTNode | None: seen = {name} while isinstance(value, Identifier): name = value.name + if (self._security_identifier_is_global_binding(value) + and name in self._direct_program_tuple_bindings + and name not in self._global_mutable_infos): + return node.object if (not self._security_identifier_is_global_binding(value) or name not in global_expr_map or name in seen or name in self._direct_program_tuple_binding_names @@ -4803,6 +4841,8 @@ def _emit_security_expr_hist_members( value = self._security_global_history_value(node) if self._is_session_flag(node.object): cpp_t = "bool" + elif value is node.object: + cpp_t = self._infer_type(value) elif value is not None: # A global's history is a double (a bool's a bool): its na # reads na, where typed from the global's ``int`` the first @@ -4812,12 +4852,14 @@ def _emit_security_expr_hist_members( cpp_t = "bool" if self._infer_type(value) == "bool" else "double" else: cpp_t = self._infer_type(node.object) - if cpp_t not in ("double", "int", "bool"): + if cpp_t not in ("double", "int", "int64_t", "bool", "std::string"): cpp_t = "double" name = f"_sec{sec_id}_expr_hist_{idx}" self._security_expr_hist_by_node[(sec_id, id(node))] = { "name": name, "type": cpp_t, + "object": node.object, + "tuple_global": value is node.object, } lines.append(f" Series<{cpp_t}> {name}{mbb_suffix};") @@ -6103,6 +6145,10 @@ def _security_global_value_inlinable(self, name: str) -> bool: requested bar at each read: its value, through the globals it reads, calls no multi-statement user function, whose statements -- and TA state -- the evaluator would emit again at every read.""" + if name in self._direct_program_tuple_bindings: + # The declaration is evaluated once in the evaluator prologue, + # including a multi-statement tuple helper. + return True cache = getattr(self, "_security_global_value_cache", None) if cache is None: cache = self._security_global_value_cache = {} @@ -6187,6 +6233,79 @@ def _security_pure_call(self, node: FuncCall) -> bool: and len(node.args) == len(info.node.params) and self._security_pure_body(info.node)) + def _security_needed_global_tuples(self, root) -> set[int]: + """Tuple declarations reached through the payload's resolved bindings.""" + needed: set[int] = set() + seen: set[int] = set() + pending = [root] + globals_ = getattr(self.ctx, "global_expr_map", {}) or {} + while pending: + if getattr(self, "_budget", None) is not None: + self._budget.check(phase=Phase.CODEGEN) + node = pending.pop() + if isinstance(node, (list, tuple)): + pending.extend(node) + continue + if not isinstance(node, ASTNode) or id(node) in seen: + continue + seen.add(id(node)) + if isinstance(node, Identifier) and self._security_identifier_is_global_binding(node): + binding = self._direct_program_tuple_bindings.get(node.name) + if binding is not None: + needed.add(id(binding[0])) + value = globals_.get(node.name) + if value is not None: + pending.append(value) + if isinstance(node, FuncCall): + key = self._security_user_call_key(node) + info = self._func_info_map.get(key) + if info is not None and info.node is not None: + pending.extend(info.node.body) + pending.extend(syntax_children(node)) + return needed + + def _security_global_tuple_element( + self, sec_id, name, ta_results, resolving, mutable_names, lines, + ) -> str: + """Sample a declaration once; all member reads share its tuple value.""" + cached = self._security_arg_global_values + if name in cached: + return cached[name] + declaration, _index = self._direct_program_tuple_bindings[name] + key = f"tuple:{id(declaration)}" + if key in resolving: + raise AssertionError("cyclic resolved tuple declaration") + if lines is None: + element = self._security_pure_user_tuple_element(name, declaration.value) + if element is not None: + _info, stack = self._security_bind_helper_args(declaration.value, ()) + return self._build_security_expr( + sec_id, element, None, ta_results, resolving, + mutable_names, stack, lines, + ) + raise AssertionError("tuple declaration requires a statement context") + resolving.add(key) + try: + value = self._build_security_expr( + sec_id, declaration.value, None, ta_results, resolving, + mutable_names, (), lines, + ) + finally: + resolving.remove(key) + temp = "_pf" + self._security_next_inline_name(sec_id, "tuple", "value") + lines.append(f" const auto {temp} = {value};") + site = self._get_ta_site(declaration.value) + fields = TA_TUPLE_FIELDS.get( + self._ta_name_from_site(site) if site is not None else "", (), + ) + for slot, target in enumerate(declaration.names): + if target != "_": + cached[target] = ( + f"{temp}.{fields[slot]}" if slot < len(fields) + else f"std::get<{slot}>({temp})" + ) + return cached[name] + def _security_pure_user_tuple_element(self, name: str, call): """The element expression a payload reads when it reads ``name``, a global of a tuple declaration ``[.., name, ..] = f(args)`` whose call @@ -8712,6 +8831,40 @@ def emit_security_ta(indices: list[int]) -> None: self._emit_security_rebinds(sec_id, info, lines, ta_results, indent=2, emitted_lines=lines) emit_security_ta(post_rebind_ta_indices) + needed_tuples = self._security_needed_global_tuples(expr_node) + for declaration in self.ctx.ast.body: + if isinstance(declaration, TupleAssign) and id(declaration) in needed_tuples: + target = next((name for name in declaration.names if name != "_"), None) + if target is not None: + self._security_global_tuple_element( + sec_id, target, ta_results, set(), security_mutable_names, lines, + ) + # Global tuple declarations run on every requested bar, even when + # a lazy payload does not read their history on that bar. + for (sid, _node_id), meta in self._security_expr_hist_by_node.items(): + obj = meta.get("object") + if (sid != sec_id or not isinstance(obj, Identifier) + or not meta.get("tuple_global")): + continue + value = self._security_arg_global_values.get(obj.name) + if value is None: + value = self._build_security_expr( + sec_id, obj, None, ta_results, set(), + security_mutable_names, (), lines, + ) + hist, cpp_t = meta["name"], meta["type"] + if cpp_t in ("int", "int64_t"): + value = na_preserving_int_cast(value, cpp_t) + elif cpp_t == "double": + value = ( + "[](auto _pf_v) { return is_na(_pf_v) ? na() " + f": static_cast(_pf_v); }}({value})" + ) + if (hist, cpp_t) not in self._security_global_hist_pushes: + self._security_global_hist_pushes.append((hist, cpp_t)) + lines.append( + f" if (is_complete) {{ {hist}_next = {value}; {hist}_set = true; }}" + ) # A global is an unconditional statement of the requested context, # even when the payload reads it only in an untaken selection arm. # Its sampled dependencies keep declaration order and one value. @@ -8727,6 +8880,7 @@ def emit_security_ta(indices: list[int]) -> None: and tuple_size and tuple_size > 0 and isinstance(expr_node, TupleLiteral) + and not item.get("is_lower_tf_array") ): # A tuple body destructures into per-element scalar members # ``_req_sec_{sec_id}_{i}`` (declared in ``base.py`` and reset in @@ -8773,6 +8927,41 @@ def emit_security_ta(indices: list[int]) -> None: emitted_lines=lines, ) if item.get("is_lower_tf_array"): + if returns_tuple: + # Evaluate the tuple once per intrabar, then distribute its + # elements to arrays of their own types. A stateful helper + # must not run once for every destructured element. + temp = f"_pf_lower_tuple_{sec_id}" + lines.append(f" const auto {temp} = {expr_cpp};") + lines.append( + f" if (security_lower_tf_sub_bar_index({sec_id}) == 0) " + f"{{ {self._lower_tf_clear_cpp(item)} }}" + ) + site = self._get_ta_site(expr_node) + fields = TA_TUPLE_FIELDS.get( + self._ta_name_from_site(site) if site is not None else "", (), + ) + for index, ctype in enumerate(self._lower_tf_tuple_cpp_types( + item["tuple_element_types"])): + value = ( + f"{temp}.{fields[index]}" if index < len(fields) + else f"std::get<{index}>({temp})" + ) + if ctype in ("int", "int64_t"): + value = na_preserving_int_cast(value, ctype) + elif ctype == "double": + value = ( + "[](auto _pf_v) { return is_na(_pf_v) ? na() " + f": static_cast(_pf_v); }}({value})" + ) + lines.append( + f" std::get<{index}>(_req_sec_lower_tf_{sec_id})" + f".push_back({value});" + ) + self._emit_security_ohlc_hist_pushes(sec_id, lines) + self._emit_security_ta_hist_pushes(sec_id, info, ta_results, lines) + lines.extend((" }", "")) + return # Match the accumulator's element type before adapting its value. # An integer sentinel must not pass through a floating NaN. ctype = self._infer_cpp_type_for_security_elem(expr_node) @@ -8906,7 +9095,7 @@ def _emit_security_evaluators(self, lines: list[str]) -> None: # gaps mode flushes between completions). Clearing the # vector is the right fallback. lines.append(f" case {sec_id}:") - lines.append(f" _req_sec_lower_tf_{sec_id}.clear();") + lines.append(f" {self._lower_tf_clear_cpp(item)}") for field in sorted(self._security_ohlc_hist_fields_by_sec.get(sec_id, ())): lines.append( f" {self._security_ohlc_hist_series_cpp(sec_id, field)}.clear();" @@ -9357,19 +9546,11 @@ def _build_security_expr( if expr_node.name in self._security_arg_global_values: return self._security_arg_global_values[expr_node.name] value = global_expr_map[expr_node.name] - if (expr_node.name in self._direct_program_tuple_binding_names - and self._security_ta_tuple_element_field(expr_node.name) is None): - # An element of a tuple declaration whose call is a pure - # user function: only that element is lowered, on the - # requested bar, with the call's arguments bound to the - # function's parameters. - element = self._security_pure_user_tuple_element( - expr_node.name, value) - if element is not None: - _info, bound_stack = self._security_bind_helper_args(value, ()) - return self._build_security_expr( - sec_id, element, ta_range, ta_results, resolving, - security_mutable_names, bound_stack, emitted_lines) + if expr_node.name in self._direct_program_tuple_bindings: + return self._security_global_tuple_element( + sec_id, expr_node.name, ta_results, resolving, + security_mutable_names, emitted_lines, + ) # A global bound to a user call is one value per requested # bar, however many reads the payload makes (``g - g[1]``): # every read inlined the call again, which advanced its TA @@ -9679,6 +9860,7 @@ def _build_security_expr( self._security_identifier_is_global_binding(expr_node.object) and expr_node.object.name in self._direct_program_tuple_binding_names and expr_node.object.name not in self._global_mutable_infos + and not self._security_reads_global_history(sec_id, expr_node) ): # Its value is the whole tuple: every earlier build # emitted the tuple's history, which did not compile. @@ -9752,7 +9934,8 @@ def _build_security_expr( # builtin keeps the chart's series and never gets here): # a double, which an integer slot of this evaluator # narrows na-preserving (``_security_emits_double``). - if self._infer_type(expr_node.object) == "std::string": + if (self._infer_type(expr_node.object) == "std::string" + and not meta.get("tuple_global")): # No string history on the requested clock; every # earlier build refused the read. self._codegen_error( @@ -10395,12 +10578,17 @@ def argument(value): emitted_lines, ) self._security_warn_chart_call(expr_node) - self._security_check_tuple_element_history(expr_node, sec_id, helper_binding_stack) + tuple_history = self._security_check_tuple_element_history( + expr_node, sec_id, helper_binding_stack, + ) chart: set[int] = set() if self._security_requested_calls: self._security_warn_global_history(expr_node, helper_binding_stack, sec_id) - chart_read = self._security_root_chart_read( - expr_node, helper_binding_stack, sec_id, security_mutable_names + chart_read = ( + f"the history of '{tuple_history.object.name}'" + if tuple_history is not None else self._security_root_chart_read( + expr_node, helper_binding_stack, sec_id, security_mutable_names, + ) ) if chart_read is not None: if self._security_requested_used: @@ -10425,7 +10613,7 @@ def argument(value): def _security_check_tuple_element_history( self, root, sec_id: int, helper_binding_stack=None - ) -> None: + ) -> Subscript | None: """History of a tuple declaration's element read under a node the expression visitor renders, directly or through a global's value, reads the chart's series: warned. Refused, as the builder refuses the @@ -10433,12 +10621,14 @@ def _security_check_tuple_element_history( tuple's element outside a user call's arguments, its site in the requested history and reached by no multi-statement helper, whose struct result the evaluator then pushed into a ``Series`` - (``_emit_security_ta_hist_pushes`` skips it).""" + (``_emit_security_ta_hist_pushes`` skips it). Return the first chart + history read so a global referring to it keeps the same fallback.""" global_expr_map = getattr(self.ctx, "global_expr_map", {}) or {} info = self._security_info_without_methods(self._security_eval_info[sec_id]) hist = self._security_ta_hist_idx_by_sec.get(sec_id, ()) helper_reached = {key[0] for key in info.get("inline_helper_ta_indices", [])} followed: set[tuple[str, bool]] = set() + chart_history = None stack = [(root, False, helper_binding_stack)] while stack: n, in_call, bindings = stack.pop() @@ -10463,6 +10653,8 @@ def _security_check_tuple_element_history( and self._security_identifier_is_global_binding(n.object) and n.object.name in self._direct_program_tuple_binding_names and n.object.name not in self._global_mutable_infos + and not (self._security_reads_global_history(sec_id, n) + and self._security_fallback_owns(n, bindings or (), sec_id)) and self._resolve_security_index_literal(n.index, bindings) != 0): name = n.object.name site = self._get_ta_site(global_expr_map.get(name)) @@ -10474,6 +10666,8 @@ def _security_check_tuple_element_history( f"request.security payload reads the history of " f"'{name}', an element of a tuple declaration", ) + if chart_history is None: + chart_history = n warned = getattr(self, "_security_warned_tuple_history", None) if warned is None: warned = self._security_warned_tuple_history = set() @@ -10491,6 +10685,7 @@ def _security_check_tuple_element_history( stack.extend( (v, in_call, bindings) for k, v in vars(n).items() if k != "annotations" ) + return chart_history def _security_note_chart_read(self, node, reason: str) -> None: """Record the first thing the evaluator being emitted reads on the @@ -11233,6 +11428,16 @@ def _security_fallback_owns( history (re-evaluating ``g`` on the requested bar would pair it with the chart's ``g[1]``), and ``timeframe.*`` (beside the chart's ``timeframe.in_seconds()``).""" + # A helper's parameter is bound in both evaluator modes. The legacy + # fallback may keep a surrounding builtin's rendering, but it cannot + # resolve that parameter in the chart function's lexical scope. + if (isinstance(node, Identifier) + and not self._security_identifier_is_global_binding(node)): + binding = self._security_lookup_helper_binding_context( + node.name, helper_binding_stack, + ) + if binding is not None and self._security_bound_is_scalar(*binding): + return True selection = getattr(self, "_security_selection_context", None) if selection is not None: if id(node) in selection["reads"]: @@ -11262,6 +11467,13 @@ def _security_fallback_owns( and node.name in self._input_backed_vars) or self._security_source_input_call(node) is not None ) + # Cached current values belong to requested evaluation. A chart + # fallback still needs its global Series as a history receiver, even + # when the evaluator has also sampled that declaration's tuple. + if (isinstance(node, Identifier) + and self._security_identifier_is_global_binding(node) + and node.name in self._security_arg_global_values): + return True if self._get_ta_site(node) is not None: return True if isinstance(node, FuncCall): diff --git a/pineforge_codegen/codegen/visit_stmt.py b/pineforge_codegen/codegen/visit_stmt.py index e367346c..b09f4c4b 100644 --- a/pineforge_codegen/codegen/visit_stmt.py +++ b/pineforge_codegen/codegen/visit_stmt.py @@ -1711,7 +1711,7 @@ def emit_call_tuple(call_expr: str, *, decompose: bool = False) -> None: # User-defined function returning a tuple: use C++17 structured bindings if isinstance(node.value, FuncCall): func_name, namespace = self._resolve_callee(node.value.callee) - if namespace == "request" and func_name == "security": + if namespace == "request" and func_name in ("security", "security_lower_tf"): # A request whose names are reassigned is read where it is # evaluated (external_requests): its data-missing stop. if UNPINNED_ANNOTATION in (node.value.annotations or {}): @@ -1768,6 +1768,14 @@ def _tuple_call_info(self, call: FuncCall): def _tuple_binding_cpp_types(self, node: TupleAssign) -> list[str]: """Exact supported tuple element types for later lexical operations.""" count = len(node.names) + lower_types = (node.value.annotations or {}).get( + "lower_tf_tuple_element_types", (), + ) + if lower_types: + return [ + f"std::vector<{ctype}>" + for ctype in self._lower_tf_tuple_cpp_types(lower_types) + ] if isinstance(node.value, (IfStmt, SwitchStmt)): return self._infer_selection_tuple_types(node.value, count) if not isinstance(node.value, FuncCall): diff --git a/pineforge_codegen/lexer.py b/pineforge_codegen/lexer.py index aba38033..79f3e6d1 100644 --- a/pineforge_codegen/lexer.py +++ b/pineforge_codegen/lexer.py @@ -386,12 +386,24 @@ def _tokenize_line(self) -> None: indent_level += 1 raw = self.source[line_start: self.pos] - if " \t" in raw: + columns = raw.count("\t") * 4 + raw.count(" ") + mixed = "\t" in raw and " " in raw + aligned_block = ( + columns % 4 == 0 + and columns // 4 <= self.indent_stack[-1] + 1 + ) + if " \t" in raw and not aligned_block: self._emit_diagnostic( "Indentation cannot contain spaces followed by a tab.", self.line, self.col, self.col + 1, ) - if "\t" in raw: + if mixed and aligned_block and not self._in_continuation: + indent_level = columns // 4 + elif "\t" in raw: + # Keep the established treatment of incomplete mixed prefixes + # and first-body skipped widths, including pending logical-line + # continuations. Their TV grammar is not established by the + # nested, complete-level layouts. indent_level = raw.count("\t") else: indent_level = len(raw) // 4 diff --git a/pineforge_codegen/parser.py b/pineforge_codegen/parser.py index 2645bf0f..2ea987d0 100644 --- a/pineforge_codegen/parser.py +++ b/pineforge_codegen/parser.py @@ -835,12 +835,19 @@ def _looks_like_call_template_args(self) -> bool: return False def _parse_var_keyword_decl(self) -> VarDecl: - """Parse: var [type] name = expr or varip [type] name = expr""" + """Parse: (var | varip) [[qualifier] type] name = expr.""" start_tok = self._current() is_var = self._current().type == TokenType.VAR is_varip = self._current().type == TokenType.VARIP self._advance() + qualifier = None + if (self._check(TokenType.IDENT) + and self._current().value in ("const", "series", "simple") + and (self._peek().type in TYPE_KEYWORDS + or self._is_ident_typed_var_decl(offset=1))): + qualifier = self._advance().value + type_hint = None if self._current().type in TYPE_KEYWORDS: type_hint = self._parse_type_hint_string() @@ -873,6 +880,10 @@ def _parse_var_keyword_decl(self) -> VarDecl: name=name_tok.value, value=value, is_var=is_var, is_varip=is_varip, type_hint=type_hint, ) + if qualifier is not None: + node.annotations = {"qualifier": qualifier} + if self._library and qualifier == "const": + node.annotations["declared_const"] = True self._retain_declaration_names(node, name_tok) return self._set_loc(node, start_tok) diff --git a/tests/test_callable_ta_forwarded_length.py b/tests/test_callable_ta_forwarded_length.py index 10e82dca..77833618 100644 --- a/tests/test_callable_ta_forwarded_length.py +++ b/tests/test_callable_ta_forwarded_length.py @@ -11,6 +11,7 @@ from pineforge_codegen import transpile from pineforge_codegen.errors import CompileError +from tests._compile import compile_cpp from tests.test_runtime_var_initialization import _compile_and_run @@ -640,3 +641,16 @@ def test_recursive_stateful_callable_is_rejected_before_instance_expansion() -> CompileError, match="Recursive stateful callable cycle" ): transpile(source) + + +@pytest.mark.parametrize("average", ["rma", "ema", "sma"]) +def test_unreachable_forwarding_keeps_live_input_backed_lengths(average) -> None: + source = f'''//@version=6 +strategy("reachable input length") +filtered(float sample, int window) => ta.{average}(sample, window) +unreached(float sample, int window) => filtered(filtered(sample, window), window) +period = input.int(7, "Period", minval = 1) +observed = filtered(window = period, sample = close) +plot(observed) +''' + compile_cpp(transpile(source, check_support=True), label="reachable-input-backed-ta-length") diff --git a/tests/test_lower_timeframe_tuple_types.py b/tests/test_lower_timeframe_tuple_types.py new file mode 100644 index 00000000..00fac391 --- /dev/null +++ b/tests/test_lower_timeframe_tuple_types.py @@ -0,0 +1,52 @@ +"""Lower-timeframe tuple elements retain independent array types.""" + +import pytest + +from pineforge_codegen import transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser +from pineforge_codegen.symbols import TypeSpec +from tests._compile import compile_cpp + + +@pytest.mark.parametrize("helper", [False, True], ids=["inline", "helper"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +def test_lower_timeframe_tuple_collection_types_are_per_element(helper, named): + payload = '[close, bar_index, close > open, "sample", color.blue]' + declaration = "" + if helper: + declaration = ( + "pack(float price, int ordinal, bool rising) =>\n" + ' [price, ordinal, rising, "sample", color.blue]\n' + ) + payload = "pack(close, bar_index, close > open)" + arguments = ( + f'expression = {payload}, timeframe = "15", symbol = syminfo.tickerid' + if named else f'syminfo.tickerid, "15", {payload}' + ) + source = ( + '//@version=6\nstrategy("typed lower tuple")\n' + declaration + + f"[prices, positions, rising, labels, colors] = request.security_lower_tf({arguments})\n" + + "plot(array.size(prices) + array.size(positions) + array.size(rising)" + + " + array.size(labels) + array.size(colors))\n" + ) + ast = Parser(Lexer(source).tokenize(), source=source).parse() + context = Analyzer(ast).analyze() + for name, inner in zip( + ("prices", "positions", "rising", "labels", "colors"), + ("float", "int64", "bool", "string", "color"), + ): + assert context.collection_types[name] == TypeSpec.array(TypeSpec.primitive(inner)) + compile_cpp(transpile(source, check_support=True), label="lower-tuple-per-element-types") + + +def test_lower_timeframe_tuple_arrays_keep_lexical_method_dispatch(): + source = """//@version=6 +strategy("local lower tuple") +readSize() => + [prices, rising] = request.security_lower_tf(syminfo.tickerid, "15", [close, close > open]) + prices.size() + rising.size() +plot(readSize()) +""" + compile_cpp(transpile(source, check_support=True), label="lower-tuple-lexical-array-dispatch") diff --git a/tests/test_mixed_block_indentation.py b/tests/test_mixed_block_indentation.py new file mode 100644 index 00000000..23d0790b --- /dev/null +++ b/tests/test_mixed_block_indentation.py @@ -0,0 +1,40 @@ +"""Mixed indentation preserves nested block structure and accepted wraps.""" + +import pytest + +from pineforge_codegen.ast_nodes import IfStmt, VarDecl +from pineforge_codegen.lexer import Lexer, TokenType +from pineforge_codegen.parser import Parser + + +@pytest.mark.parametrize( + "outer,inner", + [("\t", "\t "), (" ", " \t"), ("\t", "\t\t"), (" ", " ")], +) +def test_mixed_complete_levels_keep_nested_ast_and_dedent(outer, inner): + source = ( + "if close > open\n" + outer + "if high > close\n" + + inner + "observed = close\n" + + "after = open\n" + ) + tokens = Lexer(source).tokenize() + assert [token.line for token in tokens if token.type == TokenType.INDENT] == [2, 3] + assert [token.line for token in tokens if token.type == TokenType.DEDENT] == [4, 4] + program = Parser(tokens, source=source).parse() + outer_node, after = program.body + assert isinstance(outer_node, IfStmt) + (inner_node,) = outer_node.body + assert isinstance(inner_node, IfStmt) + (observed,) = inner_node.body + assert isinstance(observed, VarDecl) + assert observed.name == "observed" + assert isinstance(after, VarDecl) + assert after.name == "after" + + +@pytest.mark.parametrize("extra_spaces", [1, 2, 3]) +def test_previously_accepted_tab_first_incomplete_widths_keep_their_block(extra_spaces): + source = "if close > open\n\t" + " " * extra_spaces + "observed = close\n" + program = Parser(Lexer(source).tokenize(), source=source).parse() + (declaration,) = program.body[0].body + assert declaration.name == "observed" diff --git a/tests/test_persistent_declaration_qualifiers.py b/tests/test_persistent_declaration_qualifiers.py new file mode 100644 index 00000000..c47db1d6 --- /dev/null +++ b/tests/test_persistent_declaration_qualifiers.py @@ -0,0 +1,88 @@ +"""Persistence and qualifiers occupy separate positions in the declaration AST.""" + +import pytest + +from pineforge_codegen.ast_nodes import VarDecl +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser + + +@pytest.mark.parametrize("storage", ["var", "varip"]) +@pytest.mark.parametrize( + "qualifier,hint,initializer,expected_hint", + [ + ("const", "string", '"x"', "string"), + ("const", "float", "1.5", "float"), + ("const", "int", "3", "int"), + ("const", "bool", "true", "bool"), + ("const", "color", "color.blue", "color"), + ("simple", "int", "3", "int"), + ("series", "float", "close", "float"), + ("series", "float[]", "array.new_float(0)", "array"), + ("series", "array", "array.new_float(0)", "array"), + ("series", "Sample", "Sample.new()", "Sample"), + ("series", "lib.Sample", "lib.newSample()", "lib.Sample"), + ], +) +def test_persistence_then_qualifier_keeps_the_declaration( + storage, qualifier, hint, initializer, expected_hint, +): + source = f"{storage} {qualifier} {hint} held = {initializer}\n" + program = Parser(Lexer(source).tokenize(), source=source).parse() + assert len(program.body) == 1 + declaration = program.body[0] + assert isinstance(declaration, VarDecl) + assert declaration.name == "held" + assert declaration.type_hint == expected_hint + assert (declaration.is_var, declaration.is_varip) == ( + storage == "var", storage == "varip", + ) + assert declaration.annotations["qualifier"] == qualifier + assert (declaration.loc.line, declaration.loc.col) == (1, 1) + ((name, location),) = declaration.annotations["declaration_names"] + assert name == "held" + assert (location.line, location.col) == (1, source.index("held") + 1) + + +@pytest.mark.parametrize("storage", ["var", "varip"]) +@pytest.mark.parametrize("word", ["const", "simple", "series"]) +@pytest.mark.parametrize("hint", ["", "int ", "Sample "]) +def test_contextual_qualifier_words_in_the_name_slot_are_names(storage, word, hint): + source = f"{storage} {hint}{word} = 3\n" + declaration = Parser(Lexer(source).tokenize(), source=source).parse().body[0] + assert isinstance(declaration, VarDecl) + assert declaration.name == word + assert declaration.type_hint == (hint.strip() or None) + assert "qualifier" not in declaration.annotations + + +@pytest.mark.parametrize("storage", ["var", "varip"]) +@pytest.mark.parametrize("word", ["const", "simple", "series"]) +def test_contextual_qualifier_word_as_a_type_keeps_its_type(storage, word): + source = f"{storage} {word} held = {word}.new()\n" + declaration = Parser(Lexer(source).tokenize(), source=source).parse().body[0] + assert declaration.type_hint == word + assert declaration.name == "held" + assert "qualifier" not in declaration.annotations + + +@pytest.mark.parametrize("storage", ["var", "varip"]) +@pytest.mark.parametrize("header", ["if close > open", "make() =>"]) +def test_local_persistent_qualified_declaration_keeps_storage(storage, header): + source = f"{header}\n {storage} const float held = 1.5\n" + declaration = Parser(Lexer(source).tokenize(), source=source).parse().body[0].body[0] + assert isinstance(declaration, VarDecl) + assert declaration.annotations["qualifier"] == "const" + assert (declaration.is_var, declaration.is_varip) == ( + storage == "var", storage == "varip", + ) + assert (declaration.loc.line, declaration.loc.col) == (2, 5) + + +@pytest.mark.parametrize("storage", ["var", "varip"]) +def test_library_persistent_const_keeps_constant_provenance(storage): + source = f"{storage} const float held = 1.5\n" + program = Parser(Lexer(source).tokenize(), source=source, library=True).parse() + declaration = program.body[0] + assert declaration.annotations["qualifier"] == "const" + assert declaration.annotations["declared_const"] is True diff --git a/tests/test_requested_na_helper_bindings.py b/tests/test_requested_na_helper_bindings.py new file mode 100644 index 00000000..22de8183 --- /dev/null +++ b/tests/test_requested_na_helper_bindings.py @@ -0,0 +1,52 @@ +"""Requested builtin observers retain their helper's parameter bindings.""" + +import pytest + +from pineforge_codegen import transpile +from tests._compile import compile_cpp +from tests.test_v6_declaration_request_compilation import ( + GLOBAL_TUPLE_HELPER, GLOBAL_TUPLE_HISTORY, TIMEFRAME_HELPER, TIME_HELPER, +) + + +@pytest.mark.parametrize( + "source", [TIMEFRAME_HELPER, GLOBAL_TUPLE_HELPER], + ids=["timeframe-change", "global-tuple"], +) +@pytest.mark.parametrize( + "definition", + [ + "valid(x) => nz(x)", + "valid(float x) => na(x) ? 0 : 1", + "valid(float x) => nz(source = x, replacement = 0.0)", + "valid(x) => x > 0 ? 1 : 0", + "valid(x) => math.abs(x)", + ], + ids=["nz", "typed-na", "typed-named-nz", "comparison-control", "math-control"], +) +def test_observer_bindings_compose_with_requested_context(source, definition): + source = source.replace("valid(x) => na(x) ? 0 : 1", definition, 1) + compile_cpp(transpile(source, check_support=True), label="requested-observer-binding") + + +@pytest.mark.parametrize( + "payload", + ["valid(close)", 'time("W")', 'timeframe.change("W") ? 1 : 0'], + ids=["helper-alone", "time-alone", "change-alone"], +) +def test_independent_requested_payload_parts_remain_compilable(payload): + source = TIME_HELPER.replace('valid(close) * time("W")', payload, 1) + compile_cpp(transpile(source, check_support=True), label="independent-requested-payload") + + +@pytest.mark.parametrize( + "declaration", + [ + '[upper, lower, ignored] = ta.macd(close, 12, 26, 9)', + '[upper, lower] = ta.supertrend(3.0, 10)', + ], + ids=["macd-control", "supertrend-control"], +) +def test_builtin_tuple_member_history_keeps_requested_storage(declaration): + source = GLOBAL_TUPLE_HISTORY.replace("[upper, lower] = channel()", declaration, 1) + compile_cpp(transpile(source, check_support=True), label="requested-builtin-tuple-history") diff --git a/tests/test_security_user_tuple_global.py b/tests/test_security_user_tuple_global.py index 4d856dfa..e0047fa6 100644 --- a/tests/test_security_user_tuple_global.py +++ b/tests/test_security_user_tuple_global.py @@ -4,8 +4,8 @@ bar when a request.security payload reads `g`, directly or through a function that reads the global: the global is a plain value of the requested context. A function with state (a ta.* call, a `var`, a request) advances that state once -per evaluation, so reading its tuple once per element would advance it twice; -that case, and the history of an element, stay refused. +per evaluation. Every element shares its declaration's requested-context +sample, including when the payload reads a member more than once. The value rows hold the requested series to numbers written here from the source and the 1-day alignment of a 15-minute chart (96 chart bars per day), @@ -16,11 +16,12 @@ from __future__ import annotations import math +import re import pytest from pineforge_codegen import transpile -from pineforge_codegen.errors import CompileError +from tests._compile import compile_cpp from tests._e2e import ( Build, chart_feed_head, execute_all, per_bar_mismatches, skip_unless_e2e_env, ) @@ -68,10 +69,19 @@ def test_a_pure_user_tuple_element_translates_in_a_payload(name, declarations, o @pytest.mark.parametrize("name,declarations,payload", STATEFUL, ids=[row[0] for row in STATEFUL]) -def test_a_stateful_user_tuple_element_stays_refused(name, declarations, payload): - with pytest.raises(CompileError, match="an element of a tuple declaration it cannot re-evaluate") as caught: - transpile(_source(declarations, payload, "close", "60")) - assert [d.code for d in caught.value.diagnostics] == ["PF-E3044"] +def test_a_stateful_user_tuple_declaration_is_sampled_once(name, declarations, payload): + cpp = transpile( + _source(declarations, f"{payload} + {payload} + b", "close", "60"), + check_support=True, + ) + evaluator = cpp.split( + "void _eval_security_0(const Bar& bar, bool is_complete) {", 1, + )[1].split("\n }\n", 1)[0] + samples = re.findall(r"const auto (_pf_sec0_tuple_\d+_value) = ", evaluator) + assert len(samples) == 1 + assert evaluator.count(f"std::get<0>({samples[0]})") == 2 + assert f"std::get<1>({samples[0]})" in evaluator + compile_cpp(cpp, label=f"requested-tuple-{name}") def _column(outcome, name): diff --git a/tests/test_v6_declaration_request_compilation.py b/tests/test_v6_declaration_request_compilation.py new file mode 100644 index 00000000..1b9ae392 --- /dev/null +++ b/tests/test_v6_declaration_request_compilation.py @@ -0,0 +1,260 @@ +"""Complete strategy compilation for qualified declarations and request payloads. + +These are compile regressions. TradingView value comparisons belong to the +parity population and are separate from this translation-unit check. +""" + +import pytest + +from pineforge_codegen import transpile +from tests._compile import compile_cpp, skip_if_no_compile_env + + +COLOR_DECLARATION = """//@version=6 +strategy("var const declaration", overlay = true, initial_capital = 10000) +var const color LINE_COLOR = color.blue +fast = ta.ema(close, 10) +slow = ta.ema(close, 30) +plot(fast, color = LINE_COLOR) +if ta.crossover(fast, slow) + strategy.entry("Long", strategy.long, qty = 0.01) +if ta.crossunder(fast, slow) + strategy.close("Long") +""" + +FLOAT_DECLARATION = """//@version=6 +strategy("var const float in a calculation", overlay = true, initial_capital = 10000) +var const float BAND = 0.002 +fast = ta.ema(close, 10) +slow = ta.ema(close, 30) +if fast > slow * (1 + BAND) + strategy.entry("Long", strategy.long, qty = 0.01) +if fast < slow + strategy.close("Long") +""" + +LOWER_TUPLE = """//@version=6 +strategy("security_lower_tf with a tuple", overlay = true, initial_capital = 10000) +[closes, opens] = request.security_lower_tf(syminfo.tickerid, "15", [close, open]) +float body = array.size(closes) > 0 ? array.last(closes) - array.first(opens) : close - open +fast = ta.ema(body, 5) +slow = ta.ema(body, 20) +if ta.crossover(fast, slow) + strategy.entry("Long", strategy.long, qty = 0.01) +if ta.crossunder(fast, slow) + strategy.close("Long") +""" + +MIXED_BLOCKS = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +fast = ta.ema(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) + if close > open + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + +TIME_HELPER = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +valid(x) => na(x) ? 0 : 1 +weekStart = request.security(syminfo.tickerid, "D", valid(close) * time("W")) +fast = ta.ema(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) and time >= weekStart + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + +TIMEFRAME_HELPER = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +valid(x) => na(x) ? 0 : 1 +dailyScore = request.security(syminfo.tickerid, "D", valid(close) + (timeframe.change("W") ? 1 : 0)) +fast = ta.ema(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) and dailyScore == 1 + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + +GLOBAL_TUPLE_HELPER = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +channel() => + upper = ta.highest(high, 20) + lower = ta.lowest(low, 20) + [upper, lower] +[upper, lower] = channel() +valid(x) => na(x) ? 0 : 1 +score() => valid(close) + valid(upper) +dailyScore = request.security(syminfo.tickerid, "D", score()) +fast = ta.ema(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) and dailyScore == 2 + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + +GLOBAL_TUPLE_HISTORY = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +channel() => + upper = ta.highest(high, 20) + lower = ta.lowest(low, 20) + [upper, lower] +[upper, lower] = channel() +dailyAbove = request.security(syminfo.tickerid, "D", close > upper[1]) +fast = ta.ema(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) and dailyAbove + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + +UNUSED_FORWARDER = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +smooth(src, len) => ta.rma(src, len) +smoothTwice(src, len) => smooth(smooth(src, len), len) +fast = smooth(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + + +@pytest.mark.parametrize( + "source", + [ + pytest.param(COLOR_DECLARATION, id="color"), + pytest.param(FLOAT_DECLARATION, id="float"), + pytest.param( + COLOR_DECLARATION.replace("\nvar const ", "\nconst ", 1), + id="color-control", + ), + pytest.param( + FLOAT_DECLARATION.replace("\nvar const ", "\nconst ", 1), + id="float-control", + ), + pytest.param( + COLOR_DECLARATION.replace( + "var const color LINE_COLOR = color.blue", + "varip const int C = 3", + ).replace("color = LINE_COLOR", "color = color.blue"), + id="varip-int", + ), + ], +) +def test_persistent_qualified_declaration_compiles(source): + skip_if_no_compile_env() + compile_cpp(transpile(source, check_support=True), label="persistent-qualified-declaration") + + +@pytest.mark.parametrize( + "declaration", + [ + pytest.param( + '[closes, opens] = request.security_lower_tf(syminfo.tickerid, "15", [close, open])', + id="inline", + ), + pytest.param( + 'closeOpen() =>\n [close, open]\n' + '[closes, opens] = request.security_lower_tf(syminfo.tickerid, "15", closeOpen())', + id="helper", + ), + pytest.param( + 'closes = request.security_lower_tf(syminfo.tickerid, "15", close)\n' + 'opens = request.security_lower_tf(syminfo.tickerid, "15", open)', + id="separate-control", + ), + ], +) +def test_lower_timeframe_tuple_arrays_compile(declaration): + skip_if_no_compile_env() + source = LOWER_TUPLE.replace(LOWER_TUPLE.splitlines()[2], declaration, 1) + compile_cpp(transpile(source, check_support=True), label="lower-timeframe-tuple-arrays") + + +@pytest.mark.parametrize( + "outer,inner", + [ + pytest.param("\t", "\t ", id="tab-then-spaces"), + pytest.param(" ", " \t", id="spaces-then-tab"), + pytest.param("\t", "\t\t", id="tabs-control"), + pytest.param(" ", " ", id="spaces-control"), + ], +) +def test_mixed_nested_block_compiles(outer, inner): + skip_if_no_compile_env() + source = MIXED_BLOCKS.replace( + "\tif close > open\n\t strategy.entry", + outer + "if close > open\n" + inner + "strategy.entry", + 1, + ) + compile_cpp(transpile(source, check_support=True), label="mixed-nested-block") + + +@pytest.mark.parametrize( + "source", + [ + pytest.param(TIME_HELPER, id="time-na"), + pytest.param( + TIME_HELPER.replace("valid(x) => na(x) ? 0 : 1", "valid(x) => nz(x)"), + id="time-nz", + ), + pytest.param( + TIME_HELPER.replace("valid(x)", "valid(float x)", 1), + id="time-typed", + ), + pytest.param( + TIME_HELPER.replace('valid(close) * time("W")', '(na(close) ? 0 : 1) * time("W")'), + id="time-inline-control", + ), + pytest.param(TIMEFRAME_HELPER, id="timeframe-change"), + pytest.param(GLOBAL_TUPLE_HELPER, id="global-tuple-helper"), + pytest.param( + GLOBAL_TUPLE_HELPER.replace( + "[upper, lower] = channel()\nvalid(x)", "valid(x)", 1, + ).replace( + "score() => valid(close) + valid(upper)", + "score() =>\n [upper, lower] = channel()\n valid(close) + valid(upper)", + 1, + ), + id="local-tuple-control", + ), + pytest.param(GLOBAL_TUPLE_HISTORY, id="global-tuple-history"), + pytest.param( + GLOBAL_TUPLE_HISTORY.replace( + "[upper, lower] = channel()", "upper = ta.highest(high, 20)", 1, + ), + id="direct-history-control", + ), + ], +) +def test_requested_helpers_keep_parameter_and_tuple_bindings(source): + skip_if_no_compile_env() + compile_cpp(transpile(source, check_support=True), label="requested-helper-bindings") + + +@pytest.mark.parametrize( + "source", + [ + pytest.param(UNUSED_FORWARDER, id="uncalled"), + pytest.param( + UNUSED_FORWARDER.replace( + "smoothTwice(src, len) => smooth(smooth(src, len), len)\n", "", 1, + ), + id="removed-control", + ), + pytest.param( + UNUSED_FORWARDER + "plot(smoothTwice(close, 9))\n", id="called-control", + ), + ], +) +def test_unreachable_length_forwarding_does_not_allocate_ta(source): + skip_if_no_compile_env() + compile_cpp(transpile(source, check_support=True), label="unused-length-forwarder") diff --git a/tests/test_v6_reporter_controls.py b/tests/test_v6_reporter_controls.py new file mode 100644 index 00000000..2c03dcea --- /dev/null +++ b/tests/test_v6_reporter_controls.py @@ -0,0 +1,48 @@ +"""Previously accepted control forms keep their native compilation contract.""" + +import pytest + +from pineforge_codegen import transpile +from tests._compile import compile_cpp +from tests.test_i372_form_1 import BUILTIN_CONTROL, BUILTIN_SCRIPT, LOCAL_SCRIPT +from tests.test_input_string_var_default import ( + NONDEFAULT_BRANCH, NONDEFAULT_CONTROL, OPTIONS_CONTROL, VARIABLE_OPTIONS, +) +from tests.test_v6_declaration_request_compilation import COLOR_DECLARATION, UNUSED_FORWARDER + + +def test_conditional_history_call_control_keeps_both_written_sites(): + source = LOCAL_SCRIPT.replace( + "wrapper(x) =>\n s = x\n change(s)\nv = wrapper(close)", + "v = close > open ? change(close) : change(open)", + 1, + ) + compile_cpp(transpile(source, check_support=True), label="conditional-history-call-control") + + +@pytest.mark.parametrize("source", [BUILTIN_SCRIPT, BUILTIN_CONTROL]) +def test_builtin_derived_history_control_has_native_types(source): + compile_cpp(transpile(source, check_support=True), label="builtin-derived-history-control") + + +@pytest.mark.parametrize( + "source", + [NONDEFAULT_BRANCH, NONDEFAULT_CONTROL, VARIABLE_OPTIONS, OPTIONS_CONTROL], + ids=["default-var", "default-control", "options-var", "options-control"], +) +def test_reported_string_inputs_keep_native_compilation(source): + compile_cpp(transpile(source, check_support=True), label="string-input-var-control") + + +def test_persistent_declaration_without_qualifier_keeps_its_storage(): + source = COLOR_DECLARATION.replace("\nvar const color ", "\nvar color ", 1) + compile_cpp(transpile(source, check_support=True), label="persistent-color-control") + + +def test_uncalled_direct_ta_length_control_keeps_compiling(): + source = UNUSED_FORWARDER.replace( + "smoothTwice(src, len) => smooth(smooth(src, len), len)", + "unused(src, len) => ta.rma(src, len) * 2", + 1, + ) + compile_cpp(transpile(source, check_support=True), label="unused-direct-length-control")