OLD | NEW |
1 // Derived from Plan 9's /sys/src/cmd/units.y | 1 // Derived from Plan 9's /sys/src/cmd/units.y |
2 // http://plan9.bell-labs.com/sources/plan9/sys/src/cmd/units.y | 2 // http://plan9.bell-labs.com/sources/plan9/sys/src/cmd/units.y |
3 // | 3 // |
4 // Copyright (C) 2003, Lucent Technologies Inc. and others. All Rights Reserved. | 4 // Copyright (C) 2003, Lucent Technologies Inc. and others. All Rights Reserved. |
5 // Portions Copyright 2009 The Go Authors. All Rights Reserved. | 5 // Portions Copyright 2009 The Go Authors. All Rights Reserved. |
6 // Distributed under the terms of the Lucent Public License Version 1.02 | 6 // Distributed under the terms of the Lucent Public License Version 1.02 |
7 // See http://plan9.bell-labs.com/plan9/license.html | 7 // See http://plan9.bell-labs.com/plan9/license.html |
8 | 8 |
9 // Generate parser with prefix "units_": | 9 // Generate parser with prefix "units_": |
10 // go tool yacc -p "units_" | 10 // go tool yacc -p "units_" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 69 |
70 %union { | 70 %union { |
71 node Node | 71 node Node |
72 vvar *Var | 72 vvar *Var |
73 numb int | 73 numb int |
74 vval float64 | 74 vval float64 |
75 } | 75 } |
76 | 76 |
77 %type <node> prog expr expr0 expr1 expr2 expr3 expr4 | 77 %type <node> prog expr expr0 expr1 expr2 expr3 expr4 |
78 | 78 |
79 %token» <vval>» VAL | 79 %token» <vval>» VÄL // dieresis to test UTF-8 |
80 %token <vvar> VAR | 80 %token <vvar> VAR |
81 %token <numb> _SUP // tests leading underscore in token name | 81 %token <numb> _SUP // tests leading underscore in token name |
82 %% | 82 %% |
83 prog: | 83 prog: |
84 ':' VAR expr | 84 ':' VAR expr |
85 { | 85 { |
86 var f int | 86 var f int |
87 f = int($2.node.dim[0]) | 87 f = int($2.node.dim[0]) |
88 $2.node = $3 | 88 $2.node = $3 |
89 $2.node.dim[0] = 1 | 89 $2.node.dim[0] = 1 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 expr0: | 192 expr0: |
193 VAR | 193 VAR |
194 { | 194 { |
195 if $1.node.dim[0] == 0 { | 195 if $1.node.dim[0] == 0 { |
196 Errorf("undefined %v", $1.name) | 196 Errorf("undefined %v", $1.name) |
197 $$ = one | 197 $$ = one |
198 } else { | 198 } else { |
199 $$ = $1.node | 199 $$ = $1.node |
200 } | 200 } |
201 } | 201 } |
202 |» VAL | 202 |» VÄL |
203 { | 203 { |
204 $$ = one | 204 $$ = one |
205 $$.vval = $1 | 205 $$.vval = $1 |
206 } | 206 } |
207 | '(' expr ')' | 207 | '(' expr ')' |
208 { | 208 { |
209 $$ = $2 | 209 $$ = $2 |
210 } | 210 } |
211 %% | 211 %% |
212 | 212 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 break | 268 break |
269 } | 269 } |
270 } | 270 } |
271 peekrune = c | 271 peekrune = c |
272 f, err := strconv.ParseFloat(sym, 64) | 272 f, err := strconv.ParseFloat(sym, 64) |
273 if err != nil { | 273 if err != nil { |
274 fmt.Printf("error converting %v\n", sym) | 274 fmt.Printf("error converting %v\n", sym) |
275 f = 0 | 275 f = 0 |
276 } | 276 } |
277 yylval.vval = f | 277 yylval.vval = f |
278 » return VAL | 278 » return VÄL |
279 } | 279 } |
280 | 280 |
281 func (UnitsLex) Error(s string) { | 281 func (UnitsLex) Error(s string) { |
282 Errorf("syntax error, last name: %v", sym) | 282 Errorf("syntax error, last name: %v", sym) |
283 } | 283 } |
284 | 284 |
285 func main() { | 285 func main() { |
286 var file string | 286 var file string |
287 | 287 |
288 flag.BoolVar(&vflag, "v", false, "verbose") | 288 flag.BoolVar(&vflag, "v", false, "verbose") |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 if l > Maxe { | 754 if l > Maxe { |
755 Error("underflow in divide") | 755 Error("underflow in divide") |
756 return 0 | 756 return 0 |
757 } | 757 } |
758 return a / b | 758 return a / b |
759 } | 759 } |
760 | 760 |
761 func fadd(a, b float64) float64 { | 761 func fadd(a, b float64) float64 { |
762 return a + b | 762 return a + b |
763 } | 763 } |
OLD | NEW |