research on implementation
the best implementation seems to be [[AST]]
suggested [[database]] structure could be:
CREATE TABLE Calculations (
calculation_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
expression JSON NOT NULL,
result VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES Users(user_id)
);
INSERT INTO Calculations (user_id, expression, result)
VALUES (1, '{
"type": "BinaryExpression",
"operator": "-",
"left": {
"type": "BinaryExpression",
"operator": "*",
"left": { "type": "Literal", "value": 3 },
"right": {
"type": "BinaryExpression",
"operator": "+",
"left": { "type": "Literal", "value": 4 },
"right": { "type": "Literal", "value": 5 }
}
},
"right": {
"type": "FunctionCall",
"name": "sqrt",
"arguments": [{ "type": "Literal", "value": 16 }]
}
}', '23');
Running the expressions
python
JS using math.js evaluate()
Last updated