You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: langs/calc/README.md
+8-24Lines changed: 8 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,8 @@ The ```rules``` method uses byte-level pattern matching to create tokens for num
27
27
28
28
The wellformed definition adds new ```Operation``` token types that have 2 ```Expression``` children. Also, ```Expression``` tokens have a new definition, being able to wrap both ```Number``` tokens as well as ```Operation``` tokens.
29
29
30
-
```mulDivPass``` and ```addSubPass``` both create nested expressions and splicing the old ```Op``` tokens that were previously defined. Both methods do this by pattern matching on the sequence of ```(Expression, Op, Expression)``` and replacing this sequence with
30
+
The passes are a sequence of transformations defined in the `passes` field, in this consisting of 2 binary operation parsing passes.
31
+
```MulDivPass``` and ```AddSubPass``` both create nested expressions and splicing the old ```Op``` tokens that were previously defined. Both methods do this by pattern matching on the sequence of ```(Expression, Op, Expression)``` and replacing this sequence with
31
32
32
33
```
33
34
Expression(
@@ -38,38 +39,21 @@ Expression(
38
39
)
39
40
```
40
41
41
-
```mulDivPass``` is executed before ```addSubPass``` to create precedence, allowing multiplication and division operations to be nested deeper than addition and subtraction operations in the AST.
42
+
```MulDivPass``` is executed before ```AddSubPass``` to create precedence, allowing multiplication and division operations to be nested deeper than addition and subtraction operations in the AST.
42
43
43
44
44
45
### 4. ```CalcEvaluator.scala```
45
46
```CalcEvaluator``` simplifies the AST and computes the value of the arithmetic expression.
46
47
47
48
The wellformed definition is imported from ```package.scala```, picking up with the AST structure of where ```CalcParser``` left off.
48
-
49
-
```simplifyPass``` splices all expressions repeatedly until there's only a single expression node at the top of the AST structure. The pass uses a bottom-up strategy to begin with simplifying the base-case expressions with no nesting as it goes up the AST.
50
-
51
-
The pass sequence pattern matches on
52
-
53
-
```
54
-
Expression(
55
-
Operation(
56
-
Expression(
57
-
Number
58
-
),
59
-
Expression(
60
-
Number
61
-
)
62
-
)
63
-
)
64
-
```
65
-
66
-
and replaces the sequence with ```Expression(Number)```. The remaining ```Expression``` token at the end of the pass contains the value of arithmetic expression.
67
-
68
-
```removeLayerPass``` splices the ```Expression``` token at the top of the AST and replaces it with the ```Number``` token that was wrapped inside. Pattern matching is done on the sequence of ```Expression(Number)``` and replaces it with just ```Number```.
49
+
Each pass is annotated with its own input / output grammars, expressed as changes to the previous pass's output grammar.
50
+
-`ConstantsPass` converts each number to a native Scala `Int` for easier arithmetic, showing an example of the `Node.Embed` feature.
51
+
-`EvaluatorPass` is a ruleset that describes basic arithmetic evaluation, which will fold a wellformed tree into a single node of the form `Expression(Node.Embed[Int](???))`.
52
+
-`StripExpressionPass` simply cleans up the previous pass's tree, leaving just `Node.Embed[Int](???)` containing the result of evaluating the expression.
69
53
70
54
71
55
## Usage
72
-
To learn how to use the calculator, ```CalcReader.test.scala``` contains methods (```parse```, ```read```, ```evaluate```) that execute the different components of the calculator.
56
+
To learn how to use the calculator, ```package.test.scala``` contains methods (```parse```, ```read```, ```evaluate```) that execute the different components of the calculator.
0 commit comments