diff --git a/CHANGELOG.md b/CHANGELOG.md index 88dac8f9..5fe3ef1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,36 @@ supported as exact pairs; on the 0.x line they are independent. See the ## Unreleased (next release) +- Strategy sizing constants retain their string values in expressions, aliases + and helper calls. Host order and risk settings convert these values to their + numeric representation only at the setting boundary. + +- `timeframe.in_seconds` and `timeframe.change` resolve an evaluated empty + string to the chart period, including input, variable, function-return and + named arguments. The argument is evaluated once, and nonempty strings keep + their explicit period. Bare `na` remains accepted; the value returned by + `timeframe.change(na)` is still awaiting verification against TradingView. + Non-string timeframe arguments are refused before C++ emission with the + stable `PF-E2095` type diagnostic and a source location, including input + values, reassignments, indicator results and helper parameters. Equally + specific diagnostic templates distinguish primitive types from qualifiers + using their catalog argument kinds. Dotted builtin signatures retain supplied + arguments, including escaped string literals and VWAP anchors. (#394) +- Script identifiers spelled like integer-to-float conversion temporaries, + including the underscore suffixes of nested conversions, now use the + generator's reserved-name escaping. +- Reference-typed UDT fields keep their type when initialized or reassigned + with `na`, including array defaults. Drawing handles cross user-function + and tuple boundaries by value, and `label(na)` produces a missing label + handle. Polyline locals, arrays, fields and delete methods retain opaque + handles and argument evaluation while rendering remains a visual no-op. +- Generated function variants and persistent local copies avoid authored and + generated member names, including names ending in `_cs1` and composed names + 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. + - 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. The paired engine reports a typed timezone error identifying the failed bar. diff --git a/docs/reference/support-contracts.md b/docs/reference/support-contracts.md index 7487bdda..cf55d2f5 100644 --- a/docs/reference/support-contracts.md +++ b/docs/reference/support-contracts.md @@ -20,6 +20,12 @@ lowering. Structural mutation, collection reassignment/escape, history and pair-binder iteration retain PF-E5086 pending TV evidence/provenance support; declaration tails yielding collections as callable/selection values are refused. `polyline.all` retains PF-E5085 until the engine has a Polyline handle/arena. +Polyline locals, array elements and UDT fields retain opaque typed handles; +default construction and bare `na` use a missing handle, and copies retain +its identity. `polyline.new` and functional or method-form `delete` keep the +existing visual-only rendering contract, while evaluating their arguments +once in parameter order. Opaque ID allocation joins script reset and rollback +state. This does not supply the live arena required by `polyline.all`. `var`/`varip` aliases retain PF-E5086 until a TradingView capture establishes their lifetime semantics. Whether a later creation in the same bar is visible through the alias is not yet measured against TradingView. diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 772e2426..6c1ec40c 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -27,6 +27,7 @@ SymbolTable, TypeSpec, method_receiver_type_name, + public_type_name, unify_pine_types, ) from ..errors import SourceLocation, Diagnostic, CompileError, Level, Phase @@ -265,6 +266,7 @@ def __init__(self, ast: Program, filename: str = "", self._block_var_owner: dict[str, int] = {} self._block_var_renames: dict[int, dict[str, str]] = {} self._block_var_seq = 0 + self._block_var_taken: set[str] | None = None # Identifier node identity -> the lexical Symbol.scope resolved while # that node's source scope is live. Codegen uses this narrow # provenance to distinguish true global aliases from same-named local @@ -5018,6 +5020,31 @@ def _record_script_block_binding(self, sym: Symbol, outer: Symbol | None) -> Non or any(id(body) == outer_owner for body in self._block_node_stack) ) + def _allocate_block_var_member(self, node: VarDecl) -> str: + """Keep generated lexical storage apart from every syntax spelling.""" + if self._block_var_taken is None: + self._block_var_taken = set() + for item, _depth in iter_ast_nodes(self._ast): + if self._budget is not None: + self._budget.check(item.loc, Phase.ANALYZER) + for attr in ("name", "var", "member"): + value = getattr(item, attr, None) + if isinstance(value, str): + self._block_var_taken.add(value) + for attr in ("names", "vars", "params", "members"): + self._block_var_taken.update( + value for value in (getattr(item, attr, None) or ()) + if isinstance(value, str) + ) + while True: + if self._budget is not None: + self._budget.check(node.loc, Phase.ANALYZER) + self._block_var_seq += 1 + member_name = f"{node.name}__blk{self._block_var_seq}" + if member_name not in self._block_var_taken: + self._block_var_taken.add(member_name) + return member_name + def _visit_VarDecl(self, node: VarDecl) -> PineType: if (not node.is_var and not node.is_varip and node.type_hint is None and isinstance(node.value, NaLiteral) @@ -5276,8 +5303,7 @@ def _visit_VarDecl(self, node: VarDecl) -> PineType: self._block_var_owner[node.name] = block_id elif owner != block_id: # Sibling-scope collision: disambiguate this declaration. - self._block_var_seq += 1 - member_name = f"{node.name}__blk{self._block_var_seq}" + member_name = self._allocate_block_var_member(node) self._block_var_renames.setdefault(block_id, {})[node.name] = member_name self._var_members.append((member_name, val_type, init_str)) scope_cursor = self._symbols.current_scope @@ -7157,15 +7183,6 @@ def _operator_operand_type( or authored_method ): result = None - if result is None and isinstance(node, MemberAccess): - if isinstance(node.object, Identifier): - ns = node.object.name - if ns == "syminfo": - ptype = sigs.SYMINFO_VARIABLES.get(f"syminfo.{node.member}") - if ptype is not None: - result = TypeSpec.primitive(ptype.value) - elif ns == "timeframe" and node.member == "period": - result = TypeSpec.primitive("string") if result is None and isinstance(node, FuncCall): callee = node.callee if isinstance(callee, Identifier): @@ -7459,6 +7476,46 @@ def _visit_Ternary(self, node: Ternary) -> PineType: return main_type return result_type + def _check_timeframe_string_arguments( + self, node: FuncCall, + parameter_types: dict[str, PineType | TypeSpec] | None = None, + ) -> None: + """Check string signature slots using lexical and written-call types.""" + if self._pine_version != 6 or self._in_v5_library_definition(node): + return + callee = node.callee + if (not isinstance(callee, MemberAccess) + or not isinstance(callee.object, Identifier) + or callee.object.name != "timeframe" + or self._value_bindings.declaration_bindings.get(id(callee.object)) is not None + or (parameter_types is not None and callee.object.name in parameter_types)): + return + intrinsic = sigs.BUILTIN_FUNCTIONS.get(f"timeframe.{callee.member}") + if intrinsic is None: + return + bound = self._bind_callable_args(node, intrinsic.param_names) + for parameter, value in zip(intrinsic.primary.params, bound): + if value is None or parameter.pine_type != PineType.STRING: + continue + actual = self._operator_operand_type(value, parameter_types) + if actual is None or actual in { + TypeSpec.primitive("string"), TypeSpec.primitive("na"), + }: + # Untyped helper parameters stay open until their written + # call supplies a type. Bare na adopts the string signature. + continue + function_name = intrinsic.name + argument_type = public_type_name(actual) or str(actual) + parameter_type = parameter.pine_type.value + name = parameter.name + from ..pine_spelling import expr_start + + self._error( + f"Cannot call '{function_name}' with an argument of type {argument_type} " + f"for {parameter_type} parameter '{name}'.", + expr_start(value).loc or value.loc, + ) + def _visit_FuncCall(self, node: FuncCall) -> PineType: if self._pine_version == 6 and not self._in_v5_library_definition(node): self._parameter_qualifiers.check_builtin(node) @@ -7589,6 +7646,7 @@ def _visit_FuncCall(self, node: FuncCall) -> PineType: visited_member_arg_types[id(arg)] = self._visit(arg) for val in node.kwargs.values(): visited_member_arg_types[id(val)] = self._visit(val) + self._check_timeframe_string_arguments(node) # UDT method call-site typing. Method definitions may contain an # untyped parameter history read; resolve receiver + args here and # apply the same deferred map-history gate as regular UDFs before @@ -8958,6 +9016,8 @@ def _visit_MemberAccess(self, node: MemberAccess) -> PineType: if drawing_all_kind(node) not in {None, "polyline"}: # Collections use TypeSpec for their shape; PineType stays scalar. return PineType.FLOAT + if self._builtin_sizing_constant(node) is not None: + return PineType.STRING # Handle specific namespaces if isinstance(node.object, Identifier): ns = node.object.name @@ -8967,9 +9027,6 @@ def _visit_MemberAccess(self, node: MemberAccess) -> PineType: # Direction constants if node.member in ("long", "short"): return PineType.INT - # Qty type constants - if node.member in ("percent_of_equity", "fixed", "cash"): - return PineType.INT # Commission type constants if node.member in ("commission",): return PineType.VOID # namespace prefix for .percent etc. diff --git a/pineforge_codegen/analyzer/types.py b/pineforge_codegen/analyzer/types.py index f65daf91..5e6d7a99 100644 --- a/pineforge_codegen/analyzer/types.py +++ b/pineforge_codegen/analyzer/types.py @@ -48,6 +48,7 @@ from ..errors import Phase from ..limits import iter_ast_nodes from ..matrix_overloads import matrix_sum_has_rhs +from ..reference_casts import na_reference_cast_kind from ..symbols import ( PineType, TypeSpec, @@ -59,7 +60,9 @@ # Drawing-objects-as-data type names (spec §4.1). Defined locally — the # analyzer must not import from ``codegen`` (codegen imports analyzer, so the # reverse would be a cycle). Mirrors codegen.tables.DRAWING_TYPE_TO_CPP keys. -_DRAWING_TYPE_NAMES = frozenset({"line", "box", "label", "linefill", "chart.point"}) +_DRAWING_TYPE_NAMES = frozenset({ + "line", "box", "label", "linefill", "chart.point", "polyline", +}) _DRAWING_NS = frozenset({"line", "box", "label", "linefill"}) _DIRECT_ARRAY_VALUE_PRODUCERS = frozenset({ "from", @@ -91,6 +94,26 @@ class TypeHelper: docstring above. """ + def _builtin_sizing_constant(self, value): + if not (isinstance(value, MemberAccess) + and isinstance(value.object, Identifier)): + return None + receiver = value.object + constant = sigs.get_sizing_constant(receiver.name, value.member) + if constant is None: + return None + parameters = getattr(self, "_parameter_types", None) + bindings = self._value_bindings.declaration_bindings + if ((self._pine_version == 6 and id(receiver) not in bindings) + or self._value_bindings.binding_of(receiver) is not None + or (self._pine_version != 6 + and self._symbols.resolve(receiver.name) is not None) + or (parameters is not None and receiver.name in parameters) + or receiver.name in self._udt_fields + or receiver.name in self._enum_defs): + return None + return constant + def _split_top_level_type_args(self, text: str) -> list[str]: args: list[str] = [] cur: list[str] = [] @@ -588,6 +611,10 @@ def direct_user_udt_ctor_name(node: ASTNode) -> str | None: return typed_receiver_spec # Drawing-objects-as-data return typing: *.new / *.copy -> handle of # the self-type; linefill.get_line* -> line; chart.point.* -> point. + if cast_kind := na_reference_cast_kind(value): + return TypeSpec.udt(cast_kind) + if ns == "polyline" and func == "new": + return TypeSpec.udt("polyline") if ns in _DRAWING_NS: if func in ("new", "copy"): return TypeSpec.udt(ns) @@ -807,6 +834,8 @@ def direct_user_udt_ctor_name(node: ASTNode) -> str | None: owner = self._type_spec_from_expr(value.object) if owner is not None and owner.kind == "udt" and owner.name: return (self._udt_field_type_specs.get(owner.name) or {}).get(value.member) + if self._builtin_sizing_constant(value) is not None: + return TypeSpec.primitive("string") return None @staticmethod @@ -1535,6 +1564,9 @@ def _check_node(self, node): op, operands = node.op, (node.operand,) elif isinstance(node, Ternary): op, operands = "?:", (node.condition, node.true_val, node.false_val) + elif isinstance(node, FuncCall): + self._analyzer._check_timeframe_string_arguments(node, self._parameter_types) + return else: return self._analyzer._check_operator_operands(node, op, operands, self._parameter_types) @@ -1544,6 +1576,23 @@ def _type_spec_from_expr(self, value, **kwargs): return self._analyzer._operator_operand_type( value, self._parameter_types, ) + if (isinstance(value, MemberAccess) + and isinstance(value.object, Identifier) + and self._pine_version == 6 + and not self._in_v5_library_definition(value)): + receiver = value.object + bindings = self._value_bindings.declaration_bindings + # Require the receiver's recorded lexical namespace use. A + # missing record is not proof, and even an unresolved parameter + # or local in the written-call environment shadows a namespace. + if (id(receiver) in bindings and bindings[id(receiver)] is None + and (self._parameter_types is None + or receiver.name not in self._parameter_types) + and receiver.name not in self._udt_fields + and receiver.name not in self._enum_defs): + spec = sigs.get_variable_type_spec(receiver.name, value.member) + if spec is not None: + return spec return super()._type_spec_from_expr(value, **kwargs) def _visit(self, node): diff --git a/pineforge_codegen/codegen/base.py b/pineforge_codegen/codegen/base.py index 6bd0a8ad..21e0785a 100644 --- a/pineforge_codegen/codegen/base.py +++ b/pineforge_codegen/codegen/base.py @@ -32,6 +32,7 @@ from ..errors import CompileError, Diagnostic, Level, Phase, SourceLocation from ..limits import TimeBudget, iter_ast_nodes, syntax_children from ..matrix_overloads import matrix_sum_has_rhs +from ..reference_casts import na_reference_cast_kind from . import graph_journal from .capacities import CapacityPolicy, validate_capacity_options from ..session_reads import emitted_session_reads @@ -736,7 +737,12 @@ def func_var_storage(owner: str, raw_name: str) -> str: remap = {} for orig_name in orig_names: safe = self._safe_name(orig_name) - remap[safe] = f"{safe}_cs{cs_idx}" + # Several enclosing owners can refer to the same clone. + # Its identity belongs to the original member + callsite, + # independently of which owner first requests its name. + remap[safe] = self._generated_class_name( + ("var-clone", safe, cs_idx), f"{safe}_cs{cs_idx}" + ) self._func_cs_var_remap[(fname, cs_idx)] = remap self._func_var_members_set.update( orig_name for orig_name in orig_names) @@ -1110,9 +1116,8 @@ def func_var_storage(owner: str, raw_name: str) -> str: # ``_active_var_remap`` while emitting the owning block's statements. self._block_var_renames: dict[int, dict[str, str]] = getattr(ctx, "block_var_renames", {}) or {} self._udt_field_type_specs: dict[str, dict[str, TypeSpec]] = getattr(ctx, "udt_field_type_specs", {}) or {} - # Map UDT struct name -> set of field names that were dropped from the - # emitted C++ struct because they had drawing-only types (label, line, - # box, linefill, polyline, table, chart.point). Populated eagerly + # Map UDT struct name -> visual-only table fields omitted from the + # emitted record. Populated eagerly # here from ``self._udt_defs`` so downstream visitors (visit_expr / # visit_stmt) can consult it before ``generate()`` runs. The struct # emission loop later asserts/syncs against this same map. Used to @@ -1120,9 +1125,9 @@ def func_var_storage(owner: str, raw_name: str) -> str: # generated C++ never references a member that doesn't exist on the # emitted struct. See: pineforge-codegen issue #10. # Drawing-objects-as-data: line/box/label/linefill/chart.point are now - # REAL data (un-dropped from UDT structs). Only table/polyline stay - # dropped (no C++ representation). See drawing-objects-as-data.md §4.2. - _DRAWING_TYPES_INIT = {"table", "polyline"} + # REAL data (un-dropped from UDT structs). Polyline retains an opaque + # handle while its geometry remains visual-only. Only table is dropped. + _DRAWING_TYPES_INIT = {"table"} self._udt_omitted_fields: dict[str, set[str]] = {} for _type_name, _fields in self._udt_defs.items(): _omitted = set() @@ -1198,14 +1203,14 @@ def func_var_storage(owner: str, raw_name: str) -> str: self._map_vars.add(_name) elif _spec.kind == "udt" and _spec.name: self._udt_var_types.setdefault(_name, _spec.name) - # Table / polyline variables and params have NO C++ representation + # Table variables and params have NO C++ representation # (SKIP_VAR_TYPES). A *method* call on such a receiver # (``panel.cell(...)``, ``dash.merge_cells(...)``) is a visual no-op # that must be dropped — but unlike the namespace form # (``table.cell(...)``) the receiver is a bare var/param the # namespace-based skip cannot see. Collect those names so # ``_is_skip_expr`` can drop their method calls. - _SKIP_DECL_TYPES = set(SKIP_VAR_TYPES) | {"polyline"} + _SKIP_DECL_TYPES = set(SKIP_VAR_TYPES) self._visual_drop_vars: set[str] = set() for _node in self._walk_ast(self.ctx.ast): if isinstance(_node, VarDecl): @@ -1221,8 +1226,8 @@ def func_var_storage(owner: str, raw_name: str) -> str: _h = _hints[_i] if _i < len(_hints) else None if _h and str(_h).replace(" ", "") in _SKIP_DECL_TYPES: self._visual_drop_vars.add(_p) - # Build set of all member names (series vars, var members) for collision - # detection. It precedes the security metadata below: a timeframe + # Build the authored/analyzer data-member inventory for callable name + # collisions. It precedes the security metadata below: a timeframe # computed by a user function (``tf = tfFromLabel(choice)``) renders # that call through ``_func_safe_name`` at registration time. self._all_member_names: set[str] = set() @@ -1230,6 +1235,15 @@ def func_var_storage(owner: str, raw_name: str) -> str: self._all_member_names.add(self._safe_name(name)) for name, _, _ in ctx.var_members: self._all_member_names.add(self._safe_name(name)) + for name, _ptype in ctx.global_var_decls: + self._all_member_names.add(self._safe_name(name)) + # Freeze natural callable identities before security rendering and + # later support allocators. Calls and declarations must use this same + # allocation even when an authored member is spelled like a clone. + for fi in ctx.func_infos: + self._func_cpp_base_name(fi.name) + for cs_idx in range(ctx.func_call_site_counts.get(fi.name, 0)): + self._func_variant_name(fi.name, cs_idx) # Collect request.security metadata per call self._security_eval_info: list[dict] = [] self._security_ta_variant_names: dict[tuple[int, int, tuple], str] = {} @@ -1359,7 +1373,10 @@ def func_var_storage(owner: str, raw_name: str) -> str: # Drawing-objects-as-data: gate all new emission (drawing.hpp include + # the per-type arenas) on this flag so non-drawing strategies stay # byte-identical. Caps come from the strategy() header max_*_count. - self._uses_drawing = self._detect_drawing_usage() + self._uses_polyline = self._detect_drawing_usage({"polyline"}) + self._uses_drawing = self._detect_drawing_usage( + set(DRAWING_TYPE_TO_CPP) - {"polyline"}, + ) self._drawing_caps = self._compute_drawing_caps() if self._uses_drawing else {} # Pine history directives retain their existing global-maximum rule. @@ -1451,14 +1468,7 @@ def _is_default_drawing_initializer(self, node) -> bool: """Whether a drawing RHS is the constant missing handle.""" return ( self._is_na_expr(node) - or ( - isinstance(node, FuncCall) - and isinstance(node.callee, Identifier) - and node.callee.name in ("line", "box") - and len(node.args) == 1 - and not node.kwargs - and self._is_na_expr(node.args[0]) - ) + or na_reference_cast_kind(node) is not None ) def _is_runtime_scalar_var_initializer( @@ -1573,6 +1583,7 @@ def _prepare_runtime_scalar_var_initializers(self) -> None: self._runtime_var_init_flags: dict[tuple[int, str], str] = {} used_names = set(self._all_member_names) + used_names.update(self._generated_class_names.values()) # ``_all_member_names`` historically covers persistent ``var`` and # Series members only. Plain global declarations are class members as # well, so include them before minting a generated flag; otherwise a @@ -1609,7 +1620,7 @@ def _prepare_runtime_scalar_var_initializers(self) -> None: for callsite in range( self.ctx.func_call_site_counts.get(func_info.name, 0) ): - used_names.add(f"{emitted}_cs{callsite}") + used_names.add(self._func_variant_name(func_info.name, callsite)) used_names.update(inst["name"] for inst in self._fresh_instances) def allocate_flag(base: str) -> str: @@ -2059,7 +2070,7 @@ def fixnan_originals(fname: str) -> list[str]: return list(self._func_cs_fixnan_remap.get((fname, 0), {}).keys()) def natural_name(fname: str, cs_idx: int) -> str: - return f"{self._func_cpp_base_name(fname)}_cs{cs_idx}" + return self._func_variant_name(fname, cs_idx) interned: dict[tuple, dict] = {} worklist: list[dict] = [] @@ -2178,17 +2189,28 @@ def natural_name(fname: str, cs_idx: int) -> str: ginst = interned.get(key) if ginst is None: fresh_counter += 1 - inst_name = f"{self._func_cpp_base_name(g_name)}__ni{fresh_counter}" + inst_name = self._generated_class_name( + ("function-instance", fresh_counter), + f"{self._func_cpp_base_name(g_name)}__ni{fresh_counter}", + ) fvar_remap: dict[str, str] = {} for v in var_originals(g_name): - fresh_member = f"{v}__ni{fresh_counter}" + fresh_member = self._generated_class_name( + ("var-instance", fresh_counter, v), + f"{v}__ni{fresh_counter}", + ) fvar_remap[v] = fresh_member self._fresh_var_members.append((g_name, v, fresh_member)) # Fresh fixnan members: each path gets its OWN previous- # value member so two call paths never share fixnan state. + # These are the owners of the reserved fixnan namespace, + # rather than identities composed from Pine spellings. ffixnan_remap: dict[str, str] = {} for orig_fn_member in fixnan_originals(g_name): - fresh_fn_member = f"{orig_fn_member}__ni{fresh_counter}" + fresh_fn_member = self._generated_helper_name( + ("fixnan-instance", fresh_counter, orig_fn_member), + f"{orig_fn_member}__ni{fresh_counter}", + ) ffixnan_remap[orig_fn_member] = fresh_fn_member # Find the original FixnanCallSite to carry its type. orig_fn_site = None @@ -3948,6 +3970,15 @@ def _func_cpp_base_name(self, fname: str) -> str: return self._emit_udt_method_cpp_name(fi) return self._func_safe_name(fname) + def _func_variant_name(self, fname: str, cs_idx: int) -> str: + """Resolve a callable clone once for emission, dispatch and history.""" + name = self._generated_class_name( + ("function-clone", fname, cs_idx), + f"{self._func_cpp_base_name(fname)}_cs{cs_idx}", + ) + self._func_variant_callsite[(fname, name)] = cs_idx + return name + def _inline_history_contexts_for_owner(self, owner: str | None) -> list[str | None]: """Return every method-emission context that owns one source AST site. @@ -3970,7 +4001,7 @@ def _inline_history_contexts_for_owner(self, owner: str | None) -> list[str | No ) and total_cs > 0 if cloned: contexts: list[str | None] = [ - f"{self._func_cpp_base_name(owner)}_cs{idx}" + self._func_variant_name(owner, idx) for idx in range(total_cs) ] else: @@ -4131,11 +4162,7 @@ def owner_cs_for_context( ) -> int | None: if owner is None or context is None: return None - prefix = f"{self._func_cpp_base_name(owner)}_cs" - if not context.startswith(prefix): - return None - suffix = context[len(prefix):] - return int(suffix) if suffix.isdigit() else None + return self._func_variant_callsite.get((owner, context)) def target_cs_for_context( fi, @@ -4271,7 +4298,7 @@ def emitted_context_for_call( fi, call, owner, owner_context ) if target_cs is not None: - return f"{self._func_cpp_base_name(fi.name)}_cs{target_cs}" + return self._func_variant_name(fi.name, target_cs) return None requested_roots: set[tuple[str, str | None]] = set() @@ -4733,6 +4760,7 @@ def _prepare_udt_generated_names(self) -> None: getattr(self, "_runtime_var_init_flag_used_names", ()) ) class_used.update(self._all_member_names) + class_used.update(self._generated_class_names.values()) class_used.update( self._safe_name(name) for name in self._all_bound_names ) @@ -6155,6 +6183,8 @@ def _generate_cpp(self) -> str: lines.append(" DrawingArena _pf_boxes_{_PF_DRAWING_UNBOUNDED};") lines.append(" DrawingArena _pf_labels_{_PF_DRAWING_UNBOUNDED};") lines.append(" DrawingArena _pf_linefills_{_PF_DRAWING_UNBOUNDED};") + if self._uses_polyline: + lines.append(" uint64_t _pf_polyline_next_id_ = 0;") # 8e. The copies an array or a matrix variable's executions left # (pineforge_codegen/collection_history.py). @@ -6209,12 +6239,14 @@ def allocate_legacy_flag(base: str) -> str: for fi in self.ctx.func_infos: if fi.name not in self.ctx.func_var_members: continue - emitted = self._func_cpp_base_name(fi.name) total_cs = self.ctx.func_call_site_counts.get(fi.name, 0) variants = range(total_cs) if total_cs > 0 else (None,) for cs_idx in variants: - suffix = f"_cs{cs_idx}" if cs_idx is not None else "" - flag = allocate_legacy_flag(f"_fvinit_{emitted}{suffix}") + emitted = ( + self._func_variant_name(fi.name, cs_idx) + if cs_idx is not None else self._func_cpp_base_name(fi.name) + ) + flag = allocate_legacy_flag(f"_fvinit_{emitted}") lines.append(f" bool {flag} = false;") for inst in self._fresh_instances: @@ -6564,13 +6596,17 @@ def _is_skip_expr(self, node) -> bool: if self._is_chart_point_callee(node.callee): return False func_name, namespace = self._resolve_callee(node.callee) + if namespace == "polyline" and func_name in {"new", "delete"}: + # The geometry is visual-only; its handle and argument + # evaluations must still flow through declarations and calls. + return False if namespace is None and func_name in SKIP_FUNC_NAMES: return True if namespace in SKIP_NAMESPACES: return True if namespace in SKIP_VAR_TYPES: return True - # Method call on a table/polyline-typed receiver var/param + # Method call on a table-typed receiver var/param # (``panel.cell(...)``). These types have no C++ representation, so # the call is a visual no-op — drop it (mirrors the namespace form). if namespace in self._visual_drop_vars: @@ -6586,9 +6622,8 @@ def _is_skip_expr(self, node) -> bool: def _is_omitted_udt_field(self, node) -> bool: """True when ``node`` is a ``MemberAccess`` on a UDT variable and the - member name was dropped from the emitted struct because it had a - drawing-only type (label, line, box, linefill, polyline, table, - chart.point). Callers use this to rewrite reads and strip writes so + member name was dropped from the emitted struct because it had the + visual-only table type. Callers rewrite reads and strip writes so the generated C++ never references a non-existent struct member. See: pineforge-codegen issue #10. """ diff --git a/pineforge_codegen/codegen/drawing.py b/pineforge_codegen/codegen/drawing.py index e64fc480..9ea6fb39 100644 --- a/pineforge_codegen/codegen/drawing.py +++ b/pineforge_codegen/codegen/drawing.py @@ -1,15 +1,15 @@ """Drawing-objects-as-data dispatch for the codegen. Spec: ``docs/drawing-objects-as-data.md`` §4.3 (the 4-form call dispatch), -§L (handle lifecycle) and §U (UDT × drawing). Everything here only ever -runs for a strategy whose ``self._uses_drawing`` is True (the dispatch is -reached solely when a ``line``/``box``/``label``/``linefill``/``chart.point`` -call or a drawing-typed receiver is present), so non-drawing strategies emit -byte-identical C++. +§L (handle lifecycle) and §U (UDT × drawing). Engine-backed drawing support is +emitted when ``self._uses_drawing`` is True; opaque polyline value support is +emitted when that type is used. Non-drawing strategies need neither block. The geometry of a drawing object becomes REAL C++ state (per-type arena in ``pineforge/drawing.hpp``); every VISUAL kwarg/method (color / style / width / bgcolor / text_* / extend-as-visual / force_overlay) is accepted-and-DROPPED. +Polyline geometry remains visual-only; its typed IDs and argument effects +survive assignments, collections and calls. ``DrawingVisitor`` is mixed into ``CodeGen``; it owns no state of its own and reads only attributes established by ``CodeGen.__init__`` (``_uses_drawing``, @@ -23,6 +23,7 @@ from .. import signatures as sigs from ..ast_nodes import FuncCall, Identifier, MemberAccess +from ..errors import CodegenInvariantError from ..symbols import method_receiver_type_name from .tables import ( ARRAY_VOID_METHODS as _ARRAY_VOID_METHODS, @@ -33,6 +34,30 @@ _MAP_VOID_METHODS = frozenset({"clear", "put_all"}) +POLYLINE_HANDLE_CPP = """\ +// Polyline rendering remains visual-only. Its opaque ID is still a value: +// copies preserve identity, default construction is na, and no numeric +// conversion exists. IDs are allocated by checkpointed per-strategy state. +struct _PFPolyline { + uint64_t id = 0; + bool is_na() const { return id == 0; } + friend bool operator==(const _PFPolyline& _pf_left, const _PFPolyline& _pf_right) { + return _pf_left.id == _pf_right.id; + } + friend bool operator!=(const _PFPolyline& _pf_left, const _PFPolyline& _pf_right) { + return !(_pf_left == _pf_right); + } +}; +""" + +_POLYLINE_PARAMS = { + "new": ( + "points", "curved", "closed", "xloc", "line_color", "fill_color", + "line_style", "line_width", "force_overlay", + ), + "delete": ("id",), +} + # Largest declared line, box or label count the native arena factory accepts: its int32 # domain minus the observed-slack it adds (drawing_bound_from_declared_count in # pineforge/drawing.hpp: INT32_MAX - kDrawingObservedSlack, with the slack 5). @@ -323,6 +348,39 @@ def _collected(dtype: str, handle: str) -> str: def _emit_drawing_ctor(self, dtype: str, node: FuncCall) -> str: return self._collected(dtype, self._emit_drawing_alloc(dtype, node)) + def _emit_polyline_call(self, method: str, node: FuncCall, receiver=None) -> str: + """Keep opaque handle flow and evaluate every supplied argument once. + + These calls have no renderer or live arena. Binding by parameter order + still matters: points/style expressions can call user functions or + mutate collections, and delete's receiver can itself shift an array. + """ + params = _POLYLINE_PARAMS.get(method) + if params is None: + raise CodegenInvariantError(node) + args = ([receiver] if receiver is not None else []) + list(node.args) + if (len(args) > len(params) + or any(name not in params or name in params[:len(args)] + for name in node.kwargs)): + raise CodegenInvariantError(node) + values = dict(zip(params, args)) + values.update(node.kwargs) + if params[0] not in values: + raise CodegenInvariantError(node) + if method == "delete": + value = self._visit_rhs_value( + values["id"], target_cpp_type=DRAWING_TYPE_TO_CPP["polyline"], + ) + return f"static_cast({value})" + statements = " ".join( + f"static_cast({self._visit_expr(values[name])});" + for name in params if name in values + ) + return ( + f"([&]() {{ {statements} " + "return _PFPolyline{++this->_pf_polyline_next_id_}; }())" + ) + def _emit_drawing_alloc(self, dtype: str, node: FuncCall) -> str: arena = DRAWING_ARENA[dtype] if dtype == "linefill": @@ -447,6 +505,8 @@ def _emit_drawing_method(self, dtype: str, method: str, recv_node, arg_nodes: li ``recv_node`` is the handle receiver; ``arg_nodes`` the remaining args. """ + if dtype == "polyline": + return self._emit_polyline_call(method, node, receiver=recv_node) arena = DRAWING_ARENA[dtype] recv = self._visit_expr(recv_node) geometry, noop = DRAWING_METHODS_BY_TYPE[dtype] @@ -572,7 +632,7 @@ def _drawing_call_return_cpp(self, node: FuncCall) -> str | None: _fn, ns = self._resolve_callee(callee) from .tables import DRAWING_NS dtype = None - if ns in DRAWING_NS: + if ns in DRAWING_NS or ns == "polyline": dtype = ns else: recv_spec = self._type_spec_from_expr(callee.object) @@ -597,7 +657,7 @@ def _drawing_call_is_void(self, node) -> bool: _fn, ns = self._resolve_callee(node.callee) from .tables import DRAWING_NS dtype = None - if ns in DRAWING_NS: + if ns in DRAWING_NS or ns == "polyline": dtype = ns else: recv_spec = self._type_spec_from_expr(node.callee.object) @@ -606,6 +666,8 @@ def _drawing_call_is_void(self, node) -> bool: dtype = recv_spec.name if dtype is None: return False + if dtype == "polyline": + return method == "delete" if method in ("new", "copy"): return False # constructors / copy return a handle if (dtype, method) in _DRAWING_GETTER_RET: @@ -792,57 +854,82 @@ def _emit_drawing_collectors(self, lines: list[str]) -> None: # ------------------------------------------------------------------ # _uses_drawing detection + arena caps # ------------------------------------------------------------------ - def _spec_mentions_drawing(self, spec) -> bool: + def _spec_mentions_drawing(self, spec, kinds=None) -> bool: + kinds = DRAWING_TYPE_TO_CPP if kinds is None else kinds if spec is None: return False - if spec.kind == "udt" and spec.name in DRAWING_TYPE_TO_CPP: + if spec.kind == "udt" and spec.name in kinds: return True if spec.kind == "array": - return self._spec_mentions_drawing(spec.element) + return self._spec_mentions_drawing(spec.element, kinds) if spec.kind == "map": - return self._spec_mentions_drawing(spec.key) or self._spec_mentions_drawing(spec.value) + return (self._spec_mentions_drawing(spec.key, kinds) + or self._spec_mentions_drawing(spec.value, kinds)) if spec.kind == "matrix": - return self._spec_mentions_drawing(spec.element) + return self._spec_mentions_drawing(spec.element, kinds) return False - def _detect_drawing_usage(self) -> bool: - """True iff emitted C++ needs pineforge/drawing.hpp + the arenas. + def _detect_drawing_usage(self, kinds=None) -> bool: + """Whether declarations or calls require any of the given handle kinds. - Mirrors ``_detect_matrix_usage``: any var/field/array of a drawing type - OR any line/box/label/linefill namespace call OR any chart.point call. + The same type walk selects real engine drawing support and opaque + polyline support independently, including unused typed parameters. """ + kinds = DRAWING_TYPE_TO_CPP if kinds is None else kinds for t in self._udt_var_types.values(): - if t in DRAWING_TYPE_TO_CPP: + if t in kinds: return True for spec in self._collection_types.values(): - if self._spec_mentions_drawing(spec): + if self._spec_mentions_drawing(spec, kinds): return True for specs in self._func_collection_types.values(): for spec in specs.values(): - if self._spec_mentions_drawing(spec): + if self._spec_mentions_drawing(spec, kinds): return True for specs in self._block_collection_types.values(): for spec in specs.values(): - if self._spec_mentions_drawing(spec): + if self._spec_mentions_drawing(spec, kinds): return True for fields in self._udt_field_type_specs.values(): for spec in fields.values(): - if self._spec_mentions_drawing(spec): + if self._spec_mentions_drawing(spec, kinds): return True for _tname, _fields in self._udt_defs.items(): for f in _fields: - if self._spec_mentions_drawing(self._type_spec_from_hint_name(f.type_name)): + if self._spec_mentions_drawing( + self._type_spec_from_hint_name(f.type_name), kinds, + ): return True - from .tables import DRAWING_NS from ..drawing_all import drawing_all_kind for node in self._walk_ast(self.ctx.ast): - if drawing_all_kind(node) in DRAWING_NS: + if drawing_all_kind(node) in kinds: + return True + hint = getattr(node, "type_hint", None) + if hint and self._spec_mentions_drawing( + self._type_spec_from_hint_name(hint), kinds, + ): return True + for hint in ((getattr(node, "annotations", None) or {}).get( + "param_type_hints", + ) or ()): + if hint and self._spec_mentions_drawing( + self._type_spec_from_hint_name(hint), kinds, + ): + return True if isinstance(node, FuncCall): + for hint in self._template_args_from_call(node): + if self._spec_mentions_drawing( + self._type_spec_from_hint_name(hint), kinds, + ): + return True _fn, ns = self._resolve_callee(node.callee) - if ns in DRAWING_NS: + if ns in kinds: + return True + if (isinstance(node.callee, Identifier) + and node.callee.name in kinds): return True - if self._is_chart_point_callee(node.callee): + if ("chart.point" in kinds + and self._is_chart_point_callee(node.callee)): return True return False diff --git a/pineforge_codegen/codegen/emit_top.py b/pineforge_codegen/codegen/emit_top.py index d9c67b33..675ff411 100644 --- a/pineforge_codegen/codegen/emit_top.py +++ b/pineforge_codegen/codegen/emit_top.py @@ -87,6 +87,7 @@ import re +from ..signatures import STRATEGY_SIZING_CONSTANTS from .run_stops import RUN_STOP_SHIMS_CPP, TIMEZONE_STOP_SHIM_CPP from ..ast_nodes import ( @@ -107,7 +108,7 @@ RUNTIME_REGISTER_SECURITY_LOWER_TF_EVAL_FN, ) from ..limits import iter_ast_nodes -from .drawing import DRAWING_LIFETIME_CPP +from .drawing import DRAWING_LIFETIME_CPP, POLYLINE_HANDLE_CPP from .tv_number_format import TV_NUMBER_FORMAT_CPP from .tv_string_units import TV_STRING_UNITS_CPP, TV_STRING_UNITS_MARKER @@ -265,6 +266,8 @@ def _emit_includes(self, lines: list[str]) -> None: self._emit_ta_compat_shims(lines) if getattr(self, "_uses_drawing", False): lines.append(DRAWING_LIFETIME_CPP) + if self._uses_polyline: + lines.append(POLYLINE_HANDLE_CPP) if self._uses_tv_number_format: lines.append(TV_NUMBER_FORMAT_CPP) lines.append("") @@ -1353,9 +1356,8 @@ def declared_or_v6_default(key: str): # default_qty_type: strategy.fixed / strategy.percent_of_equity / strategy.cash qty_type_map = { - "strategy.fixed": "QtyType::FIXED", - "strategy.percent_of_equity": "QtyType::PERCENT_OF_EQUITY", - "strategy.cash": "QtyType::CASH", + name: f"QtyType::{item.host_name}" + for name, item in STRATEGY_SIZING_CONSTANTS.items() } qty_type = declared_or_v6_default("default_qty_type") if qty_type in qty_type_map: @@ -1446,9 +1448,13 @@ def declared_or_v6_default(key: str): lines.append(' } else if (key == "close_entries_rule") {') lines.append(' overrides.close_entries_rule = (value == "ANY" || value == "any" || value == "1");') lines.append(' } else if (key == "default_qty_type") {') - lines.append(' if (value == "fixed" || value == "strategy.fixed" || value == "0") overrides.default_qty_type = static_cast(QtyType::FIXED);') - lines.append(' else if (value == "percent_of_equity" || value == "strategy.percent_of_equity" || value == "1") overrides.default_qty_type = static_cast(QtyType::PERCENT_OF_EQUITY);') - lines.append(' else if (value == "cash" || value == "strategy.cash" || value == "2") overrides.default_qty_type = static_cast(QtyType::CASH);') + for index, (name, item) in enumerate(STRATEGY_SIZING_CONSTANTS.items()): + branch = "if" if index == 0 else "else if" + lines.append( + f' {branch} (value == "{item.value}" || value == "{name}" ' + f'|| value == "{item.host_value}") overrides.default_qty_type = ' + f'static_cast(QtyType::{item.host_name});' + ) lines.append(" else return;") lines.append(' } else if (key == "commission_type") {') lines.append(' if (value == "percent" || value == "strategy.commission.percent" || value == "0") overrides.commission_type = static_cast(CommissionType::PERCENT);') @@ -2358,10 +2364,19 @@ def _emit_udt_method_cpp_name(self, fi: FuncInfo) -> str: allocated = {} for key, name in methods: encoded = cpp_identifier_fragment(name) - allocated[key] = ( - name if encoded == name else - self._allocate_generated_cpp_name(encoded, used) - ) + if encoded == name and name in self._all_member_names: + # Let the function channel separate an authored value + # binding from its established typed-method spelling. + allocated[key] = name + else: + preferred = ( + name if encoded == name else + self._allocate_generated_cpp_name(encoded, used) + ) + allocated[key] = self._generated_class_name( + ("typed-method", key), preferred + ) + used.add(allocated[key]) self._escaped_method_cpp_names = allocated return self._func_safe_name(allocated[fi.name]) @@ -2522,6 +2537,7 @@ def _emit_func_def_body(self, fi: FuncInfo, lines: list[str], or ( recv_spec.kind == "udt" and recv_spec.name not in self._udt_defs + and recv_spec.name not in DRAWING_TYPE_TO_CPP ) ): cpp_t = f"{cpp_t}&" @@ -2537,7 +2553,8 @@ def _emit_func_def_body(self, fi: FuncInfo, lines: list[str], ) cpp_t = ( recv_cpp - if fi.udt_type_name in self._udt_defs + if (fi.udt_type_name in self._udt_defs + or fi.udt_type_name in DRAWING_TYPE_TO_CPP) else f"{recv_cpp}&" ) safe_p = self._safe_name(p) @@ -2596,7 +2613,8 @@ def _emit_func_def_body(self, fi: FuncInfo, lines: list[str], if spec.kind == "udt": self._udt_param_udt[p] = spec.name self._udt_param_udt[self._safe_name(p)] = spec.name - if spec.name not in self._udt_defs: + if (spec.name not in self._udt_defs + and spec.name not in DRAWING_TYPE_TO_CPP): cpp_t = f"{cpp_t}&" elif spec.kind in ("array", "map"): elem = spec.element if spec.kind == "array" else spec.value @@ -2703,7 +2721,7 @@ def _emit_func_def_body(self, fi: FuncInfo, lines: list[str], self._active_call_site_idx = None self._current_instance_name = instance["name"] elif call_site_idx is not None: - func_name = f"{func_name}_cs{call_site_idx}" + func_name = self._func_variant_name(fi.name, call_site_idx) remap = self._func_cs_ta_remap.get((fi.name, call_site_idx), {}) self._active_ta_remap = remap var_remap = self._func_cs_var_remap.get((fi.name, call_site_idx), {}) diff --git a/pineforge_codegen/codegen/helpers.py b/pineforge_codegen/codegen/helpers.py index d0d666cc..15996b45 100644 --- a/pineforge_codegen/codegen/helpers.py +++ b/pineforge_codegen/codegen/helpers.py @@ -464,6 +464,7 @@ def cpp_code_only(cpp: str) -> str: _security_helper_series_ _security_helper_series_str_ _use_precalc _var_initialized _ta_initialized_ _inputs_initialized_ _pf_lines_ _pf_boxes_ _pf_labels_ _pf_linefills_ + _PFPolyline _pf_polyline_next_id_ _pf_script_state_checkpoint_ _PFScriptState _PFForeignEmaSeeding _PFALMA _PFKC _PFKCW _PFAnchoredVWAP _PFAnchoredVWAPBands _PFPivotPointLevels _PFArrayField _PFTvFormatValue @@ -533,6 +534,7 @@ def cpp_code_only(cpp: str) -> str: _pf_series_raw _pf_raw _pf_val_t _pf_out _pf_prior _pf_hist_offset_raw _pf_hist_offset_numeric _pf_hist_offset _pf_hist_bar _pf_calendar_ts _pf_calendar_tz + _pf_timeframe_arg _pf_qty_type _tz _yr _mo _dy _hr _min _sc _res _old _old_tz _had_old _secs _last_tz _last_yr _last_mo _last_dy _last_hr _last_min _last_sc _last_res @@ -542,7 +544,7 @@ def cpp_code_only(cpp: str) -> str: _pf_number_text _pf_substring_text _pf_substring_begin _pf_substring_end _pf_substring_error _pf_array_new_size _pf_array_new_value _pf_array_new_size_type _pf_array_new_error - _pf_read _pf_v5_l _pf_v5_r _pf_wide_l _pf_wide_r + _pf_read _pf_v5_l _pf_v5_r _pf_wide_l _pf_wide_r _pf_w _pf_symbol _pf_colon _pf_color _pf_bar_back _pf_ema_seeding _pf_history_array _pf_history_offset _pf_matrix_target _pf_array_index_type @@ -550,7 +552,8 @@ def cpp_code_only(cpp: str) -> str: _pf_sort_left _pf_sort_right _pf_sort_left_na _pf_sort_right_na """.split()) # These numbered members and locals keep their source-order spelling rather -# than allocating against authored names. Reserve their complete spellings, +# than allocating against authored names. Double coercions append underscores +# to _pf_w to distinguish nested temporaries. Reserve their complete spellings, # just as for fixed temporaries, without reserving ordinary underscore names. _CPP_TEMPORARY_PATTERN = re.compile( r"_v\d+|_secval_\d+(?:_v\d+)?|_tuple_result_\d+(?:_elem_\d+)?|_tuple_unused_\d+|__switch_val_\d+" @@ -560,6 +563,7 @@ def cpp_code_only(cpp: str) -> str: r"|_pf_every_bar_ta_\d+|_pf_shared_\d+_\d+|__pf_array\w*|__pf_raw_\w+" r"|_pf_collection_hist_\w+" r"|_pf_substring_arg\d+" + r"|_pf_w_+" r"|_req_sec_(?:lower_tf_)?\d+(?:_\d+)?|_precalc_\w+|_sec\d+_\w+" r"|_pf_(?:sec|rec)_missing_\d+|_eval_security_\d+" r"|_RuntimeSec\d+|_runtime_(?:feed|request)_\d+" @@ -717,6 +721,14 @@ def _initialise_safe_names(self, ast) -> None: ctx = getattr(self, "ctx", None) if "time_close" in getattr(ctx, "series_vars", ()): bound.add("time_close") + # Sibling-block and owner-qualified persistent identities need not + # occur in the syntax. Reserve them before escaping any name, and let + # their declarations and consumers use the same safe-name mapping. + storage_names = { + name for name, _, _ in getattr(ctx, "var_members", ()) + } + bound.update(storage_names) + authored.update(storage_names) self._safe_name_map: dict[str, str] = {} self._safe_name_occupied = authored self._safe_name_bound = bound @@ -750,6 +762,43 @@ def _initialise_safe_names(self, ast) -> None: self._local_function_names[name] = self._safe_name(name) self._allocate_safe_name(name) + # Analyzer-qualified storage (including sibling-block members) and + # TA/fixnan sites can have names absent from the authored AST. Clones + # and generated methods share their C++ class namespace. Reserve exact + # identities, rather than treating every authored _csN suffix as private. + self._safe_name_occupied.update( + self._safe_name(name) for name in member_names + ) + self._safe_name_occupied.update( + self._safe_name(name) + for name, _ptype in getattr(ctx, "global_var_decls", ()) + ) + for sites in ( + getattr(ctx, "ta_call_sites", ()), + getattr(ctx, "fixnan_sites", ()), + ): + self._safe_name_occupied.update(site.member_name for site in sites) + self._generated_class_names: dict[tuple, str] = {} + self._func_variant_callsite: dict[tuple[str, str], int] = {} + + def _generated_class_name(self, key: tuple, preferred: str) -> str: + """Allocate a composed Pine identity outside fixed helper namespaces.""" + if (preferred in CPP_RESERVED or preferred in BUILTIN_ACCESSOR_NAMES + or is_emitter_temporary(preferred)): + # Suffixing a reserved family such as _secN_* never escapes it. + # Change the leading token before checking exact occupied names. + preferred = f"pf_safe_{preferred}" + return self._generated_helper_name(key, preferred) + + def _generated_helper_name(self, key: tuple, preferred: str) -> str: + """Allocate one stable identity, including a helper's own namespace.""" + allocated = self._generated_class_names + if key not in allocated: + allocated[key] = self._allocate_generated_cpp_name( + preferred, self._safe_name_occupied + ) + return allocated[key] + def _allocate_safe_name(self, name: str) -> str: if name in LEGACY_CPP_RESERVED or name in BUILTIN_ACCESSOR_NAMES: base = f"_{name}_" @@ -784,12 +833,14 @@ def _safe_name(self, name: str) -> str: return name def _func_safe_name(self, name: str) -> str: - """Prefix function names that collide with class members (series vars or var members).""" + """Allocate function names that collide with class data members.""" safe = getattr(self, "_local_function_names", {}).get( name, self._safe_name(name), ) if safe in self._all_member_names: - return f"_fn_{safe}" + return self._generated_class_name( + ("function", name), f"_fn_{safe}" + ) return safe def _resolve_callee(self, callee) -> tuple[str | None, str | None]: diff --git a/pineforge_codegen/codegen/order_shapes.py b/pineforge_codegen/codegen/order_shapes.py index 86d426c8..f8230fc3 100644 --- a/pineforge_codegen/codegen/order_shapes.py +++ b/pineforge_codegen/codegen/order_shapes.py @@ -12,6 +12,7 @@ TupleAssign, UnaryOp, VarDecl, WhileStmt, ) from ..limits import iter_ast_nodes, syntax_children +from ..signatures import STRATEGY_SIZING_CONSTANTS from .capabilities import READ_ONLY_STRATEGY_CALLS, _expression from .host_members import HOST_MEMBER_NAMES @@ -23,7 +24,7 @@ FINITE_BAR_VALUES = frozenset(("open", "high", "low", "close", "hl2", "hlc3", "ohlc4", "hlcc4")) ENUM_VALUES = { "strategy.oca.none": 0, "strategy.oca.cancel": 1, "strategy.oca.reduce": 2, - "strategy.fixed": 0, "strategy.percent_of_equity": 1, "strategy.cash": 2, + **{name: item.host_value for name, item in STRATEGY_SIZING_CONSTANTS.items()}, } SETTING_DEFAULTS = { "initial_capital": 10000.0, "default_qty_type": "fixed", "default_qty_value": 1.0, diff --git a/pineforge_codegen/codegen/strategy_declaration.py b/pineforge_codegen/codegen/strategy_declaration.py index 5efe9c8f..a0731458 100644 --- a/pineforge_codegen/codegen/strategy_declaration.py +++ b/pineforge_codegen/codegen/strategy_declaration.py @@ -5,7 +5,7 @@ from ..ast_nodes import EnumDecl, FuncDef, Identifier, StrategyDecl, StringLiteral, TypeDecl, VarDecl from ..errors import CompileError, Diagnostic, Level, Phase, SourceLocation from ..limits import iter_ast_nodes -from ..signatures import MATH_CONSTANTS, STRATEGY_PARAMETERS +from ..signatures import MATH_CONSTANTS, STRATEGY_PARAMETERS, STRATEGY_SIZING_CONSTANTS from .constant_fold import NON_CONSTANT, NamedConstant, fold_constant_expression from .tables import DAYOFWEEK_MAP, NAME_ECHO_STRING_MEMBERS @@ -16,12 +16,16 @@ _NAMED_CONSTANTS = { f"strategy.{name}": NamedConstant(f"strategy.{name}", name.rsplit(".", 1)[-1]) for name in ( - "fixed", "cash", "percent_of_equity", "commission.percent", + "commission.percent", "commission.cash_per_order", "commission.cash_per_contract", "long", "short", "oca.none", "oca.cancel", "oca.reduce", "direction.all", "direction.long", "direction.short", ) } +_NAMED_CONSTANTS.update({ + name: NamedConstant(name, item.value) + for name, item in STRATEGY_SIZING_CONSTANTS.items() +}) _NAMED_CONSTANTS.update({ f"{namespace}.{member}": NamedConstant(f"{namespace}.{member}", member) for namespace in ("currency", "format") diff --git a/pineforge_codegen/codegen/ta.py b/pineforge_codegen/codegen/ta.py index 765796f6..29b2985a 100644 --- a/pineforge_codegen/codegen/ta.py +++ b/pineforge_codegen/codegen/ta.py @@ -547,7 +547,7 @@ def allocate(base: str, reserved: set[str]) -> str: reserved_types.add(emitted) total = self.ctx.func_call_site_counts.get(info.name, 0) for callsite in range(total): - reserved_members.add(f"{emitted}_cs{callsite}") + reserved_members.add(self._func_variant_name(info.name, callsite)) for instance in getattr(self, "_fresh_instances", ()): reserved_members.add(instance["name"]) self._lazy_source_clock_type_name = allocate("_PFLazySourceClock", reserved_types) diff --git a/pineforge_codegen/codegen/tables.py b/pineforge_codegen/codegen/tables.py index deae2117..baa0914a 100644 --- a/pineforge_codegen/codegen/tables.py +++ b/pineforge_codegen/codegen/tables.py @@ -480,12 +480,13 @@ def tz_time_field_lambda(field_expr: str, ts_arg: str, tz_arg: str) -> str: # --------------------------------------------------------------------------- # Drawing-objects-as-data (spec §4.1) # --------------------------------------------------------------------------- -# Pine drawing type name -> C++ handle struct (pineforge/drawing.hpp). -# ``table`` / ``polyline`` are DELIBERATELY absent — they stay 100% no-op -# (kept in SKIP_NAMESPACES / SKIP_VAR_TYPES / _DRAWING_TYPES_INIT). +# Pine drawing type name -> C++ value type. +# Polyline values have an opaque generated handle; their geometry remains a +# visual no-op. Only the engine-backed kinds below have drawing arenas. DRAWING_TYPE_TO_CPP = { "line": "Line", "box": "Box", "label": "Label", "linefill": "Linefill", "chart.point": "ChartPoint", + "polyline": "_PFPolyline", } # Function-call namespaces routed to the per-type arena dispatch. DRAWING_NS = {"line", "box", "label", "linefill"} @@ -526,7 +527,8 @@ def tz_time_field_lambda(field_expr: str, ts_arg: str, tz_arg: str) -> str: } # line / box / label / linefill REMOVED — they now route to real drawing # codegen (gated on _uses_drawing). table / polyline / chart / display / size -# / position stay no-op. +# / position stay visual-only. Polyline creation/deletion is intercepted before +# the generic skip to retain its typed handle and argument evaluations. SKIP_NAMESPACES = { "table", "polyline", "chart", "display", "size", "position", diff --git a/pineforge_codegen/codegen/types.py b/pineforge_codegen/codegen/types.py index abc3fb2b..62f35edd 100644 --- a/pineforge_codegen/codegen/types.py +++ b/pineforge_codegen/codegen/types.py @@ -53,6 +53,7 @@ from ..limits import iter_ast_nodes from ..loop_types import FLOAT_REASSIGNMENT, LoopTypeSources, for_counter_type from ..matrix_overloads import matrix_sum_has_rhs +from ..reference_casts import na_reference_cast_kind from ..symbols import ( PineType, TypeSpec, @@ -3061,6 +3062,8 @@ def _type_spec_from_expr_raw( plan = getattr(self, "_array_widths", None) return (plan.widen(spec, {("field", owner.name, node.member)}) if plan is not None else spec) + if self._builtin_sizing_constant(node) is not None: + return TypeSpec.primitive("string") return None if isinstance(node, FuncCall): func_name, namespace = self._resolve_callee(node.callee) @@ -3143,6 +3146,11 @@ def _type_spec_from_expr_raw( targs = self._template_args_from_call(node) # Drawing-objects-as-data return typing (spec §4.5 DRAWING_RETURN_SPECS): # *.new / *.copy -> handle of the self-type; linefill.get_line* -> line. + if (func_name not in getattr(self, "_func_info_map", {}) + and (cast_kind := na_reference_cast_kind(node))): + return TypeSpec.udt(cast_kind) + if namespace == "polyline" and func_name == "new": + return TypeSpec.udt("polyline") if namespace in DRAWING_NS: if func_name in ("new", "copy"): return TypeSpec.udt(namespace) @@ -5306,6 +5314,12 @@ def _slot_scalar_cpp_type(self, name: str) -> str | None: primitive_global = self._primitive_block_shadow_globals.get(name) if primitive_global is not None: return self._type_for_decl(primitive_global) + drawing_cpp_type = self._drawing_target_cpp_type(name, None) + if drawing_cpp_type is not None: + # The coarse PineType of an ID is not numeric storage. In + # particular a tuple's drawing value must never enter a double + # conversion merely because the destination is a var member. + return drawing_cpp_type for vname, ptype, _init in self.ctx.var_members: if vname == name: if self._numeric_na_member(name): @@ -6138,6 +6152,26 @@ def _infer_cpp_type_for_security_elem(self, node) -> str: return "std::vector" return "double" + def _builtin_sizing_constant(self, node): + """Resolve registered constants only through an unshadowed namespace.""" + if not (isinstance(node, MemberAccess) + and isinstance(node.object, Identifier)): + return None + receiver = node.object + name = receiver.name + constant = sigs.get_sizing_constant(name, node.member) + if constant is None: + return None + if (self.ctx.identifier_decl_bindings.get(id(receiver)) is not None + or ValueBindings.declaration_of(receiver) is not None + or name in self._current_func_param_types + or name in self._current_func_locals + or name in self._current_loop_vars + or name in self._udt_defs + or name in self._enum_defs): + return None + return constant + def _text_type(self, node) -> str: """Pine bool provenance is consumed only by explicit text conversion.""" if not self._pine_v5_body: @@ -6340,6 +6374,11 @@ def _infer_type( return "double" if isinstance(node, FuncCall): func_name, namespace = self._resolve_callee(node.callee) + if (func_name not in getattr(self, "_func_info_map", {}) + and (cast_kind := na_reference_cast_kind(node))): + return DRAWING_TYPE_TO_CPP[cast_kind] + if namespace == "polyline" and func_name == "new": + return DRAWING_TYPE_TO_CPP["polyline"] if namespace == "color": return "int64_t" if func_name in {"new", "rgb", "from_gradient"} else "int" if (namespace == "request" and func_name == "security" diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index efb19c5c..edebfd5f 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -157,8 +157,16 @@ from ..external_requests import RECORDED_KEY_ANNOTATION, request_stop_marker from ..intrinsic_binding import bind_intrinsic_arguments, validate_intrinsic_keywords from ..limits import iter_ast_nodes +from ..reference_casts import na_reference_cast_kind from ..security_contexts import ticker_symbol_arg -from ..symbols import PineType, TypeSpec, method_receiver_type_name +from ..symbols import ( + PineType, + TypeSpec, + method_receiver_type_name, + public_cpp_type_name, + public_type_name, +) +from ..analyzer.value_bindings import ValueBindings from ..method_binding import ( MethodBindError, bind_function_defaults, @@ -428,12 +436,12 @@ def _udt_method_call_emit_name(self, fi, node: FuncCall) -> str: cs_info = self.ctx.func_call_cs_map.get(id(node)) if self._active_call_site_idx is not None and cs_info is not None: - return f"{base}_cs{self._active_call_site_idx}" + return self._func_variant_name(fi.name, self._active_call_site_idx) if cs_info is not None and cs_info[0] == fi.name: - return f"{base}_cs{cs_info[1]}" + return self._func_variant_name(fi.name, cs_info[1]) if (self._active_call_site_idx is not None and self.ctx.func_call_site_counts.get(fi.name, 0) > 1): - return f"{base}_cs{self._active_call_site_idx}" + return self._func_variant_name(fi.name, self._active_call_site_idx) return base def _typed_user_method_info(self, receiver, member: str): @@ -2573,6 +2581,8 @@ def _visit_func_call(self, node: FuncCall) -> str: return self._emit_drawing_namespace_call(namespace, func_name, node) if self._is_chart_point_callee(callee): return self._emit_chart_point(func_name, node) + if namespace == "polyline" and func_name in {"new", "delete"}: + return self._emit_polyline_call(func_name, node) # Skip visual/unsupported namespace calls if namespace in SKIP_NAMESPACES or namespace in SKIP_VAR_TYPES: @@ -2990,10 +3000,8 @@ def _visit_func_call(self, node: FuncCall) -> str: ) # Drawing casts of na use the same missing handle as a typed binding. - if (namespace is None and func_name in ("line", "box") - and len(node.args) == 1 and not node.kwargs - and self._is_na_expr(node.args[0])): - return f"{DRAWING_TYPE_TO_CPP[func_name]}{{}}" + if cast_kind := na_reference_cast_kind(node): + return f"{DRAWING_TYPE_TO_CPP[cast_kind]}{{}}" # Type cast functions: int(x), float(x), bool(x), string(x) if func_name == "int" and namespace is None and node.args: @@ -3211,22 +3219,84 @@ def _visit_func_call(self, node: FuncCall) -> str: return f'{log_funcs[func_name]}({self._visit_expr(message)})' return '"" /* unsupported log */' - # timeframe.* calls (e.g., timeframe.change) — not supported in single-TF backtest + # An empty timeframe argument denotes the chart period, whether it + # comes from a literal, an input or a user call. Bind by parameter + # name and resolve the evaluated string, not the argument's AST form. if namespace == "timeframe": - if func_name == "change": - tf_arg = self._visit_expr(node.args[0]) if node.args else 'script_tf_' - # Symbol-clock overload: D/W/M boundaries fall on the SYMBOL's - # session day (17:00 ET on OANDA forex, 09:30 ET RTH on - # equities), intraday buckets on the session-open grid — the - # same clock request.security aggregation uses. UTC/24x7 - # syminfo reduces to the tz-less epoch math bit-for-bit. + if func_name in ("change", "in_seconds"): + argument = self._resolve_func_args( + node, f"timeframe.{func_name}", + ).get("timeframe") + + def timeframe_call(tf_arg: str) -> str: + if func_name == "change": + # Keep the symbol-clock overload for calendar and + # session-anchored intraday boundaries. + return ( + f'tf_change(prev_bar_timestamp_, current_bar_.timestamp, {tf_arg}, ' + 'syminfo_.timezone, syminfo_.session)' + ) + return f'tf_to_seconds({tf_arg})' + + if argument is None: + return timeframe_call("script_tf_") + actual = self._type_spec_from_expr(argument) + if (actual is None and isinstance(argument, MemberAccess) + and isinstance(argument.object, Identifier) + and self.ctx.identifier_decl_bindings.get(id(argument.object)) is None + and ValueBindings.declaration_of(argument.object) is None): + variable_spec = sigs.get_variable_type_spec( + argument.object.name, argument.member, + ) + if variable_spec is not None: + actual = variable_spec + actual_cpp_type = self._text_type(argument) + argument_is_string = ( + actual == TypeSpec.primitive("string") + or (actual is None and actual_cpp_type == "std::string") + ) + if (not isinstance(argument, NaLiteral) + and (not argument_is_string or actual_cpp_type == "bool")): + # A call-site variant may resolve an untyped helper after + # analysis. Check before bool-to-text compatibility can + # turn a non-string value into a C++ string. + argument_type = ( + public_type_name(actual) + if actual is not None + else public_cpp_type_name(actual_cpp_type) + ) + intrinsic = sigs.BUILTIN_FUNCTIONS[f"{namespace}.{func_name}"] + parameter = intrinsic.primary.params[0] + function_name = intrinsic.name + parameter_type = parameter.pine_type.value + name = parameter.name + from ..pine_spelling import expr_start + + self._codegen_error( + expr_start(argument), + f"Cannot call '{function_name}' with an argument of type {argument_type} " + f"for {parameter_type} parameter '{name}'.", + ) + argument_cpp = self._visit_rhs_value( + argument, target_cpp_type="std::string", + ) + if cpp_is_plain_read(argument_cpp): + # Keep resolved locals and parameters as direct engine + # arguments when nonempty. Stable reads can be checked + # without introducing another binding. + return ( + f'(({argument_cpp}).empty() ? {timeframe_call("script_tf_")} : ' + f'{timeframe_call(argument_cpp)})' + ) + # A function-return argument can carry state. Evaluate it + # once before testing emptiness and passing the chosen value. + call = timeframe_call( + "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" + ) return ( - f'tf_change(prev_bar_timestamp_, current_bar_.timestamp, {tf_arg}, ' - 'syminfo_.timezone, syminfo_.session)' + '([&]() { const std::string _pf_timeframe_arg = ' + f'({argument_cpp}); return {call}; }}())' ) - if func_name == "in_seconds": - tf_arg = self._visit_expr(node.args[0]) if node.args else 'script_tf_' - return f'tf_to_seconds({tf_arg})' # Defensive: support_checker.NOT_YET_FUNC should already have rejected # any unhandled timeframe.* call. Reaching here implies the checker was # bypassed. @@ -3277,20 +3347,7 @@ def _visit_func_call(self, node: FuncCall) -> str: try: val = self._visit_rhs_value( value_node, - target_cpp_type=( - f_cpp_type - if (self._is_nullable_collection_cpp_type(f_cpp_type) - or (f_cpp_type == "std::vector" - and ("field", namespace, f.name) - in self._array_widths.wide) - or f_cpp_type in self._udt_defs - or f_cpp_type in DRAWING_TYPE_TO_CPP.values() - # A bare na for a string field is the typed - # string na, not the numeric ``na()``. - or (f_cpp_type == "std::string" - and self._is_na_expr(value_node))) - else None - ), + target_cpp_type=f_cpp_type, ) finally: self._array_ctor_expected = previous_expected @@ -3612,16 +3669,16 @@ def _visit_arg_for_series(arg_node, arg_idx): # the parent's active call-site index. This ensures sub-functions # called from ma_cs6() use their _cs6 variant, not _cs0. fname, _ = cs_info - emit_name = f"{self._func_safe_name(fname)}_cs{self._active_call_site_idx}" + emit_name = self._func_variant_name(fname, self._active_call_site_idx) elif cs_info is not None: fname, cs_idx = cs_info - emit_name = f"{self._func_safe_name(fname)}_cs{cs_idx}" + emit_name = self._func_variant_name(fname, cs_idx) elif (self._active_call_site_idx is not None and func_name in self._func_names and self.ctx.func_call_site_counts.get(func_name, 0) > 1): # Inside a per-call-site variant: propagate call-site index to # sub-functions that also have variants (for state isolation) - emit_name = f"{self._func_safe_name(func_name)}_cs{self._active_call_site_idx}" + emit_name = self._func_variant_name(func_name, self._active_call_site_idx) call_head = emit_name if namespace is None and func_name in self._func_names: return self._ordered_user_call_expr( @@ -3754,6 +3811,34 @@ def _strategy_string_arg(self, node) -> str: """A missing Pine string uses the host's empty-string sentinel.""" return '""' if node is None or isinstance(node, NaLiteral) else self._visit_expr(node) + def _strategy_qty_type_arg(self, node) -> str: + """Convert a Pine sizing string only where the host expects its code.""" + if node is None: + return "-1" + constant = self._builtin_sizing_constant(node) + if constant is not None: + return str(constant.host_value) + if isinstance(node, Ternary): + return ( + f"(({self._visit_expr(node.condition)}) ? " + f"({self._strategy_qty_type_arg(node.true_val)}) : " + f"({self._strategy_qty_type_arg(node.false_val)}))" + ) + value = self._visit_expr(node) + if self._infer_type(node) != "std::string": + # Retain the legacy numeric host API, including its -1 sentinel. + return value + cases = " ".join( + f"if (_pf_qty_type == {self._visit_expr(StringLiteral(value=item.value))}) " + f"return {item.host_value};" + for item in sigs.STRATEGY_SIZING_CONSTANTS.values() + ) + return ( + "([&]() -> int { const std::string _pf_qty_type = " + f"({value}); {cases} " + 'throw std::invalid_argument("Unknown strategy sizing value"); }())' + ) + def _visit_strategy_call(self, func_name: str, node: FuncCall) -> str: from .order_shapes import record_order_call if func_name in ("convert_to_account", "convert_to_symbol"): @@ -3789,7 +3874,7 @@ def _visit_strategy_call(self, func_name: str, node: FuncCall) -> str: comment_val = self._strategy_string_arg(comment) oca_name_val = self._strategy_string_arg(oca_name) oca_type_val = self._visit_expr(oca_type) if oca_type else "0" - qty_type_val = self._visit_expr(qty_type) if qty_type else "-1" + qty_type_val = self._strategy_qty_type_arg(qty_type) qty_val = self._visit_float_arg(qty) if stop is not None or limit is not None or qty is not None or oca_name is not None or oca_type is not None or qty_type is not None: limit_val = self._visit_float_arg(limit) @@ -4405,18 +4490,20 @@ def _resolve_func_args(self, node: FuncCall, sig_key: str) -> dict: Uses the PineScript parameter ordering from signatures registry. """ - # sig_key is like "strategy.entry" -> namespace="strategy", func_name="entry" + # Namespace records use their short name; dotted builtin records use + # the complete key in BUILTIN_FUNCTIONS (e.g. timeframe.change). parts = sig_key.split(".", 1) + intrinsic = ( + sigs.get_intrinsic(parts[0], parts[1]) if len(parts) == 2 else None + ) + if intrinsic is None: + intrinsic = sigs.get_intrinsic(None, sig_key) validate_intrinsic_keywords( - sigs.get_intrinsic(parts[0], parts[1]) if len(parts) == 2 - else sigs.get_intrinsic(None, sig_key), + intrinsic, node.kwargs, pine_version=self.ctx.ast.version, ) - if len(parts) == 2: - param_names = sigs.get_param_names(parts[0], parts[1]) or [] - else: - param_names = sigs.get_param_names(None, sig_key) or [] + param_names = intrinsic.param_names if intrinsic is not None else [] result: dict = {} # Map positional args by parameter name for i, arg in enumerate(node.args): diff --git a/pineforge_codegen/codegen/visit_expr.py b/pineforge_codegen/codegen/visit_expr.py index da1f99a1..194f0cda 100644 --- a/pineforge_codegen/codegen/visit_expr.py +++ b/pineforge_codegen/codegen/visit_expr.py @@ -506,6 +506,8 @@ def _drawing_target_cpp_type( self, target_name: str | None, target_cpp_type: str | None, + *, + target_node=None, ) -> str | None: """Resolve a drawing target without leaking a shadowed global type. @@ -520,6 +522,11 @@ def _drawing_target_cpp_type( if target_cpp_type in DRAWING_TYPE_TO_CPP.values() else None ) + if target_node is not None: + spec = self._type_spec_from_expr(target_node) + if (spec is not None and spec.kind == "udt" + and spec.name in DRAWING_TYPE_TO_CPP): + return DRAWING_TYPE_TO_CPP[spec.name] if not target_name: return None if target_name in self._lexical_drawing_types: @@ -547,7 +554,7 @@ def _drawing_target_cpp_type( # later local declaration pre-shadow earlier statements. Exact # top-level types are captured separately because the analyzer's flat # raw-name registry can be overwritten by a same-named local. - return self._global_drawing_cpp_types.get(target_name) + return getattr(self, "_global_drawing_cpp_types", {}).get(target_name) def _udt_target_cpp_type( self, @@ -997,6 +1004,9 @@ def _visit_member_access(self, node: MemberAccess) -> str: # call reaches the array it holds. return f"(*{field})" return field + sizing = self._builtin_sizing_constant(node) + if sizing is not None: + return self._visit_expr(StringLiteral(value=sizing.value)) if isinstance(node.object, Identifier): ns = node.object.name if ns == "alert" and node.member in ALERT_FREQ_VALUES: @@ -1085,13 +1095,6 @@ def _visit_member_access(self, node: MemberAccess) -> str: return "avg_losing_trade_percent()" if node.member == "margin_liquidation_price": return "margin_liquidation_price()" - # Qty type / commission constants - if node.member == "fixed": - return "0" - if node.member == "percent_of_equity": - return "1" - if node.member == "cash": - return "2" if node.member == "account_currency": return self._strategy_account_currency_expr() # Sub-namespaces (oca, direction, risk, commission) handled below diff --git a/pineforge_codegen/codegen/visit_stmt.py b/pineforge_codegen/codegen/visit_stmt.py index ffdb77e0..e367346c 100644 --- a/pineforge_codegen/codegen/visit_stmt.py +++ b/pineforge_codegen/codegen/visit_stmt.py @@ -493,7 +493,7 @@ def _visit_stmt(self, node: ASTNode, lines: list[str], indent: int) -> None: if is_pct is None: # Mutable aliases and computed enums must use # the value at this call, not an initializer. - type_value = self._visit_expr(type_arg) + type_value = self._strategy_qty_type_arg(type_arg) percent_flag = ( f"(({type_value}) == " "static_cast(QtyType::PERCENT_OF_EQUITY))" @@ -1297,6 +1297,7 @@ def _visit_assignment(self, node: Assignment, lines: list[str], pad: str) -> Non selection_cpp_type = self._drawing_target_cpp_type( target_name, None, + target_node=node.target if target_name is None else None, ) if selection_cpp_type is None: selection_cpp_type = self._int_slot_cpp_type(target_name) @@ -1325,14 +1326,9 @@ def _visit_assignment(self, node: Assignment, lines: list[str], pad: str) -> Non # Get target name if target_name is None: - # Assignment to a UDT field that was dropped from the emitted - # struct because it had a drawing-only type (label/line/box/ - # linefill/polyline/table/chart.point). The struct has no such - # member, so emit a placeholder comment instead of a real C++ - # assignment. We intentionally do NOT visit the RHS here: drawing - # constructors (label.new / line.new / ...) live in - # SKIP_NAMESPACES, so they have no observable side effects in - # the backtest runtime. See: pineforge-codegen issue #10. + # Retain the existing omission of visual-only table fields. + # Drawing and opaque polyline handles are real record members and + # follow the typed assignment path below. if self._is_omitted_udt_field(node.target): recv = self._visit_expr(node.target.object) omitted = self._cpp_comment_escape(f"{recv}.{node.target.member} {node.op}") @@ -1347,6 +1343,14 @@ def _visit_assignment(self, node: Assignment, lines: list[str], pad: str) -> Non target_cpp_type = self._udt_target_cpp_type( target_node=node.target, ) + if target_cpp_type is None: + target_cpp_type = self._drawing_target_cpp_type( + None, None, target_node=node.target, + ) + if target_cpp_type is None: + field_spec = self._type_spec_from_expr(node.target) + if field_spec is not None and field_spec.kind == "array": + target_cpp_type = self._type_spec_to_cpp(field_spec) if (target_cpp_type is None and self._is_na_expr(node.value) and self._udt_field_is_string(node.target)): # A bare na assigned to a string field is the typed string na, diff --git a/pineforge_codegen/diagnostic_codes.py b/pineforge_codegen/diagnostic_codes.py index 4b8ca4c7..bcd6c7a8 100644 --- a/pineforge_codegen/diagnostic_codes.py +++ b/pineforge_codegen/diagnostic_codes.py @@ -14,6 +14,11 @@ never guessed: a text no template renders gets the uncatalogued code of its severity (``PF-E0000`` / ``PF-W0000``), which the test suite refuses. +For templates with the same amount of literal text, recognized Pine primitive +types in catalog-declared type arguments take precedence over open string +arguments. Remaining ties keep the stable code order; every candidate still +renders the same text. + The match never backtracks over the text's characters: each literal segment of a template goes to its leftmost place after the previous one, the last to the text's end, and every argument is the text between its literals -- the @@ -38,6 +43,8 @@ from pathlib import Path from typing import Any +from .symbols import PineType + CATALOG_PATH = Path(__file__).with_name("diagnostics_catalog.json") CATALOG_SCHEMA = "pineforge-diagnostics-catalog/v1" # Notes keep historical PF-W identities. The nonfatal fallback is shared; @@ -178,6 +185,7 @@ def render_diagnostic(code: str, args: dict) -> tuple[str, str | None]: _CANONICAL_INT = re.compile(r"-?(?:0|[1-9][0-9]*)") _CANONICAL_FLOAT = re.compile(r"-?(?:0|[1-9][0-9]*)\.[0-9]+") +_PINE_TYPE_NAMES = frozenset(pine_type.value for pine_type in PineType) def _segments(parts: list) -> tuple[str, tuple[tuple[str, str], ...]]: @@ -313,6 +321,15 @@ def match(self, message: str, hint: str | None) -> dict | None: args[name] = value return args + def type_specificity(self, args: dict) -> int: + """Count recognized primitive types in this template's type arguments. + + Other spellings stay raw: this only breaks a literal-specificity tie, + without rejecting identifiers, composite types or open diagnostic text. + """ + return sum(value in _PINE_TYPE_NAMES for name, value in args.items() + if self._kinds.get(name) == "type") + @lru_cache(maxsize=1) def _matchers() -> dict[str, list[_Matcher]]: @@ -322,8 +339,8 @@ def _matchers() -> dict[str, list[_Matcher]]: continue by_severity[entry["severity"]].append(_Matcher(code, entry)) for matchers in by_severity.values(): - # The template with the most literal text wins: a message two templates - # render reads as the more specific one (both render it byte for byte). + # Prefer literal text first. Argument types can resolve equal literal + # specificity in classify; remaining ties keep this stable code order. matchers.sort(key=lambda m: (-m.specificity, m.code)) return by_severity @@ -336,14 +353,25 @@ def classify(severity: str, message: str, hint: str | None = None) -> tuple[str, no template renders gets ``PF-E0000`` for an error or ``PF-W0000`` for a warning/note, with its text as ``args`` and no new fallback identity. """ + best: tuple[str, dict] | None = None + best_specificity = -1 + best_types = -1 for matcher in _matchers().get(severity, ()): + if best is not None and matcher.specificity < best_specificity: + break if matcher.has_hint != (hint is not None): continue if not message.startswith(matcher.prefix) or not message.endswith(matcher.suffix): continue args = matcher.match(message, hint) if args is not None: - return matcher.code, args + types = matcher.type_specificity(args) + if best is None or types > best_types: + best = (matcher.code, args) + best_specificity = matcher.specificity + best_types = types + if best is not None: + return best args = {"message": message} if hint is not None: args["hint"] = hint diff --git a/pineforge_codegen/diagnostics_catalog.json b/pineforge_codegen/diagnostics_catalog.json index eb4bc5ff..c6eef36f 100644 --- a/pineforge_codegen/diagnostics_catalog.json +++ b/pineforge_codegen/diagnostics_catalog.json @@ -235,6 +235,7 @@ "PF-E2090": {"severity": "error", "area": "analysis", "message": "input.time() needs a constant default in Pine v6: timestamp() with a time zone and calendar numbers is not a constant. Use the date-string form, for example timestamp(\"01 Jan 2023 00:00 +0000\").", "hint": "Use a single date string in timestamp or an integer timestamp literal for the default.", "explanation": "The time-zone overload of timestamp, which takes a zone and calendar numbers, returns a simple int, while a Pine v6 input.time default requires a const int.", "args": {}, "user_message": "input.time() requires a constant default in Pine v6."}, "PF-E2091": {"severity": "error", "area": "analysis", "message": "plot() cannot have a linewidth below 1 in Pine v6: use a value of 1 or greater.", "hint": "Set the plot linewidth to an integer of at least 1.", "explanation": "A Pine v6 plot call cannot use an integer literal below one for linewidth; variable and calculated widths retain their existing handling.", "args": {}, "user_message": "plot() requires a linewidth of at least 1 in Pine v6."}, "PF-E2092": {"severity": "error", "area": "analysis", "message": "\"range\" cannot be used as a variable or function name in Pine v6: use another name.", "hint": "Rename the variable, function or method declaration.", "explanation": "Pine v6 reserves range as a variable and callable declaration name; parameters, user type fields and member access retain their existing handling.", "args": {}, "user_message": "Choose another variable, function or method name instead of range in Pine v6."}, + "PF-E2095": {"severity": "error", "area": "analysis", "message": "Cannot call ''{function_name}'' with an argument of type {argument_type} for {parameter_type} parameter ''{name}''.", "hint": null, "explanation": "A non-string value cannot be used for a string parameter. Pass a string value of the declared parameter type; a permitted generic na adopts that type.", "args": {"function_name": {"kind": "identifier"}, "argument_type": {"kind": "type"}, "parameter_type": {"kind": "type"}, "name": {"kind": "identifier"}}, "user_message": "Argument ''{name}'' of ''{function_name}'' has type {argument_type}, but requires {parameter_type}."}, "PF-E3001": {"severity": "error", "area": "request-security", "message": "request.security timeframe ''{name}'' cannot be resolved before the first bar: PineForge registers every requested timeframe before the script runs.", "hint": "Pass a literal, an input or a global to the helper that holds request.security.", "explanation": "The timeframe given to request.security is a name whose value PineForge cannot compute before the first bar; use a literal, an input or a global.", "args": {"name": {"kind": "identifier"}}, "user_message": "The timeframe given to request.security is a name whose value PineForge cannot compute before the first bar."}, "PF-E3002": {"severity": "error", "area": "request-security", "message": "request.security timeframe expression cannot be resolved before the first bar: PineForge registers every requested timeframe before the script runs.", "hint": "Pass a literal, an input or a global to the helper that holds request.security.", "explanation": "The timeframe expression given to request.security cannot be computed before the first bar; use a literal, an input or a global.", "args": {}, "user_message": "The timeframe expression given to request.security cannot be computed before the first bar."}, "PF-E3003": {"severity": "error", "area": "request-security", "message": "request.security timeframe switch arms must contain one expression", "hint": "Compute multi-statement timeframe logic before the request.security call or rewrite it as pure switch arms.", "explanation": "A switch that picks a request.security timeframe must have exactly one expression in each arm; compute multi-statement logic beforehand.", "args": {}, "user_message": "A switch that picks a request.security timeframe must have exactly one expression in each arm."}, diff --git a/pineforge_codegen/reference_casts.py b/pineforge_codegen/reference_casts.py new file mode 100644 index 00000000..a9c44e97 --- /dev/null +++ b/pineforge_codegen/reference_casts.py @@ -0,0 +1,38 @@ +"""Shared argument binding for missing drawing-reference casts.""" + +from .ast_nodes import FuncCall, Identifier, NaLiteral +from .signatures import FuncSig, Param, merge_kwargs_to_positional +from .symbols import PineType + + +_REFERENCE_CAST_KINDS = frozenset({"line", "box", "label", "linefill", "polyline"}) +_REFERENCE_CAST_SIGNATURE = FuncSig( + params=[Param("x", PineType.NA)], return_type=PineType.UNKNOWN, +) + + +def na_reference_cast_kind(node) -> str | None: + """Return the reference kind for a valid ``kind(na)`` / ``kind(x=na)``. + + Bind the same one-slot signature in analysis, default initialization and + expression emission. Malformed or non-NA calls retain normal diagnostics. + """ + if (not isinstance(node, FuncCall) + or not isinstance(node.callee, Identifier) + or node.callee.name not in _REFERENCE_CAST_KINDS): + return None + names = [param.name for param in _REFERENCE_CAST_SIGNATURE.params] + if (len(node.args) > len(names) + or set(node.kwargs) - set(names) + or any(name in node.kwargs for name in names[:len(node.args)])): + return None + arguments = merge_kwargs_to_positional( + _REFERENCE_CAST_SIGNATURE, node.args, node.kwargs, + ) + if len(arguments) != len(names): + return None + value = arguments[0] + if (isinstance(value, NaLiteral) + or (isinstance(value, Identifier) and value.name == "na")): + return node.callee.name + return None diff --git a/pineforge_codegen/signatures.py b/pineforge_codegen/signatures.py index 6ab3522f..0f4483d5 100644 --- a/pineforge_codegen/signatures.py +++ b/pineforge_codegen/signatures.py @@ -14,7 +14,7 @@ from dataclasses import dataclass, field from typing import Any -from .symbols import PineType +from .symbols import PineType, TypeSpec @dataclass @@ -694,6 +694,39 @@ def get_value_function(name: str) -> IntrinsicFunc | None: # Built-in variables (not functions) # ============================================================================ +@dataclass(frozen=True) +class _StorageOnlyType: + """Legacy host representation without a proven scalar Pine type.""" + + storage_type: PineType + + +@dataclass(frozen=True) +class SizingConstant: + """A Pine string value and its conversion at a host sizing boundary.""" + + value: str + host_value: int + host_name: str + + +# The complete strategy sizing constant class. Its numeric codes belong only +# to the host API; an ordinary expression reads the string value. +STRATEGY_SIZING_CONSTANTS = { + "strategy.fixed": SizingConstant("fixed", 0, "FIXED"), + "strategy.percent_of_equity": SizingConstant("percent_of_equity", 1, "PERCENT_OF_EQUITY"), + "strategy.cash": SizingConstant("cash", 2, "CASH"), +} + + +def _variable_storage_type(value: PineType | _StorageOnlyType | SizingConstant) -> PineType: + if isinstance(value, _StorageOnlyType): + return value.storage_type + if isinstance(value, SizingConstant): + return PineType.STRING + return value + + BUILTIN_VARIABLES: dict[str, PineType] = { # Price series "open": F, "high": F, "low": F, "close": F, "volume": F, @@ -735,7 +768,7 @@ def get_value_function(name: str) -> IntrinsicFunc | None: ) # Strategy variables (read-only properties, not function calls) -STRATEGY_VARIABLES: dict[str, PineType] = { +_STRATEGY_VARIABLE_DECLARATIONS: dict[str, PineType | _StorageOnlyType | SizingConstant] = { "strategy.position_size": F, "strategy.position_avg_price": F, "strategy.position_entry_name": S, @@ -771,25 +804,27 @@ def get_value_function(name: str) -> IntrinsicFunc | None: "strategy.account_currency": S, "strategy.opentrades.capital_held": F, # Direction constants - "strategy.long": B, - "strategy.short": B, + "strategy.long": _StorageOnlyType(B), + "strategy.short": _StorageOnlyType(B), # Qty type constants - "strategy.fixed": I, - "strategy.cash": I, - "strategy.percent_of_equity": I, + **STRATEGY_SIZING_CONSTANTS, # Commission type constants - "strategy.commission.percent": I, - "strategy.commission.cash_per_contract": I, - "strategy.commission.cash_per_order": I, + "strategy.commission.percent": _StorageOnlyType(I), + "strategy.commission.cash_per_contract": _StorageOnlyType(I), + "strategy.commission.cash_per_order": _StorageOnlyType(I), # OCA constants - "strategy.oca.cancel": I, - "strategy.oca.reduce": I, - "strategy.oca.none": I, + "strategy.oca.cancel": _StorageOnlyType(I), + "strategy.oca.reduce": _StorageOnlyType(I), + "strategy.oca.none": _StorageOnlyType(I), # Direction constants "strategy.direction.all": S, "strategy.direction.long": S, "strategy.direction.short": S, } +STRATEGY_VARIABLES: dict[str, PineType] = { + name: _variable_storage_type(value) + for name, value in _STRATEGY_VARIABLE_DECLARATIONS.items() +} # Barstate variables BARSTATE_VARIABLES: dict[str, PineType] = { @@ -866,13 +901,17 @@ def get_value_function(name: str) -> IntrinsicFunc | None: } # display.* plot_display constants (PineScript v6 — values for C++ codegen) +_DISPLAY_VARIABLE_DECLARATIONS: dict[str, _StorageOnlyType] = { + "display.all": _StorageOnlyType(I), + "display.none": _StorageOnlyType(I), + "display.pane": _StorageOnlyType(I), + "display.data_window": _StorageOnlyType(I), + "display.status_line": _StorageOnlyType(I), + "display.price_scale": _StorageOnlyType(I), +} DISPLAY_VARIABLES: dict[str, PineType] = { - "display.all": I, - "display.none": I, - "display.pane": I, - "display.data_window": I, - "display.status_line": I, - "display.price_scale": I, + name: value.storage_type + for name, value in _DISPLAY_VARIABLE_DECLARATIONS.items() } @@ -1040,20 +1079,48 @@ def is_intrinsic_function(namespace: str | None, func_name: str) -> bool: return False -def get_variable_type(namespace: str | None, name: str) -> PineType: - """Return a builtin variable's declared type, or UNKNOWN for other names.""" +def _variable_declaration( + namespace: str | None, name: str, +) -> PineType | _StorageOnlyType | SizingConstant: if namespace is None: return BUILTIN_VARIABLES.get(name, PineType.UNKNOWN) full = f"{namespace}.{name}" for table in ( - STRATEGY_VARIABLES, BARSTATE_VARIABLES, SYMINFO_VARIABLES, - TIMEFRAME_VARIABLES, BUILTIN_VARIABLES, DISPLAY_VARIABLES, + _STRATEGY_VARIABLE_DECLARATIONS, BARSTATE_VARIABLES, SYMINFO_VARIABLES, + TIMEFRAME_VARIABLES, BUILTIN_VARIABLES, _DISPLAY_VARIABLE_DECLARATIONS, ): if full in table: return table[full] return PineType.UNKNOWN +def get_variable_type(namespace: str | None, name: str) -> PineType: + """Return a builtin's legacy storage type, not an operator-domain proof.""" + return _variable_storage_type(_variable_declaration(namespace, name)) + + +def get_sizing_constant(namespace: str | None, name: str) -> SizingConstant | None: + """Return a sizing constant's Pine value and host conversion together.""" + return STRATEGY_SIZING_CONSTANTS.get(f"{namespace}.{name}") + + +def get_variable_type_spec( + namespace: str | None, name: str, +) -> TypeSpec | None: + """Return a builtin variable's semantic Pine type specification. + + The variable tables also serve storage and lowering consumers, so this + lookup carries only the semantic facts that are safe for operator proofs. + """ + declaration = _variable_declaration(namespace, name) + if isinstance(declaration, _StorageOnlyType): + return None + primitive = _variable_storage_type(declaration) + if primitive in (PineType.UNKNOWN, PineType.VOID): + return None + return TypeSpec.primitive(primitive.value) + + def is_intrinsic_variable(namespace: str | None, name: str) -> bool: """Check if a name is a known TradingView built-in variable.""" return get_variable_type(namespace, name) != PineType.UNKNOWN diff --git a/pineforge_codegen/symbols.py b/pineforge_codegen/symbols.py index 71ead7fd..91eb86a0 100644 --- a/pineforge_codegen/symbols.py +++ b/pineforge_codegen/symbols.py @@ -123,6 +123,32 @@ def method_receiver_type_name(spec: TypeSpec | None) -> str | None: return None +def public_type_name(spec: TypeSpec | None) -> str | None: + """Return a Pine type name suitable for a public diagnostic. + + An unresolved numeric domain retains its uncertainty; it is not evidence + for either concrete Pine scalar type. Structured specs keep their spelling. + """ + if spec is None: + return None + name = method_receiver_type_name(spec) + if name is not None: + return name + return str(spec) + + +def public_cpp_type_name(cpp_type: str) -> str: + """Map the scalar C++ fallbacks used by codegen to Pine names.""" + return { + "bool": "bool", + "int": "int", + "int64_t": "int", + "float": "float", + "double": "float", + "std::string": "string", + }.get(cpp_type, "unknown") + + def method_receiver_cpp_token( spec: TypeSpec | None, fallback_name: str | None = None, diff --git a/scripts/gen_diagnostics_catalog.py b/scripts/gen_diagnostics_catalog.py index 9a7e3578..ba5f4ca4 100644 --- a/scripts/gen_diagnostics_catalog.py +++ b/scripts/gen_diagnostics_catalog.py @@ -1071,8 +1071,8 @@ def dump(catalog: dict) -> str: } -# Declaration presentation follows the extracted template through code allocation. -STRATEGY_DECLARATION_PRESENTATION = { +# Authored presentation follows the extracted template through code allocation. +DIAGNOSTIC_PRESENTATION = { "strategy(): too many positional arguments (expected {expected_count}, got {actual_count})": ( "A strategy declaration has more positional values than its signature accepts. " "Remove the extra values or use the declared parameter names.", @@ -1088,6 +1088,13 @@ def dump(catalog: dict) -> str: "Supply a literal or an expression over earlier constants.", "Strategy setting ''{name}'' must be a compile-time constant.", ), + "Cannot call ''{function_name}'' with an argument of type {argument_type} for " + "{parameter_type} parameter ''{name}''.": ( + "A non-string value cannot be used for a string parameter. Pass a string value " + "of the declared parameter type; a permitted generic na adopts that type.", + "Argument ''{name}'' of ''{function_name}'' has type {argument_type}, " + "but requires {parameter_type}.", + ), } # Public reason vocabulary for the located series-timeframe diagnostic. @@ -1137,7 +1144,7 @@ def main(argv=None) -> int: entry["user_message"] = sentence entry["explanation"] = entry["explanation"] or _ALLOWLIST_EXPLANATION for entry in catalog["codes"].values(): - presentation = STRATEGY_DECLARATION_PRESENTATION.get(entry["message"]) + presentation = DIAGNOSTIC_PRESENTATION.get(entry["message"]) if presentation is not None: explanation, sentence = presentation entry["explanation"] = entry["explanation"] or explanation diff --git a/tests/fixtures/diagnostic_codes_pin.json b/tests/fixtures/diagnostic_codes_pin.json index 0d4aaa73..a59e136b 100644 --- a/tests/fixtures/diagnostic_codes_pin.json +++ b/tests/fixtures/diagnostic_codes_pin.json @@ -236,6 +236,7 @@ "PF-E2090": "dcc4d05f50b881e5345de8c73ec7b7951bedc86c30a24d40bc0077fca5242a0d", "PF-E2091": "2d1b04fd25a4198619628b94bf8f90b4002f33e4081c2b780fea510c49e0b44a", "PF-E2092": "74b483fe343d239ec254f03e839b257611db5295b0433eabd15072f919950f3d", + "PF-E2095": "a2bf71e6ab48d39b72365b5a69960a8622c7b38deb0aace009215b46dc3cb89d", "PF-E3001": "585dad07e797bbe2cfffa9f45e14dee040be5570cc13f8e46fa4c31e953fc92a", "PF-E3002": "db089893825afe5ad1d91934c21107fc58451cadf3d71de2539b9c5805549433", "PF-E3003": "02571beee5163d1596d8a11fe49336dfcf65f86d4fed13254bac6b36023c3c79", diff --git a/tests/fixtures/diagnostic_notes/delta.json b/tests/fixtures/diagnostic_notes/delta.json index 5027fc9a..fae0cb0d 100644 --- a/tests/fixtures/diagnostic_notes/delta.json +++ b/tests/fixtures/diagnostic_notes/delta.json @@ -4,7 +4,8 @@ "user_message" ], "added_codes": [ - "PF-E5087" + "PF-E5087", + "PF-E2095" ], "catalog_additions": { "PF-E5088": { diff --git a/tests/fixtures/empty_timeframe/arguments.pine b/tests/fixtures/empty_timeframe/arguments.pine new file mode 100644 index 00000000..99afec55 --- /dev/null +++ b/tests/fixtures/empty_timeframe/arguments.pine @@ -0,0 +1,186 @@ +//@version=6 +strategy("Empty timeframe arguments", currency = currency.NONE) +frameInput = input.timeframe("", "Frame input") +frameString = input.string("", "String input") +frameVariable = frameString +_pf_timeframe_arg = frameString +pf_safe__pf_timeframe_arg = "240" +plainVariable = "" +var string persistentFrame = "" +emptyFrame() => "" +returnedFrame(string frameArg) => frameArg +secondsPos(string frameArg) => timeframe.in_seconds(frameArg) +secondsNamed(string frameArg) => timeframe.in_seconds(timeframe = frameArg) +changePos(string frameArg) => timeframe.change(frameArg) +changeNamed(string frameArg) => timeframe.change(timeframe = frameArg) +secondsCapturedPos(string _pf_timeframe_arg) => timeframe.in_seconds(_pf_timeframe_arg) +secondsCapturedNamed(string _pf_timeframe_arg) => timeframe.in_seconds(timeframe = _pf_timeframe_arg) +changeCapturedPos(string _pf_timeframe_arg) => timeframe.change(_pf_timeframe_arg) +changeCapturedNamed(string _pf_timeframe_arg) => timeframe.change(timeframe = _pf_timeframe_arg) +secondsStatefulPos(string frameArg) => + var string savedFrame = frameArg + timeframe.in_seconds(savedFrame) +secondsStatefulNamed(string frameArg) => + var string savedFrame = frameArg + timeframe.in_seconds(timeframe = savedFrame) +changeStatefulPos(string frameArg) => + var string savedFrame = frameArg + timeframe.change(savedFrame) +changeStatefulNamed(string frameArg) => + var string savedFrame = frameArg + timeframe.change(timeframe = savedFrame) +countedFrame(array counter, string frameArg) => + array.set(counter, 0, array.get(counter, 0) + 1) + frameArg +S_literal_pos = timeframe.in_seconds("") +S_literal_named = timeframe.in_seconds(timeframe = "") +S_input_pos = timeframe.in_seconds(frameInput) +S_input_named = timeframe.in_seconds(timeframe = frameInput) +S_string_input_pos = timeframe.in_seconds(frameString) +S_string_input_named = timeframe.in_seconds(timeframe = frameString) +S_variable_pos = timeframe.in_seconds(frameVariable) +S_variable_named = timeframe.in_seconds(timeframe = frameVariable) +S_temporary_name_pos = timeframe.in_seconds(_pf_timeframe_arg) +S_temporary_name_named = timeframe.in_seconds(timeframe = _pf_timeframe_arg) +S_parameter_name_pos = secondsCapturedPos(frameString) +S_parameter_name_named = secondsCapturedNamed(frameString) +S_occupied_escape_pos = timeframe.in_seconds(pf_safe__pf_timeframe_arg) +S_occupied_escape_named = timeframe.in_seconds(timeframe = pf_safe__pf_timeframe_arg) +S_plain_variable_pos = timeframe.in_seconds(plainVariable) +S_plain_variable_named = timeframe.in_seconds(timeframe = plainVariable) +S_persistent_pos = timeframe.in_seconds(persistentFrame) +S_persistent_named = timeframe.in_seconds(timeframe = persistentFrame) +S_return_pos = timeframe.in_seconds(emptyFrame()) +S_return_named = timeframe.in_seconds(timeframe = emptyFrame()) +S_return_input_pos = timeframe.in_seconds(returnedFrame(frameInput)) +S_return_input_named = timeframe.in_seconds(timeframe = returnedFrame(frameInput)) +S_period_pos = timeframe.in_seconds(timeframe.period) +S_period_named = timeframe.in_seconds(timeframe = timeframe.period) +S_explicit_pos = timeframe.in_seconds("240") +S_explicit_named = timeframe.in_seconds(timeframe = "240") +S_helper_empty_pos = secondsPos("") +S_helper_explicit_pos = secondsPos("240") +S_helper_empty_named = secondsNamed("") +S_helper_explicit_named = secondsNamed("240") +S_stateful_empty_pos = secondsStatefulPos("") +S_stateful_explicit_pos = secondsStatefulPos("240") +S_stateful_empty_named = secondsStatefulNamed("") +S_stateful_explicit_named = secondsStatefulNamed("240") +S_omitted = timeframe.in_seconds() +C_literal_pos = timeframe.change("") ? 1 : 0 +C_literal_named = timeframe.change(timeframe = "") ? 1 : 0 +C_input_pos = timeframe.change(frameInput) ? 1 : 0 +C_input_named = timeframe.change(timeframe = frameInput) ? 1 : 0 +C_string_input_pos = timeframe.change(frameString) ? 1 : 0 +C_string_input_named = timeframe.change(timeframe = frameString) ? 1 : 0 +C_variable_pos = timeframe.change(frameVariable) ? 1 : 0 +C_variable_named = timeframe.change(timeframe = frameVariable) ? 1 : 0 +C_temporary_name_pos = timeframe.change(_pf_timeframe_arg) ? 1 : 0 +C_temporary_name_named = timeframe.change(timeframe = _pf_timeframe_arg) ? 1 : 0 +C_parameter_name_pos = changeCapturedPos(frameString) ? 1 : 0 +C_parameter_name_named = changeCapturedNamed(frameString) ? 1 : 0 +C_occupied_escape_pos = timeframe.change(pf_safe__pf_timeframe_arg) ? 1 : 0 +C_occupied_escape_named = timeframe.change(timeframe = pf_safe__pf_timeframe_arg) ? 1 : 0 +C_plain_variable_pos = timeframe.change(plainVariable) ? 1 : 0 +C_plain_variable_named = timeframe.change(timeframe = plainVariable) ? 1 : 0 +C_persistent_pos = timeframe.change(persistentFrame) ? 1 : 0 +C_persistent_named = timeframe.change(timeframe = persistentFrame) ? 1 : 0 +C_return_pos = timeframe.change(emptyFrame()) ? 1 : 0 +C_return_named = timeframe.change(timeframe = emptyFrame()) ? 1 : 0 +C_return_input_pos = timeframe.change(returnedFrame(frameInput)) ? 1 : 0 +C_return_input_named = timeframe.change(timeframe = returnedFrame(frameInput)) ? 1 : 0 +C_period_pos = timeframe.change(timeframe.period) ? 1 : 0 +C_period_named = timeframe.change(timeframe = timeframe.period) ? 1 : 0 +C_explicit_pos = timeframe.change("240") ? 1 : 0 +C_explicit_named = timeframe.change(timeframe = "240") ? 1 : 0 +C_helper_empty_pos = changePos("") ? 1 : 0 +C_helper_explicit_pos = changePos("240") ? 1 : 0 +C_helper_empty_named = changeNamed("") ? 1 : 0 +C_helper_explicit_named = changeNamed("240") ? 1 : 0 +C_stateful_empty_pos = changeStatefulPos("") ? 1 : 0 +C_stateful_explicit_pos = changeStatefulPos("240") ? 1 : 0 +C_stateful_empty_named = changeStatefulNamed("") ? 1 : 0 +C_stateful_explicit_named = changeStatefulNamed("240") ? 1 : 0 +C_omitted = timeframe.change() ? 1 : 0 +dynamicFrame = bar_index % 2 == 0 ? "" : "240" +C_dynamic_pos = timeframe.change(dynamicFrame) ? 1 : 0 +C_dynamic_named = timeframe.change(timeframe = dynamicFrame) ? 1 : 0 +var calls = array.new_int(1, 0) +C_once_pos = timeframe.change(countedFrame(calls, frameInput)) ? 1 : 0 +C_once_named = timeframe.change(timeframe = countedFrame(calls, frameInput)) ? 1 : 0 +callCount = array.get(calls, 0) +// @pf-trace S_literal_pos=S_literal_pos +// @pf-trace S_literal_named=S_literal_named +// @pf-trace S_input_pos=S_input_pos +// @pf-trace S_input_named=S_input_named +// @pf-trace S_string_input_pos=S_string_input_pos +// @pf-trace S_string_input_named=S_string_input_named +// @pf-trace S_variable_pos=S_variable_pos +// @pf-trace S_variable_named=S_variable_named +// @pf-trace S_temporary_name_pos=S_temporary_name_pos +// @pf-trace S_temporary_name_named=S_temporary_name_named +// @pf-trace S_parameter_name_pos=S_parameter_name_pos +// @pf-trace S_parameter_name_named=S_parameter_name_named +// @pf-trace S_occupied_escape_pos=S_occupied_escape_pos +// @pf-trace S_occupied_escape_named=S_occupied_escape_named +// @pf-trace S_plain_variable_pos=S_plain_variable_pos +// @pf-trace S_plain_variable_named=S_plain_variable_named +// @pf-trace S_persistent_pos=S_persistent_pos +// @pf-trace S_persistent_named=S_persistent_named +// @pf-trace S_return_pos=S_return_pos +// @pf-trace S_return_named=S_return_named +// @pf-trace S_return_input_pos=S_return_input_pos +// @pf-trace S_return_input_named=S_return_input_named +// @pf-trace S_period_pos=S_period_pos +// @pf-trace S_period_named=S_period_named +// @pf-trace S_explicit_pos=S_explicit_pos +// @pf-trace S_explicit_named=S_explicit_named +// @pf-trace S_helper_empty_pos=S_helper_empty_pos +// @pf-trace S_helper_explicit_pos=S_helper_explicit_pos +// @pf-trace S_helper_empty_named=S_helper_empty_named +// @pf-trace S_helper_explicit_named=S_helper_explicit_named +// @pf-trace S_stateful_empty_pos=S_stateful_empty_pos +// @pf-trace S_stateful_explicit_pos=S_stateful_explicit_pos +// @pf-trace S_stateful_empty_named=S_stateful_empty_named +// @pf-trace S_stateful_explicit_named=S_stateful_explicit_named +// @pf-trace S_omitted=S_omitted +// @pf-trace C_literal_pos=C_literal_pos +// @pf-trace C_literal_named=C_literal_named +// @pf-trace C_input_pos=C_input_pos +// @pf-trace C_input_named=C_input_named +// @pf-trace C_string_input_pos=C_string_input_pos +// @pf-trace C_string_input_named=C_string_input_named +// @pf-trace C_variable_pos=C_variable_pos +// @pf-trace C_variable_named=C_variable_named +// @pf-trace C_temporary_name_pos=C_temporary_name_pos +// @pf-trace C_temporary_name_named=C_temporary_name_named +// @pf-trace C_parameter_name_pos=C_parameter_name_pos +// @pf-trace C_parameter_name_named=C_parameter_name_named +// @pf-trace C_occupied_escape_pos=C_occupied_escape_pos +// @pf-trace C_occupied_escape_named=C_occupied_escape_named +// @pf-trace C_plain_variable_pos=C_plain_variable_pos +// @pf-trace C_plain_variable_named=C_plain_variable_named +// @pf-trace C_persistent_pos=C_persistent_pos +// @pf-trace C_persistent_named=C_persistent_named +// @pf-trace C_return_pos=C_return_pos +// @pf-trace C_return_named=C_return_named +// @pf-trace C_return_input_pos=C_return_input_pos +// @pf-trace C_return_input_named=C_return_input_named +// @pf-trace C_period_pos=C_period_pos +// @pf-trace C_period_named=C_period_named +// @pf-trace C_explicit_pos=C_explicit_pos +// @pf-trace C_explicit_named=C_explicit_named +// @pf-trace C_helper_empty_pos=C_helper_empty_pos +// @pf-trace C_helper_explicit_pos=C_helper_explicit_pos +// @pf-trace C_helper_empty_named=C_helper_empty_named +// @pf-trace C_helper_explicit_named=C_helper_explicit_named +// @pf-trace C_stateful_empty_pos=C_stateful_empty_pos +// @pf-trace C_stateful_explicit_pos=C_stateful_explicit_pos +// @pf-trace C_stateful_empty_named=C_stateful_empty_named +// @pf-trace C_stateful_explicit_named=C_stateful_explicit_named +// @pf-trace C_omitted=C_omitted +// @pf-trace C_dynamic_pos=C_dynamic_pos +// @pf-trace C_dynamic_named=C_dynamic_named +// @pf-trace C_once_pos=C_once_pos +// @pf-trace C_once_named=C_once_named +// @pf-trace callCount=callCount diff --git a/tests/fixtures/empty_timeframe/reporter-change.pine b/tests/fixtures/empty_timeframe/reporter-change.pine new file mode 100644 index 00000000..7b69fac4 --- /dev/null +++ b/tests/fixtures/empty_timeframe/reporter-change.pine @@ -0,0 +1,10 @@ +//@version=6 +strategy("timeframe.change empty string", overlay = true, initial_capital = 10000) +fast = ta.ema(close, 10) +slow = ta.ema(close, 30) +chg = timeframe.change("") ? 1 : 0 +// @pf-trace chg=chg +if ta.crossover(fast, slow) + strategy.entry("Long", strategy.long, qty = 0.01) +if ta.crossunder(fast, slow) + strategy.close("Long") diff --git a/tests/fixtures/empty_timeframe/reporter-seconds.pine b/tests/fixtures/empty_timeframe/reporter-seconds.pine new file mode 100644 index 00000000..e9667c37 --- /dev/null +++ b/tests/fixtures/empty_timeframe/reporter-seconds.pine @@ -0,0 +1,10 @@ +//@version=6 +strategy("timeframe.in_seconds empty string", overlay = true, initial_capital = 10000) +fast = ta.ema(close, 10) +slow = ta.ema(close, 30) +secs = timeframe.in_seconds("") +// @pf-trace secs=secs +if ta.crossover(fast, slow) + strategy.entry("Long", strategy.long, qty = 0.01) +if ta.crossunder(fast, slow) + strategy.close("Long") diff --git a/tests/test_anchor_tf_change.py b/tests/test_anchor_tf_change.py index d8e08b23..9a0f2730 100644 --- a/tests/test_anchor_tf_change.py +++ b/tests/test_anchor_tf_change.py @@ -26,7 +26,30 @@ def _pine(body: str) -> str: def _calls(cpp: str, symbol: str) -> list[str]: """Every `symbol(...)` call expression in the generated C++ (one line each).""" - return [m.group(0) for m in re.finditer(rf"\b{re.escape(symbol)}\([^;]*", cpp)] + calls = [] + for match in re.finditer(rf"\b{re.escape(symbol)}\(", cpp): + depth, quote, escaped = 0, None, False + for index in range(match.end() - 1, len(cpp)): + char = cpp[index] + if quote: + if escaped: + escaped = False + elif char == "\\": + escaped = True + elif char == quote: + quote = None + elif char in ("\"", "'"): + quote = char + elif char == "(": + depth += 1 + elif char == ")": + depth -= 1 + if depth == 0: + calls.append(cpp[match.start():index + 1]) + break + else: + raise AssertionError("Unclosed generated call: " + symbol) + return calls def test_timeframe_change_uses_symbol_clock_overload(): diff --git a/tests/test_anchor_vwap.py b/tests/test_anchor_vwap.py index a97291c6..8376915d 100644 --- a/tests/test_anchor_vwap.py +++ b/tests/test_anchor_vwap.py @@ -67,7 +67,13 @@ def test_ta_vwap_bands_form_threads_symbol_clock(): assert calls, cpp for call in calls: assert ("current_bar_.timestamp, syminfo_.timezone, syminfo_.session, " - "tf_change(") in call, call + "((std::string(\"D\")).empty() ? tf_change(") in call, call + assert ( + 'tf_change(prev_bar_timestamp_, current_bar_.timestamp, script_tf_, ' + 'syminfo_.timezone, syminfo_.session) : ' + 'tf_change(prev_bar_timestamp_, current_bar_.timestamp, std::string("D"), ' + 'syminfo_.timezone, syminfo_.session)' + ) in call, call def test_vwap_prelude_shim_gated_on_usage(): diff --git a/tests/test_clone_name_allocation.py b/tests/test_clone_name_allocation.py new file mode 100644 index 00000000..348a3143 --- /dev/null +++ b/tests/test_clone_name_allocation.py @@ -0,0 +1,638 @@ +"""Generated state and methods share a namespace with authored identifiers.""" + +from __future__ import annotations + +import os +import re +from unittest.mock import patch + +import pytest + +from pineforge_codegen import transpile +from pineforge_codegen.codegen import CodeGen +from pineforge_codegen.codegen.helpers import is_emitter_temporary +from tests._compile import compile_cpp +from tests._e2e import reference_codegen, transpile_json + + +_VAR_CLONE = """//@version=6 +strategy("clone member spelling") +f() => + var float acc = 0.0 + acc += ta.ema(close, 3) + acc +a = f() +b = f() +var float acc_cs1 = 0.0 +acc_cs1 := acc_cs1 + 1.0 +plot(a + b + acc_cs1) +""" + +_METHOD_CLONE = """//@version=6 +strategy("clone method spelling") +f(src) => ta.ema(src, 3) +a = f(close) +b = f(open) +var float f_cs1 = 2.0 +plot(a + b + f_cs1) +""" + +_COLLECTION_CLONE = """//@version=6 +strategy("clone versus collection history") +f() => + var float _pf_collection_hist = 0.0 + _pf_collection_hist += ta.ema(close, 3) + _pf_collection_hist +a = f() +b = f() +var cs1 = array.new(1, 0.0) +array.set(cs1, 0, close) +prev = cs1[1] +plot(a + b + (na(prev) ? 0.0 : array.get(prev, 0))) +""" + +_SECURITY_CLONE = """//@version=6 +strategy("clone versus security state") +f() => + var float _sec1 = 0.0 + _sec1 += ta.ema(close, 3) + _sec1 +a = f() +b = f() +c = f() +var float cs2 = 0.0 +cs2 := cs2 + 1.0 +h() => close + cs2 +v0 = request.security(syminfo.tickerid, "60", close) +v1 = request.security(syminfo.tickerid, "60", h()) +plot(a + b + c + v0 + v1) +""" + +_BLOCK_MEMBER = """//@version=6 +strategy("block member spelling") +var float x__blk1 = 100.0 +float up = na +float down = na +if close > open + var float x = 0.0 + x += 1.0 + up := x +if close <= open + var float x = 0.0 + x += 2.0 + down := x +plot(x__blk1) +plot(nz(up) + nz(down)) +""" + +_RESERVED_CALLABLE = """//@version=6 +strategy("reserved callable history") +inner(float src) => src[1] +_sec7(float src) => inner(src + 1.0) +a = _sec7(close) +b = _sec7(open) +plot(a + b) +""" + +_FUNCTION_HISTORY_PREFIX = """//@version=6 +strategy("callable prefix and global history") +global_hist_1(float src) => src + 1.0 +var float global_hist_1 = 10.0 +a = global_hist_1(close) +var float total = 0.0 +total += close +observe() => total[1] +b = observe() +plot(a + global_hist_1 + b) +""" + +_FRESH_FIXNAN = """//@version=6 +strategy("nested fixnan storage") +leaf(float src) => fixnan(src) +left(float src) => leaf(src) +right(float src) => leaf(src) +a = left(bar_index % 3 == 0 ? na : close) +b = left(bar_index % 4 == 0 ? na : open) +c = right(bar_index % 5 == 0 ? na : high) +plot(a + b + c) +""" + + +def _emit(source: str) -> tuple[CodeGen, str]: + # Observe the actual final generator without replacing any pipeline pass. + # Public transpile still runs support checks, re-analysis and the allowlist. + generated: list[CodeGen] = [] + generate = CodeGen.generate + + def record(generator: CodeGen) -> str: + cpp = generate(generator) + generated.append(generator) + return cpp + + with patch.object(CodeGen, "generate", record): + cpp = transpile(source, check_support=True, libraries={}) + return generated[-1], cpp + + +def _scalar_member(cpp: str, name: str) -> None: + declarations = re.findall( + rf"^ double {re.escape(name)}(?: = [^;\n]+)?;$", cpp, re.M + ) + assert len(declarations) == 1, name + + +def _method_body(cpp: str, name: str) -> str: + declarations = list(re.finditer( + rf"^ double {re.escape(name)}\([^\n]*\) \{{\n", cpp, re.M + )) + assert len(declarations) == 1, name + start = declarations[0].end() + return cpp[start:cpp.index("\n }", start)] + + +def _has_identifier(cpp: str, name: str) -> bool: + return re.search(rf"\b{re.escape(name)}\b", cpp) is not None + + +@pytest.mark.parametrize("spelling", ["acc_cs1", "aa"]) +def test_persistent_clone_reproducer_and_rename_control(spelling: str) -> None: + generator, cpp = _emit(_VAR_CLONE.replace("acc_cs1", spelling)) + clone = generator._func_cs_var_remap[("f", 1)]["acc"] + + assert generator._safe_name(spelling) == spelling + _scalar_member(cpp, spelling) + _scalar_member(cpp, clone) + assert clone == ("acc_cs1__pf2" if spelling == "acc_cs1" else "acc_cs1") + body = _method_body(cpp, "f_cs1") + assert _has_identifier(body, clone) + assert not _has_identifier(body, spelling) + assert re.search(rf"return {re.escape(clone)};", body) + compile_cpp(cpp, label=f"persistent-clone-{spelling}") + + +@pytest.mark.parametrize("spelling", ["f_cs1", "aa"]) +def test_callable_clone_reproducer_and_rename_control(spelling: str) -> None: + generator, cpp = _emit(_METHOD_CLONE.replace("f_cs1", spelling)) + first = generator._func_variant_name("f", 0) + second = generator._func_variant_name("f", 1) + + assert generator._safe_name(spelling) == spelling + _scalar_member(cpp, spelling) + assert first == "f_cs0" + assert second == ("f_cs1__pf2" if spelling == "f_cs1" else "f_cs1") + for output, method in (("a", first), ("b", second)): + _method_body(cpp, method) + assert re.search(rf"\b{output} = {re.escape(method)}\(", cpp) + compile_cpp(cpp, label=f"callable-clone-{spelling}") + + +def test_clone_fallback_also_avoids_authored_fallback_spelling() -> None: + source = ( + _VAR_CLONE + + "var float acc_cs1__pf2 = 7.0\n" + + "plot(acc_cs1__pf2)\n" + ) + generator, cpp = _emit(source) + clone = generator._func_cs_var_remap[("f", 1)]["acc"] + + assert clone == "acc_cs1__pf3" + for name in ("acc_cs1", "acc_cs1__pf2", clone): + _scalar_member(cpp, name) + body = _method_body(cpp, "f_cs1") + assert _has_identifier(body, clone) + assert not _has_identifier(body, "acc_cs1__pf2") + compile_cpp(cpp, label="occupied-clone-fallback") + + +def test_clone_member_cannot_be_hidden_by_an_authored_parameter() -> None: + generator, cpp = _emit("""//@version=6 +strategy("clone parameter spelling") +f(float acc_cs1) => + var float acc = 0.0 + acc += acc_cs1 + acc +a = f(close) +b = f(open) +plot(a + b) +""") + clone = generator._func_cs_var_remap[("f", 1)]["acc"] + assert clone == "acc_cs1__pf2" + _scalar_member(cpp, clone) + body = _method_body(cpp, "f_cs1") + assert _has_identifier(body, "acc_cs1") + assert _has_identifier(body, clone) + assert re.search(rf"return {re.escape(clone)};", body) + compile_cpp(cpp, label="clone-versus-parameter") + + +@pytest.mark.parametrize("storage", ["var ", ""], ids=["persistent", "ordinary"]) +def test_function_member_prefix_is_allocated_against_authored_members( + storage: str, +) -> None: + source = """//@version=6 +strategy("callable prefix spelling") +echo(float src) => src + 1.0 +var float echo = 10.0 +var float _fn_echo = 20.0 +a = echo(close) +plot(a + echo + _fn_echo) +""" + generator, cpp = _emit(source.replace("var float ", f"{storage}float ")) + method = generator._func_cpp_base_name("echo") + + assert method == "_fn_echo__pf2" + _method_body(cpp, method) + assert re.search(rf"\ba = {re.escape(method)}\(", cpp) + for name in ("echo", "_fn_echo"): + assert generator._safe_name(name) == name + _scalar_member(cpp, name) + compile_cpp(cpp, label="occupied-function-prefix") + + +def test_generated_method_base_cannot_alias_generated_storage_clone() -> None: + generator, cpp = _emit("""//@version=6 +strategy("generated method and storage spelling") +type Holder + float value +method sample_cs1(Holder self) => self.value +leaf() => + var float _udt_Holder_sample = 0.0 + _udt_Holder_sample += close + _udt_Holder_sample +a = leaf() +b = leaf() +var Holder holder = Holder.new(3.0) +c = holder.sample_cs1() +plot(a + b + c) +""") + storage = generator._func_cs_var_remap[("leaf", 1)]["_udt_Holder_sample"] + info = next(fi for fi in generator.ctx.func_infos if fi.is_udt_method) + method = generator._func_cpp_base_name(info.name) + + assert storage == "_udt_Holder_sample_cs1" + assert method == "_udt_Holder_sample_cs1__pf2" + _scalar_member(cpp, storage) + _method_body(cpp, method) + assert re.search(rf"\bc = {re.escape(method)}\(", cpp) + compile_cpp(cpp, label="generated-method-versus-storage") + + +def test_nested_instances_reserve_authored_method_and_state_spellings() -> None: + generator, cpp = _emit("""//@version=6 +strategy("nested instance spelling") +leaf(int size, float src) => + var float state = 0.0 + state += ta.highest(src, size) + state +f_get(int len, float src) => leaf(len, src) +g_get(int len, float src) => leaf(len, src) +a = f_get(10, close) +b = f_get(20, open) +c = g_get(30, high) +var float leaf__ni1 = 2.0 +var float state__ni1 = 3.0 +plot(a + b + c + leaf__ni1 + state__ni1) +""") + authored = {"leaf__ni1", "state__ni1"} + assert generator._fresh_instances + generated: list[str] = [] + for instance in generator._fresh_instances: + method = instance["name"] + generated.append(method) + body = _method_body(cpp, method) + for storage in instance["var_remap"].values(): + generated.append(storage) + _scalar_member(cpp, storage) + assert _has_identifier(body, storage) + assert all(not _has_identifier(body, name) for name in authored) + assert authored.isdisjoint(generated) + assert len(generated) == len(set(generated)) + assert "leaf__ni1__pf2" in generated + assert "state__ni1__pf2" in generated + for name in authored: + _scalar_member(cpp, name) + compile_cpp(cpp, label="occupied-nested-instances") + + +def test_renamed_callable_context_keeps_compound_argument_history() -> None: + generator, cpp = _emit("""//@version=6 +strategy("clone context history spelling") +inner(float src) => src[1] +wrapped(float src) => inner(src + 1.0) +a = wrapped(close) +b = wrapped(open) +var float wrapped_cs1 = 2.0 +plot(a + b + wrapped_cs1) +""") + contexts = generator._inline_history_contexts_for_owner("wrapped") + assert contexts == ["wrapped_cs0", "wrapped_cs1__pf2"] + bridges = [ + info for info in generator._inline_history_members + if info["kind"] in {"series_arg", "udf_series_arg"} + ] + assert set(contexts) <= {info["context"] for info in bridges} + for index, context in enumerate(contexts): + assert generator._func_variant_callsite[("wrapped", context)] == index + body = _method_body(cpp, context) + owned = [info for info in bridges if info["context"] == context] + assert all(_has_identifier(body, info["member_name"]) for info in owned) + compile_cpp(cpp, label="renamed-callable-history-context") + + +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +def test_typed_method_variants_reserve_authored_clone_spelling(named: bool) -> None: + source = """//@version=6 +strategy("typed method clone spelling") +type Holder + float seed +method sample(Holder self, float src) => self.seed + src[1] +var Holder holder = Holder.new(3.0) +a = holder.sample(close + 1.0) +b = holder.sample(open + 1.0) +var float _udt_Holder_sample_cs1 = 2.0 +plot(a + b + _udt_Holder_sample_cs1) +""" + if named: + source = source.replace( + "sample(close + 1.0)", "sample(src=close + 1.0)", + ).replace("sample(open + 1.0)", "sample(src=open + 1.0)") + generator, cpp = _emit(source) + info = next(fi for fi in generator.ctx.func_infos if fi.is_udt_method) + first = generator._func_variant_name(info.name, 0) + second = generator._func_variant_name(info.name, 1) + + assert first == "_udt_Holder_sample_cs0" + assert second == "_udt_Holder_sample_cs1__pf2" + _scalar_member(cpp, "_udt_Holder_sample_cs1") + for output, method in (("a", first), ("b", second)): + _method_body(cpp, method) + assert re.search(rf"\b{output} = {re.escape(method)}\(", cpp) + assert any(info["kind"] == "series_arg" + for info in generator._inline_history_members) + compile_cpp(cpp, label="occupied-typed-method-clone") + + +@pytest.mark.parametrize("spelling", ["cs1", "aa"]) +def test_clone_and_collection_history_keep_distinct_state(spelling: str) -> None: + generator, cpp = _emit(re.sub(r"\bcs1\b", spelling, _COLLECTION_CLONE)) + clone = generator._func_cs_var_remap[("f", 1)]["_pf_collection_hist"] + variable, = generator._collection_history_variables() + history = generator._collection_history_member(variable) + + assert clone == "pf_safe__pf_collection_hist_cs1" + assert history == f"_pf_collection_hist_{spelling}" + assert clone != history + _scalar_member(cpp, clone) + body = _method_body(cpp, "f_cs1") + assert _has_identifier(body, clone) + assert not _has_identifier(body, history) + assert f"return {clone};" in body + assert f"{history}.open_slot({spelling}," in cpp + assert re.search(rf"{re.escape(history)}\.(?:reference|value)\(1\)", cpp) + assert f"{history}.close({spelling}" in cpp + compile_cpp(cpp, label=f"clone-collection-history-{spelling}") + + +def test_reserved_authored_escape_and_composed_clone_do_not_alias() -> None: + generator, cpp = _emit( + _COLLECTION_CLONE + + "var float _pf_collection_hist_cs1 = 7.0\n" + + "plot(_pf_collection_hist_cs1)\n" + ) + authored = generator._safe_name("_pf_collection_hist_cs1") + clone = generator._func_cs_var_remap[("f", 1)]["_pf_collection_hist"] + assert authored == "pf_safe__pf_collection_hist_cs1" + assert clone == f"{authored}__pf2" + for member in (authored, clone): + _scalar_member(cpp, member) + body = _method_body(cpp, "f_cs1") + assert f"return {clone};" in body + assert not _has_identifier(body, authored) + compile_cpp(cpp, label="authored-escape-versus-composed-clone") + + +@pytest.mark.parametrize("spelling", ["cs2", "aa"]) +def test_clone_and_requested_mutable_state_stay_distinct(spelling: str) -> None: + generator, cpp = _emit(re.sub(r"\bcs2\b", spelling, _SECURITY_CLONE)) + clone = generator._func_cs_var_remap[("f", 2)]["_sec1"] + state = generator._security_state_name(1, spelling) + + assert clone == "pf_safe__sec1_cs2" + assert state == f"_sec1_{spelling}" + assert clone != state + assert _has_identifier(cpp, state) + _scalar_member(cpp, clone) + body = _method_body(cpp, "f_cs2") + assert _has_identifier(body, clone) + assert not _has_identifier(body, state) + assert f"return {clone};" in body + compile_cpp(cpp, label=f"clone-requested-state-{spelling}") + + +@pytest.mark.parametrize("spelling", ["x__blk1", "aa"]) +def test_sibling_storage_avoids_an_authored_derived_name(spelling: str) -> None: + generator, cpp = _emit(_BLOCK_MEMBER.replace("x__blk1", spelling)) + members = [ + meta[1] for meta in generator.ctx.var_member_metadata_by_node.values() + if meta[0].name == "x" + ] + assert members == ["x", "x__blk2" if spelling == "x__blk1" else "x__blk1"] + assert spelling not in members + for member in [spelling, *members]: + _scalar_member(cpp, member) + assert f"up = {members[0]};" in cpp + assert f"down = {members[1]};" in cpp + assert f"{members[0]} += 1.0;" in cpp + assert f"{members[1]} += 2.0;" in cpp + compile_cpp(cpp, label=f"sibling-storage-{spelling}") + + +@pytest.mark.parametrize("stem", [ + "_pf_collection_hist", "_sec7", "_precalc", "_strat", +]) +def test_composed_reserved_storage_avoids_a_parameter_at_its_escape( + stem: str, +) -> None: + parameter = f"pf_safe_{stem}_cs1" + generator, cpp = _emit(f"""//@version=6 +strategy("reserved clone parameter") +f(float {parameter}) => + var float {stem} = 0.0 + {stem} += {parameter} + {stem} +a = f(close) +b = f(open) +plot(a + b) +""") + clone = generator._func_cs_var_remap[("f", 1)][stem] + assert is_emitter_temporary(f"{stem}_cs1") + assert not is_emitter_temporary(clone) + assert clone == f"{parameter}__pf2" + _scalar_member(cpp, clone) + body = _method_body(cpp, "f_cs1") + assert _has_identifier(body, parameter) + assert _has_identifier(body, clone) + assert f"return {clone};" in body + checkpoint = cpp.split("_pf_script_state_checkpoint_.emplace(_PFScriptState{", 1)[1] + checkpoint = checkpoint.split("\n });", 1)[0] + assert re.search(rf"^\s+{re.escape(clone)},$", checkpoint, re.M) + compile_cpp(cpp, label="reserved-clone-versus-escaped-parameter") + + +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +def test_reserved_callable_variants_keep_their_history_contexts(named: bool) -> None: + source = _RESERVED_CALLABLE + if named: + source = source.replace( + "_sec7(close)", "_sec7(src=close)", + ).replace("_sec7(open)", "_sec7(src=open)") + generator, cpp = _emit(source) + contexts = generator._inline_history_contexts_for_owner("_sec7") + assert contexts == ["pf_safe__sec7_cs0", "pf_safe__sec7_cs1"] + bridges = [ + info for info in generator._inline_history_members + if info["kind"] in {"series_arg", "udf_series_arg"} + ] + for index, context in enumerate(contexts): + assert generator._func_variant_callsite[("_sec7", context)] == index + body = _method_body(cpp, context) + owned = [info for info in bridges if info["context"] == context] + assert owned + assert all(_has_identifier(body, info["member_name"]) for info in owned) + assert re.search(rf"\b{'ab'[index]} = {context}\(", cpp) + compile_cpp(cpp, label="reserved-callable-history-contexts") + + +def test_callable_prefix_cannot_capture_fixed_global_history_storage() -> None: + generator, cpp = _emit(_FUNCTION_HISTORY_PREFIX) + method = generator._func_cpp_base_name("global_hist_1") + assert method == "pf_safe__fn_global_hist_1" + _method_body(cpp, method) + assert re.search(rf"\ba = {method}\(", cpp) + assert re.search(r"^ Series _fn_global_hist_1;$", cpp, re.M) + assert any( + info["kind"] == "fn_global_hist" and info["member_name"] == "_fn_global_hist_1" + for info in generator._inline_history_members + ) + _scalar_member(cpp, "global_hist_1") + compile_cpp(cpp, label="callable-prefix-versus-global-history") + + +def test_sibling_allocation_reserves_later_names_and_history_storage() -> None: + source = _BLOCK_MEMBER.replace("up := x", "up := nz(x[1])").replace( + "down := x", "down := nz(x[1])", + ) + "var float x__blk2 = 7.0\nplot(x__blk2)\n" + generator, cpp = _emit(source) + members = [ + generator._safe_name(meta[1]) + for meta in generator.ctx.var_member_metadata_by_node.values() + if meta[0].name == "x" + ] + assert members == ["x", "x__blk3"] + for member in members: + assert member in generator._series_var_member_names + assert re.search(rf"^ Series {member};$", cpp, re.M) + for output, member in zip(("up", "down"), members): + assignment = [ + line for line in cpp.splitlines() if re.match(rf"\s+{output} = ", line) + ][-1] + assert _has_identifier(assignment, member) + _scalar_member(cpp, "x__blk1") + _scalar_member(cpp, "x__blk2") + compile_cpp(cpp, label="sibling-later-name-and-history") + + +def test_analyzer_only_storage_uses_collision_safe_cpp_escaping() -> None: + source = re.sub( + r"\bx\b", "_sec7", + _BLOCK_MEMBER.replace("x__blk1", "pf_safe__sec7__blk1"), + ) + generator, cpp = _emit(source) + members = [ + meta[1] for meta in generator.ctx.var_member_metadata_by_node.values() + if meta[0].name == "_sec7" + ] + assert members == ["_sec7", "_sec7__blk1"] + safe = generator._safe_name(members[1]) + assert safe == "pf_safe__sec7__blk1_2" + for member in ["pf_safe__sec7__blk1", members[0], safe]: + _scalar_member(cpp, member) + assert f"up = {members[0]};" in cpp + assert f"down = {safe};" in cpp + compile_cpp(cpp, label="analyzer-storage-escaping") + + +def test_callable_block_storage_keeps_parameters_and_clones_distinct() -> None: + generator, cpp = _emit("""//@version=6 +strategy("callable block member spelling") +f(float x__blk1) => + float result = x__blk1 + if close > open + var float x = 0.0 + x += x__blk1 + result += x + if close <= open + var float x = 0.0 + x += x__blk1 * 2.0 + result += x + result +a = f(close) +b = f(open) +plot(a + b) +""") + assert generator.ctx.block_var_renames + for index in (0, 1): + remap = generator._func_cs_var_remap[("f", index)] + members = [remap[name] for name in ("x", "x__blk2")] + assert len(set(members)) == 2 + body = _method_body(cpp, f"f_cs{index}") + assert "double x__blk1" in cpp + assert _has_identifier(body, "x__blk1") + for member in members: + _scalar_member(cpp, member) + assert _has_identifier(body, member) + compile_cpp(cpp, label="callable-block-parameter-and-clones") + + +def test_fixed_fixnan_instances_retain_their_helper_namespace() -> None: + generator, cpp = _emit(_FRESH_FIXNAN) + fresh = generator._fresh_fixnan_members + assert fresh + for site, member in fresh: + assert member.startswith(f"{site.member_name}__ni") + assert not member.startswith("pf_safe_") + _scalar_member(cpp, member) + assert any( + _has_identifier(_method_body(cpp, instance["name"]), member) + for instance in generator._fresh_instances + ) + compile_cpp(cpp, label="fixed-fixnan-instance-namespace") + + +@pytest.fixture(scope="module") +def clone_reference(): + reference = reference_codegen("4e51f6d00e3a015194d93b06e51ad90f2404cc14") + if reference is None: + unavailable = (pytest.fail if os.environ.get("PINEFORGE_REQUIRE_NATIVE") == "1" + else pytest.skip) + unavailable("MISSING INPUT: the clone spelling reference commit is unavailable") + return reference + + +@pytest.mark.parametrize("source, original", [ + (_VAR_CLONE, "acc_cs1"), + (_METHOD_CLONE, "f_cs1"), + (_BLOCK_MEMBER, "x__blk1"), + (_RESERVED_CALLABLE, "_sec7"), + (_FRESH_FIXNAN, None), +]) +def test_noncolliding_controls_keep_complete_reference_cpp( + source, original, clone_reference, tmp_path +) -> None: + if original is not None: + source = source.replace(original, "aa") + pine = tmp_path / "strategy.pine" + pine.write_text(source, encoding="utf-8") + reference = transpile_json(pine, root=clone_reference) + assert reference["ok"], reference + assert transpile(source, check_support=True, libraries={}) == reference["cpp"] diff --git a/tests/test_codegen_drawing_data.py b/tests/test_codegen_drawing_data.py index e349dcc9..303859fd 100644 --- a/tests/test_codegen_drawing_data.py +++ b/tests/test_codegen_drawing_data.py @@ -302,7 +302,7 @@ def test_drawing_handle_ternary_na_local_reassignment_compile(): strategy.entry("L", strategy.long) ''' cpp = transpile(src) - body = cpp.split("Line update(Line& initial) {", 1)[1].split(" }", 1)[0] + body = cpp.split("Line update(Line initial) {", 1)[1].split(" }", 1)[0] assert "h = ((cond) ? (_pf_collect_lines_(pf_line_new" in body assert ": (Line{}));" in body assert "na()" not in body diff --git a/tests/test_diagnostic_codes.py b/tests/test_diagnostic_codes.py index 8090ba70..0db7aeaf 100644 --- a/tests/test_diagnostic_codes.py +++ b/tests/test_diagnostic_codes.py @@ -141,6 +141,44 @@ def test_classify_reads_arguments_raw(): assert render_diagnostic(code, args) == ("Undefined variable: 'rsiLen'", None) +@pytest.mark.parametrize("message, expected_code, expected_args", [ + ( + "Cannot call 'timeframe.change' with an argument of type int for string parameter 'timeframe'.", + "PF-E2095", + {"function_name": "timeframe.change", "name": "timeframe", + "argument_type": "int", "parameter_type": "string"}, + ), + ( + "Cannot call 'chooseFrame' with an argument of type float for string parameter 'candidate'.", + "PF-E2095", + {"function_name": "chooseFrame", "name": "candidate", + "argument_type": "float", "parameter_type": "string"}, + ), + ( + "Cannot call 'strategy.risk.allow_entry_in' with a series argument for simple parameter 'value'.", + "PF-E2080", + {"function_name": "strategy.risk.allow_entry_in", "name": "value", + "argument_qualifier": "series", "parameter_qualifier": "simple"}, + ), + ( + "Cannot call 'keep' with a input argument for const parameter 'amount'.", + "PF-E2080", + {"function_name": "keep", "name": "amount", + "argument_qualifier": "input", "parameter_qualifier": "const"}, + ), + ( + "matrix>: nested collection element types not supported in v1.", + "PF-E1004", + {"element_type": "array"}, + ), +]) +def test_catalog_argument_kinds_disambiguate_matching_templates( + message, expected_code, expected_args, +): + assert classify("error", message) == (expected_code, expected_args) + assert render_diagnostic(expected_code, expected_args) == (message, None) + + def test_uncatalogued_text_keeps_a_code(): diagnostic = Diagnostic(Level.ERROR, Phase.ANALYZER, SourceLocation("t.pine", 1, 1, 1), "boom") diff --git a/tests/test_drawing_type_casts.py b/tests/test_drawing_type_casts.py index 533b2516..d84c0084 100644 --- a/tests/test_drawing_type_casts.py +++ b/tests/test_drawing_type_casts.py @@ -5,9 +5,13 @@ from pineforge_codegen import transpile -@pytest.mark.parametrize("kind,cpp_type", [("line", "Line"), ("box", "Box")]) +@pytest.mark.parametrize( + "kind,cpp_type", + [("line", "Line"), ("box", "Box"), ("label", "Label"), ("linefill", "Linefill")], +) @pytest.mark.parametrize("name", ["handle", "guide"]) -def test_na_drawing_cast_matches_typed_na_declaration(kind, cpp_type, name): +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +def test_na_drawing_cast_matches_typed_na_declaration(kind, cpp_type, name, named): def source(value): return ( '//@version=6\nstrategy("Drawing casts", overlay=true)\n' @@ -15,7 +19,8 @@ def source(value): f'{kind}.delete({name})\nplot(close)\n' ) - cpp = transpile(source(f"{kind}(na)")) + argument = "x=na" if named else "na" + cpp = transpile(source(f"{kind}({argument})")) control = transpile(source("na")) initializers = lambda text: [ line.strip() for line in text.splitlines() diff --git a/tests/test_e2e_empty_timeframe.py b/tests/test_e2e_empty_timeframe.py new file mode 100644 index 00000000..f7670011 --- /dev/null +++ b/tests/test_e2e_empty_timeframe.py @@ -0,0 +1,219 @@ +"""Empty timeframe strings resolve after argument evaluation. + +The complete reporter strategies pin the two original defects. The argument +matrix observes native values for inputs, variables, returns and helpers on +several chart periods, and counts evaluations of a stateful string argument. +""" + +from __future__ import annotations + +from datetime import datetime, timezone +import math +from pathlib import Path + +import pytest + +from tests._e2e import ( + Build, assert_same_runs, build_strategy_library, chart_feed_head, + execute_all, ok, run_strategy, skip_unless_e2e_env, transpile_json, +) +from tests._requests import Feed + + +FIXTURES = Path(__file__).parent / "fixtures" / "empty_timeframe" +REPORTER_BARS = 1441 +FIXED_SECONDS = {"1S": 1, "5": 300, "60": 3600, "D": 86400, "W": 604800} +ARGUMENTS = { + "literal": "chart", + "input": "input", + "string_input": "input", + "variable": "input", + "temporary_name": "input", + "parameter_name": "input", + "occupied_escape": "explicit", + "plain_variable": "chart", + "persistent": "chart", + "return": "chart", + "return_input": "input", + "period": "chart", + "explicit": "explicit", +} +BUILTINS = {"S": "in_seconds", "C": "change"} +STYLES = ("pos", "named") + + +@pytest.fixture(scope="module") +def reporter_runs(tmp_path_factory): + engine = skip_unless_e2e_env() + base = tmp_path_factory.mktemp("reporter_empty_timeframe") + # A public corpus replay, distinct from the reporter's BTC date window. + input_bars = REPORTER_BARS * 4 + expanded = chart_feed_head(engine, base, input_bars + 4) + rows = expanded.read_text().splitlines() + first = next(index for index, line in enumerate(rows[1:], 1) + if int(line.split(",", 1)[0]) % 3_600_000 == 0) + selected = rows[first:first + input_bars] + assert len(selected) == input_bars, "public feed has too few hourly chart inputs" + feed = base / "aligned_15m.csv" + feed.write_text("\n".join([rows[0], *selected]) + "\n") + builds = {} + for kind, function in (("seconds", "in_seconds"), ("change", "change")): + source = (FIXTURES / f"reporter-{kind}.pine").read_text() + original = f'timeframe.{function}("")' + assert source.count(original) == 1 + for variant, argument in (("empty", '""'), ("period", "timeframe.period"), + ("omitted", "")): + builds[f"{kind}_{variant}"] = Build( + source.replace(original, f"timeframe.{function}({argument})", 1), + overrides={ + "input_tf": "15", "script_tf": "60", + "runtime_overrides": {"currency": "USDT", "timezone": "UTC"}, + }, + trace=True, + ) + return execute_all(engine, feed, base, builds) + + +@pytest.mark.parametrize("kind", ("seconds", "change"), ids=("in_seconds", "change")) +def test_reporter_empty_matches_chart_controls(kind, reporter_runs): + subject = ok(reporter_runs, f"{kind}_empty") + period = ok(reporter_runs, f"{kind}_period") + omitted = ok(reporter_runs, f"{kind}_omitted") + label = "secs" if kind == "seconds" else "chg" + for tag, count, seconds in (("default", REPORTER_BARS * 4, 900.0), + ("override", REPORTER_BARS, 3600.0)): + control = period.traces[tag] + assert len(control) == count + assert {row["name"] for row in control} == {label} + assert [row["bar_index"] for row in control] == list(range(count)) + assert all(b["timestamp"] - a["timestamp"] == seconds * 1000 + for a, b in zip(control, control[1:])) + expected = ([seconds] * count if kind == "seconds" + else [0.0] + [1.0] * (count - 1)) + assert [row["value"] for row in control] == expected + assert_same_runs(omitted, period) + assert_same_runs(subject, period) + + +def _clock_stamps(period): + epoch = int(datetime(2024, 1, 1, tzinfo=timezone.utc).timestamp() * 1000) + if period == "M": + return [int(datetime(2024, month, 1, tzinfo=timezone.utc).timestamp() * 1000) + for month in range(1, 6)] + return [epoch + index * FIXED_SECONDS[period] * 1000 for index in range(33)] + + +@pytest.fixture(scope="module") +def argument_runs(tmp_path_factory): + engine = skip_unless_e2e_env() + base = tmp_path_factory.mktemp("empty_timeframe_arguments") + pine = FIXTURES / "arguments.pine" + source = pine.read_text() + result = transpile_json(pine) + assert result.get("ok"), result.get("diagnostics") + build_strategy_library(result["cpp"], base) + expected_names = {line.split()[2].split("=", 1)[0] + for line in source.splitlines() if line.startswith("// @pf-trace ")} + outcomes = {} + for period in (*FIXED_SECONDS, "M"): + stamps = _clock_stamps(period) + last_close = ( + int(datetime(2024, 6, 1, tzinfo=timezone.utc).timestamp() * 1000) + if period == "M" else stamps[-1] + stamps[-1] - stamps[-2] + ) + bars = [(stamp, close_stamp, 100.0 + index) + for index, (stamp, close_stamp) + in enumerate(zip(stamps, [*stamps[1:], last_close]))] + feed = base / f"clock_{period}.csv" + feed.write_bytes(Feed("BINANCE:BTCUSDT", period, bars).csv()) + for argument in ("", "240"): + tag = f"{period}_{'empty' if not argument else 'explicit'}" + overrides = { + "input_tf": period, "script_tf": period, + "Frame input": argument, "String input": argument, + "runtime_overrides": { + "currency": "USDT", "timezone": "UTC", "session": "24x7", + }, + } + _trades, records, stderr = run_strategy( + engine, base, feed, overrides, tag, trace=True, + ) + (base / f"run_{tag}.stderr").write_text(stderr) + columns = {} + for row in records: + columns.setdefault(row["name"], []).append(row) + assert set(columns) == expected_names, f"{tag}: missing native trace names" + for name, series in columns.items(): + assert len(series) == len(stamps), (tag, name, "missing native calls") + assert [row["bar_index"] for row in series] == list(range(len(stamps))) + assert [row["timestamp"] for row in series] == stamps + outcomes[period, argument] = { + name: [row["value"] for row in series] for name, series in columns.items() + } + return outcomes + + +def _expected(frame, stamps, chart_seconds): + if frame == "chart": + return [chart_seconds] * len(stamps), [0.0] + [1.0] * (len(stamps) - 1) + assert frame == "explicit" + size = 240 * 60 * 1000 + return ([14400.0] * len(stamps), + [0.0] + [float(now // size != before // size) + for before, now in zip(stamps, stamps[1:])]) + + +@pytest.mark.parametrize("builtin", BUILTINS, ids=("in_seconds", "change")) +@pytest.mark.parametrize("name", ARGUMENTS) +@pytest.mark.parametrize("style", STYLES) +def test_empty_argument_source_uses_the_evaluated_string(builtin, name, style, argument_runs): + for (period, argument), columns in argument_runs.items(): + stamps = _clock_stamps(period) + chart_seconds = columns["S_period_pos"][0] + assert math.isfinite(chart_seconds) + if period in FIXED_SECONDS: + assert chart_seconds == FIXED_SECONDS[period] + # Calendar-only engine periods retain their existing conversion; + # equality to the period control is the empty-argument contract. + assert columns["S_period_pos"] == [chart_seconds] * len(stamps) + assert columns["C_period_pos"] == [0.0] + [1.0] * (len(stamps) - 1) + resolution = ARGUMENTS[name] + frame = ("explicit" if resolution == "explicit" + or (resolution == "input" and argument) else "chart") + seconds, changes = _expected(frame, stamps, chart_seconds) + assert columns[f"{builtin}_{name}_{style}"] == ( + seconds if builtin == "S" else changes + ), (period, argument, builtin, name, style) + + +@pytest.mark.parametrize("builtin", BUILTINS, ids=("in_seconds", "change")) +@pytest.mark.parametrize("style", STYLES) +@pytest.mark.parametrize("body", ("helper", "stateful")) +def test_each_helper_body_preserves_empty_and_explicit_arguments(builtin, style, body, argument_runs): + for (period, argument), columns in argument_runs.items(): + stamps = _clock_stamps(period) + for suffix, frame in (("empty", "chart"), ("explicit", "explicit")): + seconds, changes = _expected(frame, stamps, columns["S_period_pos"][0]) + assert columns[f"{builtin}_{body}_{suffix}_{style}"] == ( + seconds if builtin == "S" else changes + ), (period, argument, builtin, style, body, suffix) + + +def test_omitted_arguments_keep_the_existing_chart_resolution(argument_runs): + for columns in argument_runs.values(): + for builtin in BUILTINS: + assert columns[f"{builtin}_omitted"] == columns[f"{builtin}_period_pos"] + + +def test_change_evaluates_each_string_once_and_can_switch_to_explicit(argument_runs): + for (period, argument), columns in argument_runs.items(): + stamps = _clock_stamps(period) + _, chart = _expected("chart", stamps, columns["S_period_pos"][0]) + _, explicit = _expected("explicit", stamps, columns["S_period_pos"][0]) + dynamic = [chart[index] if index % 2 == 0 else explicit[index] + for index in range(len(stamps))] + assert columns["C_dynamic_pos"] == dynamic + assert columns["C_dynamic_named"] == dynamic + assert columns["C_once_pos"] == (explicit if argument else chart) + assert columns["C_once_named"] == (explicit if argument else chart) + assert columns["callCount"] == [2.0 * (index + 1) for index in range(len(stamps))] diff --git a/tests/test_e2e_temporary_name_hygiene.py b/tests/test_e2e_temporary_name_hygiene.py index e9295016..e019e8c8 100644 --- a/tests/test_e2e_temporary_name_hygiene.py +++ b/tests/test_e2e_temporary_name_hygiene.py @@ -117,12 +117,24 @@ def test_every_temporary_the_emitter_declares_is_reserved(): # Temporaries derived from emitted C++: every underscore-prefixed local # declared inside a generated lambda that is not a name of the script. names = set() - for name in TAPES: - source = (FIXTURES / f"{name}.pine").read_text() + sources = [(FIXTURES / f"{name}.pine").read_text() for name in TAPES] + sources.append( + HEAD + 'frameChoice = input.timeframe("")\n' + 'chosenFrame() => frameChoice\n' + 'secondsValue = timeframe.in_seconds(chosenFrame())\n' + 'changedValue = timeframe.change(timeframe = chosenFrame())\n' + 'plot(secondsValue)\nplot(changedValue ? 1 : 0)\n' + ) + sources.append( + HEAD + 'qtyMode = input.string("cash", options = ["cash", "percent_of_equity"])\n' + 'strategy.entry("L", strategy.long, qty = 1, qty_type = qtyMode)\n' + ) + for source in sources: authored = {t.value for t in Lexer(source).tokenize() if t.type.name == "IDENT"} for line in transpile(source).splitlines(): if "[&]" in line: names |= {n for n in DECL.findall(line) if n not in authored and not n.startswith("pf_safe_")} assert names, "the tapes' C++ declares no temporary" + assert "_pf_timeframe_arg" in names assert sorted(n for n in names if not is_emitter_temporary(n)) == [] diff --git a/tests/test_empty_timeframe_emission.py b/tests/test_empty_timeframe_emission.py new file mode 100644 index 00000000..43d197fa --- /dev/null +++ b/tests/test_empty_timeframe_emission.py @@ -0,0 +1,170 @@ +"""Timeframe argument typing and selection, with opt-in full-TU checks.""" + +import pytest + +from pineforge_codegen import transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.diagnostic_codes import render_diagnostic +from pineforge_codegen.errors import CompileError, Level, Phase +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser + + +def engine_call(function, argument): + if function == "change": + return ( + "tf_change(prev_bar_timestamp_, current_bar_.timestamp, " + + argument + ", syminfo_.timezone, syminfo_.session)" + ) + return "tf_to_seconds(" + argument + ")" + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("argument,lowered", [ + ('""', 'std::string("")'), + ('"240"', 'std::string("240")'), + ("frameChoice", "frameChoice"), +]) +def test_empty_plain_argument_selects_chart_and_nonempty_selects_argument(function, argument, lowered): + cpp = transpile( + '//@version=6\nstrategy("Chart timeframe selection")\n' + 'frameChoice = input.timeframe("")\n' + f"value = timeframe.{function}({argument})\n" + ) + expected = ( + "((" + lowered + ").empty() ? " + + engine_call(function, "script_tf_") + " : " + + engine_call(function, lowered) + ")" + ) + assert expected in cpp + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +def test_call_argument_is_evaluated_once_before_chart_selection(function): + cpp = transpile( + '//@version=6\nstrategy("Chart timeframe selection")\n' + 'frameChoice = input.timeframe("")\n' + 'chosenFrame() => frameChoice\n' + f"value = timeframe.{function}(chosenFrame())\n" + ) + assert "const std::string _pf_timeframe_arg = " in cpp + selected = "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" + assert engine_call(function, selected) in cpp + assert cpp.count("const std::string _pf_timeframe_arg = (chosenFrame());") == 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +def test_na_argument_uses_string_missing_value_before_chart_selection(function, named): + argument = "timeframe = na" if named else "na" + cpp = transpile( + '//@version=6\nstrategy("Missing timeframe")\n' + f"value = timeframe.{function}({argument})\n" + ) + missing = "na()" + expected = ( + "((" + missing + ").empty() ? " + + engine_call(function, "script_tf_") + " : " + + engine_call(function, missing) + ")" + ) + assert expected in cpp + assert "(na()).empty()" not in cpp + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("parameter", ["frameArg", "string frameArg"]) +def test_helper_string_parameter_keeps_chart_selection(function, parameter): + cpp = transpile( + '//@version=6\nstrategy("Helper timeframe")\n' + f"withFrame({parameter}) => timeframe.{function}(frameArg)\n" + 'value = withFrame("")\n' + ) + expected = ( + "((frameArg).empty() ? " + + engine_call(function, "script_tf_") + " : " + + engine_call(function, "frameArg") + ")" + ) + assert expected in cpp + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("argument,pine_type", [ + ("0", "int"), + ("17", "int"), + ("-3", "int"), + ("0.0", "float"), + ("1.25", "float"), + ("-2.5", "float"), +]) +def test_numeric_timeframe_is_refused_during_analysis(function, named, argument, pine_type): + supplied = f"timeframe = {argument}" if named else argument + source = ( + '//@version=6\nstrategy("Numeric timeframe")\n' + f"value = timeframe.{function}({supplied})\n" + ) + ast = Parser(Lexer(source).tokenize(), source=source).parse() + with pytest.raises(CompileError) as refused: + Analyzer(ast).analyze() + diagnostic, = refused.value.diagnostics + assert diagnostic.level == Level.ERROR + assert diagnostic.phase == Phase.ANALYZER + assert diagnostic.code == "PF-E2095" + assert diagnostic.args == { + "function_name": f"timeframe.{function}", + "name": "timeframe", + "argument_type": pine_type, + "parameter_type": "string", + } + assert diagnostic.message == ( + f"Cannot call 'timeframe.{function}' with an argument of type {pine_type} " + "for string parameter 'timeframe'." + ) + assert diagnostic.hint is None + assert render_diagnostic(diagnostic.code, diagnostic.args) == ( + diagnostic.message, diagnostic.hint, + ) + assert diagnostic.location.line == 3 + assert diagnostic.location.col == source.splitlines()[2].index(argument) + 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("optimization", ["-O0", "-O2"]) +def test_na_timeframe_full_translation_unit_compiles(function, named, optimization): + from tests._compile import run_emitted_tu + + argument = "timeframe = na" if named else "na" + cpp = transpile( + '//@version=6\nstrategy("Missing timeframe compilation")\n' + f"value = timeframe.{function}({argument})\n" + ) + driver = r''' +#include +#include +int main() { + std::vector bars(4); + for (std::size_t i = 0; i < bars.size(); ++i) { + bars[i].open = bars[i].high = bars[i].low = bars[i].close = 100.0 + i; + bars[i].volume = 1.0; + bars[i].timestamp = 1704067200000LL + static_cast(i) * 3600000LL; + } + GeneratedStrategy strategy; + strategy.run(bars.data(), bars.size(), "60", "60"); + if (!strategy.last_error().empty()) { + std::cerr << strategy.last_error() << "\n"; + return 1; + } + std::cout << "executed " << static_cast(strategy.value) << "\n"; +} +''' + output = run_emitted_tu( + cpp, driver, opt=optimization, + label=f"timeframe.{function}({argument})", + ) + # This row proves a real optimized build and execution. It records the + # observed value without settling the separate TradingView na-value check. + marker, observed = output.strip().split() + assert marker == "executed" + float(observed) + print(f"timeframe.{function}({argument}) {optimization}: {output.strip()}") diff --git a/tests/test_namespace_operator_semantic_facts.py b/tests/test_namespace_operator_semantic_facts.py new file mode 100644 index 00000000..9a95b520 --- /dev/null +++ b/tests/test_namespace_operator_semantic_facts.py @@ -0,0 +1,25 @@ +"""Valid operator operands must survive legacy namespace storage types.""" + +from pineforge_codegen import transpile + + +SIZING_LABEL_SOURCE = '''//@version=6 +strategy("Sizing label") +txt = "Sizing: " + strategy.cash +if barstate.islast + label.new(bar_index, high, txt) +''' + +ORDINARY_CONTROL_SOURCE = '''//@version=6 +strategy("Ordinary addition") +plot(close + open) +''' + + +def test_strategy_sizing_string_constant_still_transpiles(): + # Acceptance only: the old numeric emission is a separate value defect. + transpile(SIZING_LABEL_SOURCE) + + +def test_ordinary_numeric_operator_still_transpiles(): + transpile(ORDINARY_CONTROL_SOURCE) diff --git a/tests/test_namespace_sizing_consumers.py b/tests/test_namespace_sizing_consumers.py new file mode 100644 index 00000000..58ddd3df --- /dev/null +++ b/tests/test_namespace_sizing_consumers.py @@ -0,0 +1,241 @@ +"""Sizing constants keep their Pine values through live emitted consumers.""" + +import re + +import pytest + +from pineforge_codegen import signatures as sigs, transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.codegen.helpers import CPP_TEMPORARY_NAMES +from pineforge_codegen.errors import CompileError +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser +from pineforge_codegen.symbols import PineType, TypeSpec + + +SIZING_MEMBERS = ("fixed", "cash", "percent_of_equity") +HEADER = '//@version=6\nstrategy("Sizing consumers")\n' + + +def _source_body(cpp): + match = re.search( + r"void on_source_bar\([^)]*\) override \{(.*?)\n \}", + cpp, re.DOTALL, + ) + assert match is not None + return match.group(1) + + +def _engine_call(function, argument): + if function == "change": + return ( + "tf_change(prev_bar_timestamp_, current_bar_.timestamp, " + f"{argument}, syminfo_.timezone, syminfo_.session)" + ) + return f"tf_to_seconds({argument})" + + +def _live_timeframe_source(member, function, form): + argument = f"strategy.{member}" + declarations = "" + supplied = f"timeframe = {argument}" if form == "named" else argument + call = f"timeframe.{function}({supplied})" + if form in ("helper", "named-helper"): + supplied = "timeframe = frameArg" if form == "named-helper" else "frameArg" + declarations = f"withFrame(frameArg) => timeframe.{function}({supplied})\n" + call = f"withFrame({argument})" + elif form == "alias": + declarations = f"frameMode = {argument}\n" + call = f"timeframe.{function}(frameMode)" + elif form == "return": + declarations = f"chosenFrame() => {argument}\n" + call = f"timeframe.{function}(chosenFrame())" + value = f"{call} ? 1 : 0" if function == "change" else call + return ( + HEADER + declarations + f"sampleValue = {value}\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + ) + + +@pytest.mark.parametrize("member", SIZING_MEMBERS) +@pytest.mark.parametrize("function", ("change", "in_seconds")) +@pytest.mark.parametrize( + "form", ("direct", "named", "helper", "named-helper", "alias", "return"), +) +def test_sizing_timeframe_arguments_reach_live_string_lowering(member, function, form): + cpp = transpile(_live_timeframe_source(member, function, form)) + body = _source_body(cpp) + assert re.search(r"\bsampleValue\s*=", body) + assert re.search(r"if \([^;]*\bsampleValue\b", body) + assert 'strategy_entry(std::string("L"), true' in body + literal = f'std::string("{member}")' + if form in ("helper", "named-helper"): + assert f"withFrame({literal})" in body + assert re.search(r"\bwithFrame\(std::string frameArg\)", cpp) + assert _engine_call(function, "frameArg") in cpp + assert "(frameArg).empty()" in cpp + elif form == "alias": + assert f"frameMode = {literal};" in body + assert _engine_call(function, "frameMode") in body + assert "(frameMode).empty()" in body + elif form == "return": + assert re.search(r"\bstd::string chosenFrame\(", cpp) + assert f"return {literal};" in cpp + assert "const std::string _pf_timeframe_arg = (chosenFrame());" in body + assert _engine_call( + function, "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)", + ) in body + else: + assert _engine_call(function, literal) in body + assert f"({literal}).empty()" in body + assert not re.search(r"\(\s*(?:[+-]?\d+(?:\.\d*)?|true|false)\s*\)\.empty\(", cpp) + + +@pytest.mark.parametrize("member", SIZING_MEMBERS) +@pytest.mark.parametrize("consumer", ("concat", "equality", "selection", "collection")) +def test_sizing_strings_keep_their_type_in_other_live_consumers(member, consumer): + if consumer == "concat": + declaration = f'sampleName = "Sizing: " + strategy.{member}\n' + elif consumer == "equality": + declaration = ( + f'rightMode = input.string("{member}")\n' + f'sampleName = strategy.{member} == rightMode ? "equal" : "different"\n' + ) + elif consumer == "selection": + declaration = ( + "chooseMode = input.bool(true)\n" + f'sampleName = chooseMode ? strategy.{member} : "other"\n' + ) + else: + declaration = ( + f'modes = array.from(strategy.{member}, "other")\n' + "sampleName = array.get(modes, input.int(0))\n" + ) + cpp = transpile( + HEADER + declaration + "if barstate.islast\n" + " strategy.entry(sampleName, strategy.long)\n" + ) + body = _source_body(cpp) + assert f'std::string("{member}")' in body + assert "strategy_entry(sampleName, true" in body + assert re.search(r"\bstd::string sampleName\b", cpp) + if consumer == "collection": + assert "std::vector" in cpp + + +@pytest.mark.parametrize("member,host_value", (("fixed", 0), ("percent_of_equity", 1), ("cash", 2))) +def test_host_qty_boundary_preserves_codes_without_changing_pine_values(member, host_value): + cpp = transpile( + HEADER + f"modeName = strategy.{member}\n" + f'strategy.entry(modeName, strategy.long, qty=1, qty_type=strategy.{member})\n' + ) + body = _source_body(cpp) + assert f'modeName = std::string("{member}");' in body + assert f", {host_value})" in body + assert sigs.get_variable_type("strategy", member) == PineType.STRING + assert sigs.STRATEGY_VARIABLES[f"strategy.{member}"] == PineType.STRING + assert sigs.get_variable_type_spec("strategy", member) == TypeSpec.primitive("string") + + +@pytest.mark.parametrize("consumer", ("order", "risk")) +def test_computed_sizing_strings_convert_once_at_numeric_host_boundaries(consumer): + source = ( + HEADER + "chooseMode = input.bool(true)\n" + "chosenMode() => chooseMode ? strategy.cash : strategy.percent_of_equity\n" + ) + if consumer == "order": + source += 'strategy.entry("L", strategy.long, qty=1, qty_type=chosenMode())\n' + else: + source += ( + "strategy.risk.max_drawdown(10, chosenMode())\n" + 'strategy.entry("L", strategy.long)\n' + ) + body = _source_body(transpile(source)) + assert body.count("const std::string _pf_qty_type = (chosenMode());") == 1 + assert '_pf_qty_type == std::string("cash")' in body + assert '_pf_qty_type == std::string("percent_of_equity")' in body + assert "strategy_entry(" in body + if consumer == "risk": + assert "set_pine_risk_max_drawdown(" in body + + +@pytest.mark.parametrize("namespace,member", ( + ("strategy", "long"), ("strategy", "short"), + ("strategy.commission", "percent"), + ("strategy.commission", "cash_per_contract"), + ("strategy.commission", "cash_per_order"), + ("strategy.oca", "none"), ("strategy.oca", "cancel"), ("strategy.oca", "reduce"), + *(("display", member) for member in ( + "all", "none", "pane", "data_window", "status_line", "price_scale", + )), +)) +def test_legacy_constant_storage_is_not_a_semantic_operator_proof(namespace, member): + assert sigs.get_variable_type(namespace, member) != PineType.UNKNOWN + assert sigs.get_variable_type_spec(namespace, member) is None + + +@pytest.mark.parametrize("version", (5, 6)) +def test_authored_namespace_receiver_keeps_its_field_type(version): + if version == 5: + libraries = {"pftest/SizingReceiver/1": ( + '//@version=5\nlibrary("SizingReceiver")\n' + "export type Mode\n int cash\n" + "export readMode(Mode strategy) => strategy.cash\n" + )} + source = ( + HEADER + "import pftest/SizingReceiver/1 as L\n" + "sampleValue = L.readMode(L.Mode.new(3))\n" + 'sizingText = "Sizing: " + strategy.cash\n' + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + ) + cpp = transpile(source, libraries=libraries) + else: + cpp = transpile( + f'//@version={version}\nstrategy("Sizing receiver")\n' + "type Mode\n int cash\n" + "readMode(Mode strategy) => strategy.cash\n" + "sampleValue = readMode(Mode.new(3))\n" + 'sizingText = "Sizing: " + strategy.cash\n' + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + ) + body = _source_body(cpp) + assert ".cash" in cpp + assert re.search(r"sizingText\s*=.*cash", body) + assert "strategy_entry(" in body + + +def test_computed_qty_type_temporary_is_reserved(): + source = ( + HEADER + + 'qtyMode = input.string("cash", options = ["cash", "percent_of_equity"])\n' + 'strategy.entry("L", strategy.long, qty = 1, qty_type = qtyMode)\n' + ) + cpp = transpile(source) + assert "_pf_qty_type" in cpp + assert "_pf_qty_type" in CPP_TEMPORARY_NAMES + + +def test_ordinary_live_numeric_control_keeps_its_operands(): + body = _source_body(transpile( + HEADER + "sampleValue = close + open\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + )) + assert "(current_bar_.close + current_bar_.open)" in body + assert "sampleValue" in body + assert "strategy_entry(" in body + + +@pytest.mark.parametrize("function", ("change", "in_seconds")) +def test_open_numeric_domain_is_not_reported_as_a_guessed_float(function): + source = HEADER + f"value = timeframe.{function}(math.round(close))\n" + ast = Parser(Lexer(source).tokenize(), source=source).parse() + with pytest.raises(CompileError) as refused: + Analyzer(ast).analyze() + diagnostic, = refused.value.diagnostics + assert diagnostic.code == "PF-E2095" + assert diagnostic.args["argument_type"] == "number" + assert str(TypeSpec.primitive("number")) == "number" diff --git a/tests/test_reference_handle_lowering.py b/tests/test_reference_handle_lowering.py new file mode 100644 index 00000000..6a539ff1 --- /dev/null +++ b/tests/test_reference_handle_lowering.py @@ -0,0 +1,152 @@ +"""Reference values keep their storage types across fields and user calls.""" + +import re + +import pytest + +from pineforge_codegen import transpile +from tests._compile import compile_cpp + + +@pytest.mark.parametrize( + "kind,cpp_type", + [ + ("line", "Line"), + ("box", "Box"), + ("label", "Label"), + ("linefill", "Linefill"), + ("chart.point", "ChartPoint"), + ("polyline", "_PFPolyline"), + ], +) +def test_reference_field_na_uses_the_field_type(kind, cpp_type): + source = f'''//@version=6 +strategy("Reference field") +type Holder + {kind} drawingRef = na +var holderRef = Holder.new() +holderRef.drawingRef := na +''' + cpp = transpile(source) + assert f"{cpp_type} drawingRef" in cpp + assert f"_pf_udt_Holder.get(holderRef).drawingRef = {cpp_type}{{}};" in cpp + compile_cpp(cpp, label=f"reference-field-na-{kind}") + + +@pytest.mark.parametrize("element", ["float", "int", "string", "line", "polyline"]) +def test_array_field_na_default_has_collection_storage(element): + source = f'''//@version=6 +strategy("Collection field") +type Bag + array<{element}> xs = na +var bagRef = Bag.new() +bagRef.xs := array.new<{element}>() +plot(bagRef.xs.size()) +bagRef.xs := na +''' + cpp = transpile(source) + assert ".xs = na()" not in cpp + assert "_PFUdtRecord_Bag{" in cpp + compile_cpp(cpp, label=f"collection-field-na-{element}") + + +@pytest.mark.parametrize( + "kind,cpp_type,factory", + [ + ("line", "Line", "line.new(bar_index, low, bar_index, high)"), + ("box", "Box", "box.new(bar_index, high, bar_index, low)"), + ("label", "Label", 'label.new(bar_index, high, "marker")'), + ], +) +@pytest.mark.parametrize("typed_parameter", [True, False], ids=["typed", "untyped"]) +def test_handle_parameter_alias_and_tuple_are_values_at_each_call( + kind, cpp_type, factory, typed_parameter +): + hint = f"{kind} " if typed_parameter else "" + source = f'''//@version=6 +strategy("Handle values") +mark({hint}initialHandle, bool replaceHandle) => + aliasHandle = initialHandle + if replaceHandle + aliasHandle := {factory} + [aliasHandle, bar_index] +var {kind} storedHandleA = na +var {kind} storedHandleB = na +[returnedA, indexA] = mark(storedHandleA, close > open) +[returnedB, indexB] = mark(storedHandleB, close < open) +storedHandleA := returnedA +storedHandleB := returnedB +''' + cpp = transpile(source) + definitions = re.findall( + rf"std::tuple<{cpp_type}, int> mark(?:_cs\d+)?" + rf"\({cpp_type} initialHandle, bool replaceHandle\)", + cpp, + ) + assert definitions + assert f"std::tuple<{cpp_type}&" not in cpp + assert "storedHandleA = returnedA;" in cpp + assert "storedHandleB = returnedB;" in cpp + compile_cpp(cpp, label=f"handle-value-boundaries-{kind}-{typed_parameter}") + + +def test_box_field_can_bind_directly_to_a_user_parameter(): + source = '''//@version=6 +strategy("Field argument") +type Holder + box drawingRef = na +extend(box drawingRef) => + box.set_right(drawingRef, bar_index) +var holderRef = Holder.new() +if barstate.isfirst + holderRef.drawingRef := box.new(bar_index, high, bar_index, low) +extend(holderRef.drawingRef) +''' + cpp = transpile(source) + assert "extend(Box drawingRef)" in cpp + assert "extend(_pf_udt_Holder.read(holderRef).drawingRef)" in cpp + compile_cpp(cpp, label="box-field-user-parameter") + + +@pytest.mark.parametrize( + "deletion", + [ + "polyline priorHandle = done.shift()\npriorHandle.delete()", + "priorHandle = done.shift()\npriorHandle.delete()", + "done.shift().delete()", + ], + ids=["typed", "untyped", "chained"], +) +def test_polyline_array_element_keeps_its_handle_type(deletion): + source = '''//@version=6 +strategy("Polyline element") +var array done = array.new() +points = array.from(chart.point.from_index(bar_index, low), chart.point.from_index(bar_index, high)) +done.push(polyline.new(points)) +''' + deletion + "\n" + cpp = transpile(source) + assert "std::vector<_PFPolyline>" in cpp + assert "None(" not in cpp + assert "polyline priorHandle" not in cpp + compile_cpp(cpp, label="polyline-array-element") + + +def test_polyline_field_and_cast_preserve_typed_reads_and_delete(): + source = '''//@version=6 +strategy("Polyline field") +type Holder + polyline drawingRef = na +var holderRef = Holder.new() +points = array.from(chart.point.from_index(bar_index, low), chart.point.from_index(bar_index, high)) +holderRef.drawingRef := polyline.new(points = points) +polyline heldHandle = holderRef.drawingRef +heldHandle.delete() +polyline.delete(id = holderRef.drawingRef) +holderRef.drawingRef := polyline(x = na) +''' + cpp = transpile(source) + assert "_PFPolyline drawingRef" in cpp + assert "_pf_udt_Holder.read(holderRef).drawingRef" in cpp + assert "_pf_udt_Holder.get(holderRef).drawingRef = _PFPolyline{};" in cpp + assert "None(" not in cpp + compile_cpp(cpp, label="polyline-field-read-delete") diff --git a/tests/test_source_host_config_codegen.py b/tests/test_source_host_config_codegen.py index 7ea6a760..a1cd3a81 100644 --- a/tests/test_source_host_config_codegen.py +++ b/tests/test_source_host_config_codegen.py @@ -259,13 +259,26 @@ def test_risk_reassigned_type_reads_the_current_value( ] assert len(calls) == 1 # The flag must read t after its write, rather than use its initializer. + conversion = ( + r"\(\[&\]\(\) -> int \{ const std::string _pf_qty_type = " + r"\(t(?:\[0\])?\); " + r'if \(_pf_qty_type == std::string\("fixed"\)\) return 0; ' + r'if \(_pf_qty_type == std::string\("percent_of_equity"\)\) return 1; ' + r'if \(_pf_qty_type == std::string\("cash"\)\) return 2; ' + r'throw std::invalid_argument\("Unknown strategy sizing value"\); \}\(\)\)' + ) assert re.fullmatch( rf"set_pine_risk_{risk_func}\(\(double\)\(14\), " - r"\(\(t(?:\[0\])?\) == static_cast\(QtyType::PERCENT_OF_EQUITY\)\)\);", + rf"\(\({conversion}\) == static_cast\(QtyType::PERCENT_OF_EQUITY\)\)\);", calls[0], ) + assert ( + f'if (_pf_qty_type == std::string("{assigned}")) return {assigned_value};' + in calls[0] + ) write = re.search( - rf"\bt(?:\.update\({assigned_value}\)| = {assigned_value});", cpp, + rf'\bt(?:\.update\(std::string\("{assigned}"\)\)' + rf'| = std::string\("{assigned}"\));', cpp, ) assert write is not None assert write.start() < cpp.index(calls[0]) diff --git a/tests/test_timeframe_argument_types.py b/tests/test_timeframe_argument_types.py new file mode 100644 index 00000000..9c241aaf --- /dev/null +++ b/tests/test_timeframe_argument_types.py @@ -0,0 +1,155 @@ +"""Timeframe string parameters use semantic types before C++ conversion.""" + +import pytest + +from pineforge_codegen import transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.diagnostic_codes import render_diagnostic +from pineforge_codegen.errors import CompileError, Level, Phase +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser + + +HEADER = '//@version=6\nstrategy("Timeframe argument types")\n' +NON_STRINGS = [ + pytest.param("frames = input.int(14)\n", "frames", "int", id="integer-input"), + pytest.param("", "input.int(7)", "int", id="inline-integer-input"), + pytest.param("counter = 0\ncounter := counter + 1\n", "counter", "int", + id="reassigned-counter"), + pytest.param("", "syminfo.mintick", "float", id="member-value"), + pytest.param("", "timeframe.multiplier", "int", id="integer-timeframe-member"), + pytest.param("", "timeframe.isdaily", "bool", id="boolean-timeframe-member"), + pytest.param("", "strategy.closedtrades", "int", id="integer-strategy-member"), + pytest.param("", "barstate.isfirst", "bool", id="boolean-barstate-member"), + pytest.param("", "ta.sma(close, 3)", "float", id="indicator-result"), + pytest.param("", "input.float(2.25)", "float", id="float-input"), + pytest.param("", "true", "bool", id="boolean-literal"), + pytest.param("", "close > open", "bool", id="boolean-series"), + pytest.param("flag = input.bool(true)\n", "flag", "bool", id="boolean-input"), + pytest.param("flag = false\nflag := close > open\n", "flag", "bool", + id="reassigned-boolean"), + pytest.param("", "color.red", "color", id="color"), + pytest.param("frames = array.new(2, 1)\n", "frames", "array", + id="array"), + pytest.param("frames = matrix.new(1, 1, 2.0)\n", "frames", + "matrix", id="matrix"), + pytest.param("frames = map.new()\n", "frames", + "map", id="map"), + pytest.param('type FrameBox\n string frame\n' + 'boxValue = FrameBox.new("60")\n', "boxValue", "FrameBox", + id="object"), +] + + +def _assert_type_diagnostic(diagnostic, function, pine_type): + assert diagnostic.level == Level.ERROR + assert diagnostic.code == "PF-E2095" + assert diagnostic.args == { + "function_name": f"timeframe.{function}", + "name": "timeframe", + "argument_type": pine_type, + "parameter_type": "string", + } + assert diagnostic.hint is None + assert render_diagnostic(diagnostic.code, diagnostic.args) == ( + diagnostic.message, diagnostic.hint, + ) + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("scope", [ + "direct", "helper", "helper-local", "named-helper", "typed-helper", +]) +@pytest.mark.parametrize("conversion", [False, True], ids=["alone", "bool-to-text"]) +@pytest.mark.parametrize("declarations,argument,pine_type", NON_STRINGS) +def test_non_string_timeframes_are_refused_by_semantic_type( + function, named, scope, conversion, declarations, argument, pine_type, +): + source = HEADER + declarations + if conversion: + source += "unrelatedString = str.tostring(close > open)\n" + value = argument if scope == "direct" else "frameArg" + if scope == "helper-local": + value = "localFrame" + supplied = f"timeframe = {value}" if named else value + call = f"timeframe.{function}({supplied})" + if scope == "direct": + source += f"value = {call}\n" + elif scope in ("helper", "named-helper", "typed-helper"): + parameter = f"{pine_type} frameArg" if scope == "typed-helper" else "frameArg" + actual = f"frameArg = {argument}" if scope == "named-helper" else argument + source += f"withFrame({parameter}) => {call}\nvalue = withFrame({actual})\n" + else: + source += ( + "withFrame(frameArg) =>\n" + " localFrame = frameArg\n" + f" {call}\n" + f"value = withFrame({argument})\n" + ) + ast = Parser(Lexer(source).tokenize(), source=source).parse() + with pytest.raises(CompileError) as refused: + Analyzer(ast).analyze() + diagnostic, = refused.value.diagnostics + _assert_type_diagnostic(diagnostic, function, pine_type) + assert diagnostic.phase == Phase.ANALYZER + line, = [(index, line) for index, line in enumerate(source.splitlines(), 1) + if call in line] + assert diagnostic.location.line == line[0] + assert diagnostic.location.col == line[1].index(supplied, line[1].index(call)) + ( + len("timeframe = ") if named else 0 + ) + 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("conversion", [False, True], ids=["alone", "bool-to-text"]) +@pytest.mark.parametrize("body,pine_type", [ + ("input.int(14)", "int"), + ("syminfo.mintick", "float"), + ("close > open", "bool"), +]) +def test_helper_return_cannot_be_converted_to_a_timeframe_string( + function, named, conversion, body, pine_type, +): + source = HEADER + if conversion: + source += "unrelatedString = str.tostring(close > open)\n" + source += f"chosenFrame() => {body}\n" + supplied = "timeframe = chosenFrame()" if named else "chosenFrame()" + source += f"value = timeframe.{function}({supplied})\n" + with pytest.raises(CompileError) as refused: + transpile(source) + diagnostic, = refused.value.diagnostics + _assert_type_diagnostic(diagnostic, function, pine_type) + assert diagnostic.phase in (Phase.ANALYZER, Phase.CODEGEN) + assert diagnostic.location.line == len(source.splitlines()) + assert diagnostic.location.col == source.splitlines()[-1].index("chosenFrame()") + 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("argument,pine_type", [ + ("input.int(14)", "int"), + ("syminfo.mintick", "float"), + ("true", "bool"), +]) +def test_emitter_checks_type_before_implicit_string_conversion( + monkeypatch, function, named, argument, pine_type, +): + # An internal caller can reach emission after bypassing an analyzer guard. + # The emitter must still refuse the semantic type before bool text lowering. + monkeypatch.setattr(Analyzer, "_check_timeframe_string_arguments", + lambda *args, **kwargs: None, raising=False) + supplied = f"timeframe = {argument}" if named else argument + source = ( + HEADER + "unrelatedString = str.tostring(close > open)\n" + f"value = timeframe.{function}({supplied})\n" + ) + with pytest.raises(CompileError) as refused: + transpile(source) + diagnostic, = refused.value.diagnostics + _assert_type_diagnostic(diagnostic, function, pine_type) + assert diagnostic.phase == Phase.CODEGEN + assert diagnostic.location.line == 4 + assert diagnostic.location.col == source.splitlines()[3].index(argument) + 1 diff --git a/tests/test_timeframe_string_tu.py b/tests/test_timeframe_string_tu.py new file mode 100644 index 00000000..fb67f87b --- /dev/null +++ b/tests/test_timeframe_string_tu.py @@ -0,0 +1,77 @@ +"""Build and execute complete string-argument TUs at distinct optimizations.""" + +import pytest + +from pineforge_codegen import transpile +from tests._compile import run_emitted_tu + + +@pytest.mark.parametrize("optimization", ["-O0", "-O2"]) +def test_string_timeframe_controls_build_and_run(optimization): + source = ( + '//@version=6\nstrategy("String timeframe compilation")\n' + 'frameChoice = input.timeframe("")\n' + 'string missingFrame = na\n' + 'unrelatedString = str.tostring(close > open)\n' + 'chosenFrame() => frameChoice\n' + 'withSeconds(frameArg) => timeframe.in_seconds(frameArg)\n' + 'withChange(frameArg) => timeframe.change(timeframe = frameArg)\n' + 'typedSeconds(string frameArg) => timeframe.in_seconds(timeframe = frameArg)\n' + 'typedChange(string frameArg) => timeframe.change(frameArg)\n' + ) + arguments = { + "explicit": '"240"', + "empty": '""', + "variable": "frameChoice", + "return": "chosenFrame()", + "formatted": "str.tostring(60)", + "hostString": "strategy.position_entry_name", + "typedMissing": "missingFrame", + } + names = [] + for function in ("in_seconds", "change"): + for kind, argument in arguments.items(): + for named in (False, True): + name = f"{function}_{kind}_{'named' if named else 'positional'}" + names.append(name) + supplied = f"timeframe = {argument}" if named else argument + source += f"{name} = timeframe.{function}({supplied})\n" + for helper in ("withSeconds", "withChange", "typedSeconds", "typedChange"): + for kind, argument in (("empty", '""'), ("explicit", '"240"')): + name = f"{helper}_{kind}" + names.append(name) + source += f"{name} = {helper}({argument})\n" + driver = r''' +#include +#include +int main() { + std::vector bars(5); + for (std::size_t i = 0; i < bars.size(); ++i) { + bars[i].open = bars[i].high = bars[i].low = bars[i].close = 100.0 + i; + bars[i].volume = 1.0; + bars[i].timestamp = 1704067200000LL + static_cast(i) * 3600000LL; + } + GeneratedStrategy strategy; + strategy.run(bars.data(), bars.size(), "60", "60"); + if (!strategy.last_error().empty()) { + std::cerr << strategy.last_error() << "\n"; + return 1; + } +''' + for name in names: + driver += ( + f' std::cout << "{name} " << ' + f'static_cast(strategy.{name}) << "\\n";\n' + ) + driver += "}\n" + output = run_emitted_tu( + transpile(source), driver, opt=optimization, label="string timeframe controls", + ) + # Value parity has its own replay oracles. This test's target is building + # every control and executing it through the real generated strategy. + rows = [line.split() for line in output.splitlines()] + assert [row[0] for row in rows] == names + assert all(len(row) == 2 for row in rows) + for _, value in rows: + float(value) + print(f"string timeframe controls {optimization}:\n{output}", end="") diff --git a/tests/test_udt_drawing_field_cleanup.py b/tests/test_udt_drawing_field_cleanup.py index 6818324b..f14f94a8 100644 --- a/tests/test_udt_drawing_field_cleanup.py +++ b/tests/test_udt_drawing_field_cleanup.py @@ -6,7 +6,8 @@ ``std::vector upln;``), and ``m.tag := label.new(...)`` / ``x = m.tag`` become real field read/writes against the shared per-type arena. -(``table``/``polyline`` fields are still dropped — they have no C++ type.) +``polyline`` fields retain opaque handles without rendering; ``table`` fields +are still dropped. """ from pineforge_codegen import transpile