LEFT | RIGHT |
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 %{ | 9 %{ |
10 | 10 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 $$.vval = $1; | 208 $$.vval = $1; |
209 } | 209 } |
210 | '(' expr ')' | 210 | '(' expr ')' |
211 { | 211 { |
212 $$ = $2; | 212 $$ = $2; |
213 } | 213 } |
214 %% | 214 %% |
215 | 215 |
216 type UnitsLex int | 216 type UnitsLex int |
217 | 217 |
218 func (l UnitsLex) Lex(yylval *YYSTYPE) int { | 218 func (l UnitsLex) Lex(yylval *yySymType) int { |
219 var c, i int | 219 var c, i int |
220 | 220 |
221 c = peekrune | 221 c = peekrune |
222 peekrune = ' ' | 222 peekrune = ' ' |
223 | 223 |
224 loop: | 224 loop: |
225 if (c >= '0' && c <= '9') || c == '.' { | 225 if (c >= '0' && c <= '9') || c == '.' { |
226 goto numb | 226 goto numb |
227 } | 227 } |
228 if ralpha(c) { | 228 if ralpha(c) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 for i = 0; ; i++ { | 266 for i = 0; ; i++ { |
267 sym += string(c) | 267 sym += string(c) |
268 c = getrune() | 268 c = getrune() |
269 if !rdigit(c) { | 269 if !rdigit(c) { |
270 break | 270 break |
271 } | 271 } |
272 } | 272 } |
273 peekrune = c | 273 peekrune = c |
274 f, err := strconv.Atof64(sym) | 274 f, err := strconv.Atof64(sym) |
275 if err != nil { | 275 if err != nil { |
276 » » fmt.Printf("error converting %v", sym) | 276 » » fmt.Printf("error converting %v\n", sym) |
277 f = 0 | 277 f = 0 |
278 } | 278 } |
279 yylval.vval = f | 279 yylval.vval = f |
280 return VAL | 280 return VAL |
281 } | 281 } |
282 | 282 |
283 func main() { | 283 func main() { |
284 var file string | 284 var file string |
285 | 285 |
286 flag.BoolVar(&vflag, "v", false, "verbose") | 286 flag.BoolVar(&vflag, "v", false, "verbose") |
287 | 287 |
288 flag.Parse() | 288 flag.Parse() |
289 | 289 |
290 file = os.Getenv("GOROOT") + "/src/cmd/goyacc/units.txt" | 290 file = os.Getenv("GOROOT") + "/src/cmd/goyacc/units.txt" |
291 if flag.NArg() > 0 { | 291 if flag.NArg() > 0 { |
292 file = flag.Arg(0) | 292 file = flag.Arg(0) |
293 } | 293 } |
294 | 294 |
295 f, err := os.Open(file, os.O_RDONLY, 0) | 295 f, err := os.Open(file, os.O_RDONLY, 0) |
296 if err != nil { | 296 if err != nil { |
297 » » fmt.Printf("error opening %v: %v", file, err) | 297 » » fmt.Printf("error opening %v: %v\n", file, err) |
298 os.Exit(1) | 298 os.Exit(1) |
299 } | 299 } |
300 fi = bufio.NewReader(f) | 300 fi = bufio.NewReader(f) |
301 | 301 |
302 one.vval = 1 | 302 one.vval = 1 |
303 | 303 |
304 /* | 304 /* |
305 * read the 'units' file to | 305 * read the 'units' file to |
306 * develope a database | 306 * develope a database |
307 */ | 307 */ |
308 lineno = 0 | 308 lineno = 0 |
309 for { | 309 for { |
310 lineno++ | 310 lineno++ |
311 if readline() { | 311 if readline() { |
312 break | 312 break |
313 } | 313 } |
314 if len(line) == 0 || line[0] == '/' { | 314 if len(line) == 0 || line[0] == '/' { |
315 continue | 315 continue |
316 } | 316 } |
317 peekrune = ':' | 317 peekrune = ':' |
318 » » Parse(UnitsLex(0)) | 318 » » yyParse(UnitsLex(0)) |
319 } | 319 } |
320 | 320 |
321 /* | 321 /* |
322 * read the console to | 322 * read the console to |
323 * print ratio of pairs | 323 * print ratio of pairs |
324 */ | 324 */ |
325 fi = bufio.NewReader(os.NewFile(0, "stdin")) | 325 fi = bufio.NewReader(os.NewFile(0, "stdin")) |
326 | 326 |
327 lineno = 0 | 327 lineno = 0 |
328 for { | 328 for { |
329 if (lineno & 1) != 0 { | 329 if (lineno & 1) != 0 { |
330 fmt.Printf("you want: ") | 330 fmt.Printf("you want: ") |
331 } else { | 331 } else { |
332 fmt.Printf("you have: ") | 332 fmt.Printf("you have: ") |
333 } | 333 } |
334 if readline() { | 334 if readline() { |
335 break | 335 break |
336 } | 336 } |
337 peekrune = '?' | 337 peekrune = '?' |
338 nerrors = 0 | 338 nerrors = 0 |
339 » » Parse(UnitsLex(0)) | 339 » » yyParse(UnitsLex(0)) |
340 if nerrors != 0 { | 340 if nerrors != 0 { |
341 continue | 341 continue |
342 } | 342 } |
343 if (lineno & 1) != 0 { | 343 if (lineno & 1) != 0 { |
344 if specialcase(&retnode, &retnode2, &retnode1) { | 344 if specialcase(&retnode, &retnode2, &retnode1) { |
345 fmt.Printf("\tis %v\n", &retnode) | 345 fmt.Printf("\tis %v\n", &retnode) |
346 } else { | 346 } else { |
347 div(&retnode, &retnode2, &retnode1) | 347 div(&retnode, &retnode2, &retnode1) |
348 fmt.Printf("\t* %v\n", &retnode) | 348 fmt.Printf("\t* %v\n", &retnode) |
349 div(&retnode, &retnode1, &retnode2) | 349 div(&retnode, &retnode1, &retnode2) |
(...skipping 27 matching lines...) Expand all Loading... |
377 */ | 377 */ |
378 func rdigit(c int) bool { | 378 func rdigit(c int) bool { |
379 switch c { | 379 switch c { |
380 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | 380 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
381 '.', 'e', '+', '-': | 381 '.', 'e', '+', '-': |
382 return true | 382 return true |
383 } | 383 } |
384 return false | 384 return false |
385 } | 385 } |
386 | 386 |
| 387 func yyError(s string) { |
| 388 Error("syntax error, last name: %v", sym) |
| 389 } |
| 390 |
387 func Error(s string, v ...interface{}) { | 391 func Error(s string, v ...interface{}) { |
388 | |
389 /* | |
390 * hack to intercept message from yaccpar | |
391 */ | |
392 if s == "syntax error" { | |
393 Error("syntax error, last name: %v", sym) | |
394 return | |
395 } | |
396 fmt.Printf("%v: %v\n\t", lineno, line) | 392 fmt.Printf("%v: %v\n\t", lineno, line) |
397 fmt.Printf(s, v) | 393 fmt.Printf(s, v) |
398 fmt.Printf("\n") | 394 fmt.Printf("\n") |
399 | 395 |
400 nerrors++ | 396 nerrors++ |
401 if nerrors > 5 { | 397 if nerrors > 5 { |
402 fmt.Printf("too many errors\n") | 398 fmt.Printf("too many errors\n") |
403 os.Exit(1) | 399 os.Exit(1) |
404 } | 400 } |
405 } | 401 } |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 if l > Maxe { | 749 if l > Maxe { |
754 Error("underflow in divide") | 750 Error("underflow in divide") |
755 return 0 | 751 return 0 |
756 } | 752 } |
757 return a / b | 753 return a / b |
758 } | 754 } |
759 | 755 |
760 func fadd(a, b float64) float64 { | 756 func fadd(a, b float64) float64 { |
761 return a + b | 757 return a + b |
762 } | 758 } |
LEFT | RIGHT |