summaryrefslogtreecommitdiff
path: root/docs/design.org
diff options
context:
space:
mode:
Diffstat (limited to 'docs/design.org')
-rw-r--r--docs/design.org6
1 files changed, 5 insertions, 1 deletions
diff --git a/docs/design.org b/docs/design.org
index 0e3a477..d813078 100644
--- a/docs/design.org
+++ b/docs/design.org
@@ -1,5 +1,5 @@
#+TITLE: 7Units Design Document
-#+SUBTITLE: For version 0.4.0
+#+SUBTITLE: For version 0.5.0-alpha.2
#+DATE: 2022 July 8
#+LaTeX_HEADER: \usepackage[a4paper, lmargin=25mm, rmargin=25mm, tmargin=25mm, bmargin=25mm]{geometry}
#+LaTeX_HEADER: \usepackage{xurl}
@@ -132,12 +132,16 @@
~ExpressionParser~ has a parameterized type ~T~, which represents the type of the value used in the expression. The expression parser currently only supports one type of value per expression; in the expressions used by 7Units numbers are treated as a kind of unit or prefix. Operators are represented by internal types; the system distinguishes between unary operators (those that take a single value, like negation) and binary operators (those that take 2 values, like +, -, * or /).
+ There is one exception to this rule - users are allowed to create "numeric operators" that take one argument of type ~T~ and one numeric argument. This is intended for exponentation, but could also be used for vector scaling.
+
Expressions are parsed in 2 steps:
1. Convert the expression to [[https://en.wikipedia.org/wiki/Reverse_Polish_notation][Reverse Polish Notation]], where operators come *after* the values they operate on, and brackets and the order of operations are not necessary. For example, "2 + 5" becomes "~2 5 +~", "(1 + 2) * 3" becomes "~1 2 + 3 *~" and the example expression earlier becomes "~2 m * 30 J * N / + 8 s * *~". This makes it simple to evaluate - early calculators used RPN for a good reason!
2. Evaluate the RPN expression. This can be done simply with a for loop and a stack. For each token in the expression, the progam does the following:
- if it is a number or unit, add it to the stack.
+ (the dimension parser adds numbers to a separate stack for type safety, as numbers cannot be stored as a dimension type like they can with units.)
- if it is a unary operator, take one value from the stack, apply the operator to it, and put the result into the stack.
- if it is a binary operator, take two values from the stack, apply the operator to them, and put the result into the stack.
+ - if it is a numeric operator, take one value from the stack and one number from the numeric stack, apply to the operator to the two, and put the result in the regular stack.
After evaluating the last token, there should be one value left in the stack - the answer. If there isn't, the original expression was malformed.
** Math Classes
There are two simple math classes in 7Units: