| 1 |
// Tacky. |
| 2 |
// |
| 3 |
// See README. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"strconv" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
type Expr struct { |
| 15 |
op string |
| 16 |
s string |
| 17 |
kid []*Expr |
| 18 |
} |
| 19 |
|
| 20 |
func (e *Expr) AddKid(kid *Expr) { |
| 21 |
e.kid = append(e.kid, kid) |
| 22 |
} |
| 23 |
|
| 24 |
func NewExpr(op, s string) *Expr { |
| 25 |
return &Expr{op, s, nil} |
| 26 |
} |
| 27 |
|
| 28 |
func NewOp(op string, a *Expr, b *Expr) *Expr { |
| 29 |
return &Expr{op, "", []*Expr{a, b}} |
| 30 |
} |
| 31 |
|
| 32 |
func NewFun(op string, args *Expr) *Expr { |
| 33 |
s := strings.TrimRight(op, "(") |
| 34 |
e := NewExpr(s, s) |
| 35 |
e.kid = args.kid |
| 36 |
return e |
| 37 |
} |
| 38 |
|
| 39 |
type formLine struct { |
| 40 |
id string |
| 41 |
desc string |
| 42 |
expr *Expr |
| 43 |
cached bool |
| 44 |
n int64 |
| 45 |
} |
| 46 |
|
| 47 |
type form struct { |
| 48 |
id string |
| 49 |
lines map[string]*formLine |
| 50 |
sortedIds []string |
| 51 |
wholeDollarsOnly bool |
| 52 |
} |
| 53 |
|
| 54 |
type Tacky struct { |
| 55 |
forms map[string]*form |
| 56 |
curForm *form |
| 57 |
curLine string |
| 58 |
curFlag string |
| 59 |
sortedIds []string |
| 60 |
wholeDollarsOnly bool |
| 61 |
} |
| 62 |
|
| 63 |
func (tax *Tacky) Expr(e *Expr) { |
| 64 |
if tax.curLine == "" { |
| 65 |
if tax.curFlag == "" { |
| 66 |
panic("assignment expected") |
| 67 |
} |
| 68 |
if e.op != "ID" { |
| 69 |
panic("flag must be 0 or 1") |
| 70 |
} |
| 71 |
switch e.s { |
| 72 |
case "1": |
| 73 |
tax.wholeDollarsOnly = true |
| 74 |
case "0": |
| 75 |
tax.wholeDollarsOnly = false |
| 76 |
default: |
| 77 |
panic("flag must be 0 or 1") |
| 78 |
} |
| 79 |
tax.curFlag = "" |
| 80 |
return |
| 81 |
} |
| 82 |
form := tax.curForm |
| 83 |
line, ok := form.lines[tax.curLine] |
| 84 |
if !ok { |
| 85 |
panic("BUG: missing line " + tax.curLine) |
| 86 |
} |
| 87 |
if line.expr == nil { |
| 88 |
line.expr = e |
| 89 |
return |
| 90 |
} |
| 91 |
line.expr = NewOp("+", line.expr, e) |
| 92 |
} |
| 93 |
|
| 94 |
func (tax *Tacky) Assign(s string) { |
| 95 |
form := tax.curForm |
| 96 |
s = strings.TrimRight(s, "=") |
| 97 |
v := strings.SplitN(s, " ", 2) |
| 98 |
id := v[0] |
| 99 |
if form == nil { |
| 100 |
if id != "whole_dollars_only" { |
| 101 |
panic("no such flag: " + id) |
| 102 |
} |
| 103 |
tax.curFlag = id |
| 104 |
return |
| 105 |
} |
| 106 |
desc := "" |
| 107 |
if len(v) == 2 { |
| 108 |
desc = v[1] |
| 109 |
} |
| 110 |
if _, ok := form.lines[id]; ok { |
| 111 |
panic("duplicate line definition: " + id) |
| 112 |
} |
| 113 |
form.lines[id] = &formLine{id, strings.TrimSpace(desc), nil, false, 0} |
| 114 |
form.sortedIds = append(form.sortedIds, id) |
| 115 |
tax.curLine = id |
| 116 |
} |
| 117 |
|
| 118 |
func (tax *Tacky) BeginForm(line string) { |
| 119 |
if tax.curForm != nil { |
| 120 |
panic("nested form") |
| 121 |
} |
| 122 |
id := strings.TrimSpace(strings.TrimRight(line, "{")) |
| 123 |
if _, ok := tax.forms[id]; ok { |
| 124 |
panic("duplicate form: " + id) |
| 125 |
} |
| 126 |
tax.curForm = &form{id, make(map[string]*formLine), nil, tax.wholeDollarsOnly} |
| 127 |
tax.curLine = "" |
| 128 |
tax.sortedIds = append(tax.sortedIds, id) |
| 129 |
tax.forms[id] = tax.curForm |
| 130 |
} |
| 131 |
|
| 132 |
func (tax *Tacky) EndForm() { |
| 133 |
tax.curForm = nil |
| 134 |
tax.curLine = "" |
| 135 |
} |
| 136 |
|
| 137 |
func atoi(s string) int64 { |
| 138 |
n, e := strconv.Atoi(s) |
| 139 |
if e != nil { |
| 140 |
panic(e) |
| 141 |
} |
| 142 |
return int64(n) |
| 143 |
} |
| 144 |
|
| 145 |
func clip(a int64) int64 { |
| 146 |
if a < 0 { |
| 147 |
return 0 |
| 148 |
} |
| 149 |
return a |
| 150 |
} |
| 151 |
|
| 152 |
func min(a, b int64) int64 { |
| 153 |
if a < b { |
| 154 |
return a |
| 155 |
} |
| 156 |
return b |
| 157 |
} |
| 158 |
|
| 159 |
func max(a, b int64) int64 { |
| 160 |
if a > b { |
| 161 |
return a |
| 162 |
} |
| 163 |
return b |
| 164 |
} |
| 165 |
|
| 166 |
func (tax *Tacky) Eval(e *Expr) int64 { |
| 167 |
if e == nil { // Undefined. |
| 168 |
return 0 |
| 169 |
} |
| 170 |
switch e.op { |
| 171 |
case "$": |
| 172 |
s := strings.TrimLeft(e.s, "$") |
| 173 |
v := strings.Split(s, ".") |
| 174 |
if len(v) == 2 { |
| 175 |
return atoi(v[0]) * 100 + atoi(v[1]) |
| 176 |
} |
| 177 |
if len(v) != 1 { |
| 178 |
panic("bad money: " + e.s) |
| 179 |
} |
| 180 |
return atoi(s) * 100 |
| 181 |
case "ID": |
| 182 |
if e.s == "0" { |
| 183 |
return 0 |
| 184 |
} |
| 185 |
return tax.Get(e.s) |
| 186 |
case "+": |
| 187 |
return tax.Eval(e.kid[0]) + tax.Eval(e.kid[1]) |
| 188 |
case "-": |
| 189 |
return tax.Eval(e.kid[0]) - tax.Eval(e.kid[1]) |
| 190 |
case "*": |
| 191 |
if e.kid[1].op == "%" { |
| 192 |
v := strings.Split(strings.TrimRight(e.kid[1].s, "%"), ".") |
| 193 |
p := atoi(v[0]) |
| 194 |
q := int64(100) |
| 195 |
if len(v) == 2 { |
| 196 |
for _ = range v[1] { |
| 197 |
p *= 10 |
| 198 |
q *= 10 |
| 199 |
} |
| 200 |
p += atoi(v[1]) |
| 201 |
} else if len(v) != 1 { |
| 202 |
panic("malformed percentage") |
| 203 |
} |
| 204 |
return (tax.Eval(e.kid[0]) * p + q / 2) / q |
| 205 |
} |
| 206 |
return tax.Eval(e.kid[0]) * tax.Eval(e.kid[1]) |
| 207 |
case "/": |
| 208 |
return tax.Eval(e.kid[0]) / tax.Eval(e.kid[1]) |
| 209 |
case "XREF": |
| 210 |
v := strings.Split(e.s, ":") |
| 211 |
return tax.XGet(strings.TrimLeft(v[0], "["), strings.TrimRight(v[1], "]")) |
| 212 |
case "clip": |
| 213 |
if len(e.kid) != 1 { |
| 214 |
panic("bad arg count") |
| 215 |
} |
| 216 |
return clip(tax.Eval(e.kid[0])) |
| 217 |
case "min": |
| 218 |
if len(e.kid) != 2 { |
| 219 |
panic("bad arg count") |
| 220 |
} |
| 221 |
return min(tax.Eval(e.kid[0]), tax.Eval(e.kid[1])) |
| 222 |
case "max": |
| 223 |
if len(e.kid) != 2 { |
| 224 |
panic("bad arg count") |
| 225 |
} |
| 226 |
return max(tax.Eval(e.kid[0]), tax.Eval(e.kid[1])) |
| 227 |
} |
| 228 |
panic("no such op: " + e.op) |
| 229 |
} |
| 230 |
|
| 231 |
func (tax *Tacky) Get(id string) int64 { |
| 232 |
line, ok := tax.curForm.lines[id] |
| 233 |
if !ok { |
| 234 |
panic("no such line: " + id) |
| 235 |
} |
| 236 |
if line.cached { |
| 237 |
return line.n |
| 238 |
} |
| 239 |
line.cached = true |
| 240 |
line.n = tax.Eval(line.expr) |
| 241 |
if tax.curForm.wholeDollarsOnly { |
| 242 |
if (line.n >= 0) { |
| 243 |
line.n = (line.n + 50) / 100 * 100 |
| 244 |
} else { |
| 245 |
line.n = (line.n - 50) / 100 * 100 |
| 246 |
} |
| 247 |
} |
| 248 |
return line.n |
| 249 |
} |
| 250 |
|
| 251 |
func (tax *Tacky) XGet(id, line string) int64 { |
| 252 |
form, ok := tax.forms[id] |
| 253 |
if !ok { |
| 254 |
panic("no such form: " + id) |
| 255 |
} |
| 256 |
orig := tax.curForm |
| 257 |
tax.curForm = form |
| 258 |
n := tax.Get(line) |
| 259 |
tax.curForm = orig |
| 260 |
return n |
| 261 |
} |
| 262 |
|
| 263 |
func dollarize(cents int64) string { |
| 264 |
if cents < 0 { |
| 265 |
return "(" + dollarize(-cents) + ")" |
| 266 |
} |
| 267 |
return fmt.Sprintf("$%d.%02d", cents/100, cents%100) |
| 268 |
} |
| 269 |
|
| 270 |
func main() { |
| 271 |
tax := new(Tacky) |
| 272 |
tax.forms = make(map[string]*form) |
| 273 |
yyParse(NewLexerWithInit(os.Stdin, func(y *Lexer) { y.p = tax })) |
| 274 |
|
| 275 |
for _, id := range tax.sortedIds { |
| 276 |
f := tax.forms[id] |
| 277 |
tax.curForm = f |
| 278 |
fmt.Println(f.id) |
| 279 |
for _, id := range f.sortedIds { |
| 280 |
fmt.Println(id, f.lines[id].desc, dollarize(tax.Get(id))) |
| 281 |
} |
| 282 |
fmt.Println() |
| 283 |
} |
| 284 |
} |