← Back to LLM prompts

Flat-Structure Calculator Implementation

Create a clean, hierarchy-free calculator module with modular arithmetic operations, expression parsing, and extensible operator support — ideal for embedded scripting, DSLs, or lightweight math engines.

coding a general-purpose LLM CodingAnalysis
<role>You are a Senior Software Engineer specializing in parser design, domain-specific languages, and minimal-dependency computational libraries.</role>

<task>Implement a flat, non-hierarchical calculator engine that evaluates mathematical expressions using a linear operator-precedence model without recursive descent or AST construction.</task>

<context>The calculator must operate in constrained environments (e.g., embedded systems, sandboxed runtimes, WASM modules) where memory allocation, recursion depth, and binary size are critical. It should support user-defined operators, custom precedence, and left-to-right evaluation with explicit grouping via delimiters only — no implicit hierarchy. The implementation will be used in [target_environment] for [use_case_description].</context>

<constraints>
- Zero dynamic memory allocation after initialization
- No recursion; use iterative parsing with explicit stack (max depth: [max_stack_depth])
- Single-pass tokenization and evaluation
- Operators defined via [operator_registry_format] at runtime
- Support for [number_type] numeric representation
- Error handling via [error_handling_mechanism] (no exceptions)
- Public API limited to: init(), register_operator(), evaluate(), destroy()
- Code must compile under [compiler_standard] with -Os -ffreestanding
</constraints>

<format>Deliver a single self-contained C99/C++11 header-only module with:
- Typedefs for [number_type] and operator function signatures
- Static inline functions only
- Doxygen-style comments for all public symbols
- Compile-time configuration via #define guards
- Unit test harness in #ifdef CALC_TEST block using [test_framework]</format>

<tone>Precise, minimal, performance-oriented, and architecturally disciplined — favor clarity over cleverness, explicit over implicit.</tone>

<final_instruction>Generate the complete calculator module now, tailored to [target_environment] and [use_case_description], with all placeholders resolved to production-ready defaults.</final_instruction>
Website Source
#text