5.5 KiB
5.5 KiB
Project rules — RPN Virtual Calculator
- Build a browser-friendly RPN calculator as a JavaScript class, currently implemented in
src/rpn-calculator.js. - Keep code names, categories, and API identifiers in English.
- Keep the public calculator API centered on the generic methods
push,pop,clear,swap(index1, index2),remove(index),edit(index),isValidIndex(index),input(command), andcommand(name, ...args). inputValuemust remain a string andisEditingmust remain a boolean.- Keep constructor options aligned with the current engine:
maxSize(default2048)base(default10, accepted range2..16)angleMode(degby default; alsoradandgrad)enabledCommands
- Available constants are
piande. - Supported operations must stay centralized in one dictionary containing at least:
argCountcategoryaliasesexecute
- Allowed operation categories are limited to
Stack,Arithmetic, andTrigonometry. - The engine currently exposes static helpers for category discovery:
getOperationCategories()andgetOperationsByCategory(). - The instance currently exposes
getOperationsByCategory()andgetConstants()helpers in addition to the generic API. - Preserve browser and CommonJS exports for
RpnCalculator.
Supported commands
- Current commands:
add,sub,mul,div,mod,pow,sqr,neg,sqrt,recip,sin,cos,tan,asin,acos,atan,log,ln,dup,drop,swap,clear,enter
- Current aliases include:
+,-,*,/,%,^,y^x,1/x
- Existing extra alias also present in code:
sqrt(x)forsqrt
Behavior rules
modis the percentage operator:a b % => (a * b) / 100.divandrecipmust throwDivision by zeroon zero divisors.sqrt,asin,acos,log, andlnmust throw explicit domain errors.logusesMath.log10.lnusesMath.log.- Trigonometric functions must support
deg,rad, andgrad. - Direct trigonometric functions convert input angles with
toRadians(...). - Inverse trigonometric functions convert results back using the current angle mode via
toDegrees(...). - The engine rounds formatted numeric results to 12 decimal places and normalizes
-0to0. inputValuemust remain a string to preserve future hexadecimal-style input support.parseInputValue(...)currently usesNumber(...)in base 10 andparseInt(..., base)for other bases.input(command)currently accepts single-character numeric editing input, including0-9,A-F,a-f,+,-, and..command(name, ...args)currently resolves aliases, supports constants, commits pending input before execution, checksenabledCommands, and throws clearUnknown command,Command disabled,Stack overflow,Stack underflow,Invalid stack index,Invalid number, andInvalid input valueerrors where appropriate.
Demo rules
- The active browser demo lives under
samples/dev/. samples/dev/index.htmlis the demo entry page.samples/dev/index.csscontains the calculator visual theme.samples/dev/index.jscontains demo-side presentation helpers and UI logic.- The demo currently exposes:
- a stack display with four visible lines
- a main display/status area
- a visible angle mode indicator
- an angle mode selector for
deg,rad, andgrad - status pills showing
inputValueandisEditing - grouped command panels for
Stack,Arithmetic,Trigonometry, andConstants - an error display area
- The demo may present user-facing labels such as
+,−,×,÷,y^x,1/x, andx²while still using English command identifiers internally. - The example HTML groups buttons by
Stack,Arithmetic, andTrigonometry, plus a dedicatedConstantssection. - Demo buttons are wired through demo helpers that eventually call
command(...)for calculator commands. - The demo default angle mode is degrees.
- Keyboard support in the demo must remain consistent with the displayed help text.
- The current demo keyboard behavior supports:
- digits and decimal point
- numpad digits and numpad arithmetic keys
Enterto validate input or toggle stack-item move modeBackspaceto edit input or drop from the stackDeleteto clearEscapeto cancel editing, cancel move mode, or clear selectionArrowUp/ArrowDownto navigate or move selected stack itemsArrowRightfor swap+,-,*,/,%,^q,n,r,i,g,l,s,c,S,Cx,y,z,tto select visible stack registers
- The demo currently implements stack selection and stack-item move mode in its own UI logic using the public stack methods.
- The demo currently duplicates X on
enterwhen not editing, matching classic RPN-style behavior in the UI layer.
File references
samples/dev/index.htmlreferences../../src/rpn-calculator.jsas the calculator engine used by the demo.- Keep the demo aligned with the public calculator API exposed by
src/rpn-calculator.js. README.mdis currently outdated and duplicated in places; if documentation work is done later, align it with the actual engine and demo behavior.
Maintenance
- Re-read
src/rpn-calculator.jsandsamples/dev/before updating these rules after project changes. - Keep this file updated after each project change using the provided editing tools.
- When changing the demo UI, keyboard help, exported API, command metadata, or calculator behavior, update these rules so they continue to match the current project behavior.