Issue 10 | Phase A: Parallel Development of Computing Engine
app.js (created by logic-dev) β Shunting-yard Calculation Engine
// Operator precedence
const PRECEDENCE = {
'+': 1, '-': 1,
'Γ': 2, '*': 2, 'Γ·': 2, '/': 2,
'%': 3, 'unary-': 4
};
// Lexical analysis
function tokenize(expression) {
// "1+2*3" β ['1', '+', '2', '*', '3']
}
// Shunting-yard algorithm β RPN
function parseExpression(tokens) {
// ['1', '+', '2', '*', '3'] β ['1', '2', '3', '*', '+']
}
// RPN evaluation
function evaluate(rpn) {
// Stack-based evaluation, returns "Error" on division by zero
}
// Real-time preview (incomplete expressions are not calculated)
function previewResult(expression) {
// Mismatched parentheses β returns ''
// Ends with an operator β returns ''
}
// Input helpers (pure functions, no DOM dependency)
function appendNumber(current, digit) { /* ... */ }
function appendOperator(current, op) { /* ... */ }
function appendDecimal(current) { /* ... */ }
function toggleSign(current) { /* ... */ }
// DOM binding layer (separated from pure functions)
function initCalculator() {
initElements();
loadModePreference();
bindButtonEvents();
bindKeyboardEvents();
bindModeToggle();
updateDisplay();
}
// Export (Node.js test compatibility)
if (typeof module !== 'undefined' && module.exports) {
module.exports = { tokenize, parseExpression, evaluate, /* ... */ };
}
Architectural Key: Pure Functions and DOM Separation
βββββββββββββββββββββββββββββββββββ
β Pure Function Layer (DOM-independent) β
β tokenize β parseExpression β evaluate β
β appendNumber, appendOperator, ... β
β β
Independently testable (Node.js) β
ββββββββββββββββ¬βββββββββββββββββββ
β
ββββββββββββββββΌβββββββββββββββββββ
β DOM Interaction Layer β
β initCalculator, handleButtonClick β
β bindKeyboardEvents, toggleMode β
β β οΈ Requires browser environment for testing β
βββββββββββββββββββββββββββββββββββ