File: tacky.go

package info (click to toggle)
golang-github-blynn-nex 0.0.0%2Bgit.2021.03.30.1a3320dab9-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: yacc: 60; makefile: 22; sh: 4
file content (284 lines) | stat: -rw-r--r-- 5,555 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Tacky.
//
// See README.

package main

import (
  "fmt"
  "os"
  "strconv"
  "strings"
)

type Expr struct {
  op string
  s string
  kid []*Expr
}

func (e *Expr) AddKid(kid *Expr) {
  e.kid = append(e.kid, kid)
}

func NewExpr(op, s string) *Expr {
  return &Expr{op, s, nil}
}

func NewOp(op string, a *Expr, b *Expr) *Expr {
  return &Expr{op, "", []*Expr{a, b}}
}

func NewFun(op string, args *Expr) *Expr {
  s := strings.TrimRight(op, "(")
  e := NewExpr(s, s)
  e.kid = args.kid
  return e
}

type formLine struct {
  id string
  desc string
  expr *Expr
  cached bool
  n int64
}

type form struct {
  id string
  lines map[string]*formLine
  sortedIds []string
  wholeDollarsOnly bool
}

type Tacky struct {
  forms map[string]*form
  curForm *form
  curLine string
  curFlag string
  sortedIds []string
  wholeDollarsOnly bool
}

func (tax *Tacky) Expr(e *Expr) {
  if tax.curLine == "" {
    if tax.curFlag == "" {
      panic("assignment expected")
    }
    if e.op != "ID" {
      panic("flag must be 0 or 1")
    }
    switch e.s {
    case "1":
      tax.wholeDollarsOnly = true
    case "0":
      tax.wholeDollarsOnly = false
    default:
      panic("flag must be 0 or 1")
    }
    tax.curFlag = ""
    return
  }
  form := tax.curForm
  line, ok := form.lines[tax.curLine]
  if !ok {
    panic("BUG: missing line " + tax.curLine)
  }
  if line.expr == nil {
    line.expr = e
    return
  }
  line.expr = NewOp("+", line.expr, e)
}

func (tax *Tacky) Assign(s string) {
  form := tax.curForm
  s = strings.TrimRight(s, "=")
  v := strings.SplitN(s, " ", 2)
  id := v[0]
  if form == nil {
    if id != "whole_dollars_only" {
      panic("no such flag: " + id)
    }
    tax.curFlag = id
    return
  }
  desc := ""
  if len(v) == 2 {
    desc = v[1]
  }
  if _, ok := form.lines[id]; ok {
    panic("duplicate line definition: " + id)
  }
  form.lines[id] = &formLine{id, strings.TrimSpace(desc), nil, false, 0}
  form.sortedIds = append(form.sortedIds, id)
  tax.curLine = id
}

func (tax *Tacky) BeginForm(line string) {
  if tax.curForm != nil {
    panic("nested form")
  }
  id := strings.TrimSpace(strings.TrimRight(line, "{"))
  if _, ok := tax.forms[id]; ok {
    panic("duplicate form: " + id)
  }
  tax.curForm = &form{id, make(map[string]*formLine), nil, tax.wholeDollarsOnly}
  tax.curLine = ""
  tax.sortedIds = append(tax.sortedIds, id)
  tax.forms[id] = tax.curForm
}

func (tax *Tacky) EndForm() {
  tax.curForm = nil
  tax.curLine = ""
}

func atoi(s string) int64 {
  n, e := strconv.Atoi(s)
  if e != nil {
    panic(e)
  }
  return int64(n)
}

func clip(a int64) int64 {
  if a < 0 {
    return 0
  }
  return a
}

func min(a, b int64) int64 {
  if a < b {
    return a
  }
  return b
}

func max(a, b int64) int64 {
  if a > b {
    return a
  }
  return b
}

func (tax *Tacky) Eval(e *Expr) int64 {
  if e == nil {  // Undefined.
    return 0
  }
  switch e.op {
  case "$":
    s := strings.TrimLeft(e.s, "$")
    v := strings.Split(s, ".")
    if len(v) == 2 {
      return atoi(v[0]) * 100 + atoi(v[1])
    }
    if len(v) != 1 {
      panic("bad money: " + e.s)
    }
    return atoi(s) * 100
  case "ID":
    if e.s == "0" {
      return 0
    }
    return tax.Get(e.s)
  case "+":
    return tax.Eval(e.kid[0]) + tax.Eval(e.kid[1])
  case "-":
    return tax.Eval(e.kid[0]) - tax.Eval(e.kid[1])
  case "*":
    if e.kid[1].op == "%" {
      v := strings.Split(strings.TrimRight(e.kid[1].s, "%"), ".")
      p := atoi(v[0])
      q := int64(100)
      if len(v) == 2 {
        for _ = range v[1] {
          p *= 10
          q *= 10
        }
        p += atoi(v[1])
      } else if len(v) != 1 {
        panic("malformed percentage")
      }
      return (tax.Eval(e.kid[0]) * p + q / 2) / q
    }
    return tax.Eval(e.kid[0]) * tax.Eval(e.kid[1])
  case "/":
    return tax.Eval(e.kid[0]) / tax.Eval(e.kid[1])
  case "XREF":
    v := strings.Split(e.s, ":")
    return tax.XGet(strings.TrimLeft(v[0], "["), strings.TrimRight(v[1], "]"))
  case "clip":
    if len(e.kid) != 1 {
      panic("bad arg count")
    }
    return clip(tax.Eval(e.kid[0]))
  case "min":
    if len(e.kid) != 2 {
      panic("bad arg count")
    }
    return min(tax.Eval(e.kid[0]), tax.Eval(e.kid[1]))
  case "max":
    if len(e.kid) != 2 {
      panic("bad arg count")
    }
    return max(tax.Eval(e.kid[0]), tax.Eval(e.kid[1]))
  }
  panic("no such op: " + e.op)
}

func (tax *Tacky) Get(id string) int64 {
  line, ok := tax.curForm.lines[id]
  if !ok {
    panic("no such line: " + id)
  }
  if line.cached {
    return line.n
  }
  line.cached = true
  line.n = tax.Eval(line.expr)
  if tax.curForm.wholeDollarsOnly {
    if (line.n >= 0) {
      line.n = (line.n + 50) / 100 * 100
    } else {
      line.n = (line.n - 50) / 100 * 100
    }
  }
  return line.n
}

func (tax *Tacky) XGet(id, line string) int64 {
  form, ok := tax.forms[id]
  if !ok {
    panic("no such form: " + id)
  }
  orig := tax.curForm
  tax.curForm = form
  n := tax.Get(line)
  tax.curForm = orig
  return n
}

func dollarize(cents int64) string {
  if cents < 0 {
    return "(" + dollarize(-cents) + ")"
  }
  return fmt.Sprintf("$%d.%02d", cents/100, cents%100)
}

func main() {
  tax := new(Tacky)
  tax.forms = make(map[string]*form)
  yyParse(NewLexerWithInit(os.Stdin, func(y *Lexer) { y.p = tax }))

  for _, id := range tax.sortedIds {
    f := tax.forms[id]
    tax.curForm = f
    fmt.Println(f.id)
    for _, id := range f.sortedIds {
      fmt.Println(id, f.lines[id].desc, dollarize(tax.Get(id)))
    }
    fmt.Println()
  }
}