Download this file
| 1 | package calc |
| 2 | |
| 3 | // Inspection is what a parsed formula asks of this engine: whether |
| 4 | // every function it calls is one evalCall can dispatch, and whether |
| 5 | // any of them reads the clock. |
| 6 | // |
| 7 | // Import needs both. A formula that parses but names a function this |
| 8 | // engine does not have evaluates to #NAME?, so keeping it would replace |
| 9 | // a workbook's value with an error nobody typed; it has to be told |
| 10 | // apart before it is written, not discovered afterwards. And a formula |
| 11 | // that reads the clock cannot be compared with the value a workbook |
| 12 | // saved for it, because the two were computed at different moments. |
| 13 | type Inspection struct { |
| 14 | // Unknown is true when some call names a function no table has. |
| 15 | Unknown bool |
| 16 | // Volatile is true when some call reads the recalculation clock |
| 17 | // (NOW, TODAY). |
| 18 | Volatile bool |
| 19 | } |
| 20 | |
| 21 | // Inspect walks n and every node beneath it. |
| 22 | func Inspect(n Node) Inspection { |
| 23 | var in Inspection |
| 24 | inspect(n, &in) |
| 25 | return in |
| 26 | } |
| 27 | |
| 28 | func inspect(n Node, in *Inspection) { |
| 29 | switch t := n.(type) { |
| 30 | case *Call: |
| 31 | _, ordinary := funcTable[t.Name] |
| 32 | _, clock := clockFuncTable[t.Name] |
| 33 | _, node := nodeFuncTable[t.Name] |
| 34 | if !ordinary && !clock && !node { |
| 35 | in.Unknown = true |
| 36 | } |
| 37 | if clock { |
| 38 | in.Volatile = true |
| 39 | } |
| 40 | for _, a := range t.Args { |
| 41 | inspect(a, in) |
| 42 | } |
| 43 | case *Binary: |
| 44 | inspect(t.Left, in) |
| 45 | inspect(t.Right, in) |
| 46 | case *Unary: |
| 47 | inspect(t.X, in) |
| 48 | } |
| 49 | } |
| 50 | |