Commit 3d552700 authored by Rob Pike's avatar Rob Pike

goyacc: cleanups

- remove calls to print
- make units compile again
- make units.y closer to gofmt style

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/4802052
parent f0d8af20
...@@ -410,7 +410,7 @@ outer: ...@@ -410,7 +410,7 @@ outer:
if t < NTBASE { if t < NTBASE {
j = TYPE(toklev[t]) j = TYPE(toklev[t])
if j != 0 && j != ty { if j != 0 && j != ty {
errorf("type redeclaration of token ", errorf("type redeclaration of token %s",
tokset[t].name) tokset[t].name)
} else { } else {
toklev[t] = SETTYPE(toklev[t], ty) toklev[t] = SETTYPE(toklev[t], ty)
...@@ -1448,15 +1448,15 @@ func cpres() { ...@@ -1448,15 +1448,15 @@ func cpres() {
func dumppres() { func dumppres() {
for i := 0; i <= nnonter; i++ { for i := 0; i <= nnonter; i++ {
print("nonterm %d\n", i) fmt.Printf("nonterm %d\n", i)
curres := pres[i] curres := pres[i]
for j := 0; j < len(curres); j++ { for j := 0; j < len(curres); j++ {
print("\tproduction %d:", j) fmt.Printf("\tproduction %d:", j)
prd := curres[j] prd := curres[j]
for k := 0; k < len(prd); k++ { for k := 0; k < len(prd); k++ {
print(" %d", prd[k]) fmt.Printf(" %d", prd[k])
} }
print("\n") fmt.Print("\n")
} }
} }
} }
...@@ -1550,7 +1550,7 @@ again: ...@@ -1550,7 +1550,7 @@ again:
func dumpempty() { func dumpempty() {
for i := 0; i <= nnonter; i++ { for i := 0; i <= nnonter; i++ {
if pempty[i] == EMPTY { if pempty[i] == EMPTY {
print("non-term %d %s matches empty\n", i, symnam(i+NTBASE)) fmt.Printf("non-term %d %s matches empty\n", i, symnam(i+NTBASE))
} }
} }
} }
...@@ -2768,8 +2768,8 @@ func others() { ...@@ -2768,8 +2768,8 @@ func others() {
j = tokset[i].value j = tokset[i].value
if j >= 0 && j < 256 { if j >= 0 && j < 256 {
if temp1[j] != 0 { if temp1[j] != 0 {
print("yacc bug -- cant have 2 different Ts with same value\n") fmt.Print("yacc bug -- cant have 2 different Ts with same value\n")
print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name) fmt.Printf(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name)
nerrors++ nerrors++
} }
temp1[j] = i temp1[j] = i
...@@ -2792,8 +2792,8 @@ func others() { ...@@ -2792,8 +2792,8 @@ func others() {
j = tokset[i].value - PRIVATE j = tokset[i].value - PRIVATE
if j >= 0 && j < 256 { if j >= 0 && j < 256 {
if temp1[j] != 0 { if temp1[j] != 0 {
print("yacc bug -- cant have 2 different Ts with same value\n") fmt.Print("yacc bug -- cant have 2 different Ts with same value\n")
print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name) fmt.Printf(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name)
nerrors++ nerrors++
} }
temp1[j] = i temp1[j] = i
......
...@@ -69,10 +69,10 @@ var vflag bool ...@@ -69,10 +69,10 @@ var vflag bool
%union %union
{ {
node Node; node Node
vvar *Var; vvar *Var
numb int; numb int
vval float64; vval float64
} }
%type <node> prog expr expr0 expr1 expr2 expr3 expr4 %type <node> prog expr expr0 expr1 expr2 expr3 expr4
...@@ -84,42 +84,41 @@ var vflag bool ...@@ -84,42 +84,41 @@ var vflag bool
prog: prog:
':' VAR expr ':' VAR expr
{ {
var f int; var f int
f = int($2.node.dim[0]); f = int($2.node.dim[0])
$2.node = $3; $2.node = $3
$2.node.dim[0] = 1; $2.node.dim[0] = 1
if f != 0 { if f != 0 {
Errorf("redefinition of %v", $2.name); Errorf("redefinition of %v", $2.name)
} else } else if vflag {
if vflag { fmt.Printf("%v\t%v\n", $2.name, &$2.node)
fmt.Printf("%v\t%v\n", $2.name, &$2.node);
} }
} }
| ':' VAR '#' | ':' VAR '#'
{ {
var f, i int; var f, i int
for i=1; i<Ndim; i++ { for i=1; i<Ndim; i++ {
if fund[i] == nil { if fund[i] == nil {
break; break
} }
} }
if i >= Ndim { if i >= Ndim {
Errorf("too many dimensions"); Error("too many dimensions")
i = Ndim-1; i = Ndim-1
} }
fund[i] = $2; fund[i] = $2
f = int($2.node.dim[0]); f = int($2.node.dim[0])
$2.node = one; $2.node = one
$2.node.dim[0] = 1; $2.node.dim[0] = 1
$2.node.dim[i] = 1; $2.node.dim[i] = 1
if f != 0 { if f != 0 {
Errorf("redefinition of %v", $2.name); Errorf("redefinition of %v", $2.name)
} else } else
if vflag { if vflag {
fmt.Printf("%v\t#\n", $2.name); fmt.Printf("%v\t#\n", $2.name)
} }
} }
| ':' | ':'
...@@ -127,65 +126,65 @@ prog: ...@@ -127,65 +126,65 @@ prog:
} }
| '?' expr | '?' expr
{ {
retnode1 = $2; retnode1 = $2
} }
| '?' | '?'
{ {
retnode1 = one; retnode1 = one
} }
expr: expr:
expr4 expr4
| expr '+' expr4 | expr '+' expr4
{ {
add(&$$, &$1, &$3); add(&$$, &$1, &$3)
} }
| expr '-' expr4 | expr '-' expr4
{ {
sub(&$$, &$1, &$3); sub(&$$, &$1, &$3)
} }
expr4: expr4:
expr3 expr3
| expr4 '*' expr3 | expr4 '*' expr3
{ {
mul(&$$, &$1, &$3); mul(&$$, &$1, &$3)
} }
| expr4 '/' expr3 | expr4 '/' expr3
{ {
div(&$$, &$1, &$3); div(&$$, &$1, &$3)
} }
expr3: expr3:
expr2 expr2
| expr3 expr2 | expr3 expr2
{ {
mul(&$$, &$1, &$2); mul(&$$, &$1, &$2)
} }
expr2: expr2:
expr1 expr1
| expr2 SUP | expr2 SUP
{ {
xpn(&$$, &$1, $2); xpn(&$$, &$1, $2)
} }
| expr2 '^' expr1 | expr2 '^' expr1
{ {
var i int; var i int
for i=1; i<Ndim; i++ { for i=1; i<Ndim; i++ {
if $3.dim[i] != 0 { if $3.dim[i] != 0 {
Errorf("exponent has units"); Error("exponent has units")
$$ = $1; $$ = $1
break; break
} }
} }
if i >= Ndim { if i >= Ndim {
i = int($3.vval); i = int($3.vval)
if float64(i) != $3.vval { if float64(i) != $3.vval {
Errorf("exponent not integral"); Error("exponent not integral")
} }
xpn(&$$, &$1, i); xpn(&$$, &$1, i)
} }
} }
...@@ -193,26 +192,27 @@ expr1: ...@@ -193,26 +192,27 @@ expr1:
expr0 expr0
| expr1 '|' expr0 | expr1 '|' expr0
{ {
div(&$$, &$1, &$3); div(&$$, &$1, &$3)
} }
expr0: expr0:
VAR VAR
{ {
if $1.node.dim[0] == 0 { if $1.node.dim[0] == 0 {
Errorf("undefined %v", $1.name); Errorf("undefined %v", $1.name)
$$ = one; $$ = one
} else } else {
$$ = $1.node; $$ = $1.node
}
} }
| VAL | VAL
{ {
$$ = one; $$ = one
$$.vval = $1; $$.vval = $1
} }
| '(' expr ')' | '(' expr ')'
{ {
$$ = $2; $$ = $2
} }
%% %%
...@@ -403,6 +403,10 @@ func Errorf(s string, v ...interface{}) { ...@@ -403,6 +403,10 @@ func Errorf(s string, v ...interface{}) {
} }
} }
func Error(s string) {
Errorf("%s", s)
}
func add(c, a, b *Node) { func add(c, a, b *Node) {
var i int var i int
var d int8 var d int8
...@@ -411,7 +415,7 @@ func add(c, a, b *Node) { ...@@ -411,7 +415,7 @@ func add(c, a, b *Node) {
d = a.dim[i] d = a.dim[i]
c.dim[i] = d c.dim[i] = d
if d != b.dim[i] { if d != b.dim[i] {
Errorf("add must be like units") Error("add must be like units")
} }
} }
c.vval = fadd(a.vval, b.vval) c.vval = fadd(a.vval, b.vval)
...@@ -425,7 +429,7 @@ func sub(c, a, b *Node) { ...@@ -425,7 +429,7 @@ func sub(c, a, b *Node) {
d = a.dim[i] d = a.dim[i]
c.dim[i] = d c.dim[i] = d
if d != b.dim[i] { if d != b.dim[i] {
Errorf("sub must be like units") Error("sub must be like units")
} }
} }
c.vval = fadd(a.vval, -b.vval) c.vval = fadd(a.vval, -b.vval)
...@@ -633,29 +637,29 @@ type Prefix struct { ...@@ -633,29 +637,29 @@ type Prefix struct {
} }
var prefix = []Prefix{ // prefix table var prefix = []Prefix{ // prefix table
Prefix{1e-24, "yocto"}, {1e-24, "yocto"},
Prefix{1e-21, "zepto"}, {1e-21, "zepto"},
Prefix{1e-18, "atto"}, {1e-18, "atto"},
Prefix{1e-15, "femto"}, {1e-15, "femto"},
Prefix{1e-12, "pico"}, {1e-12, "pico"},
Prefix{1e-9, "nano"}, {1e-9, "nano"},
Prefix{1e-6, "micro"}, {1e-6, "micro"},
Prefix{1e-6, "μ"}, {1e-6, "μ"},
Prefix{1e-3, "milli"}, {1e-3, "milli"},
Prefix{1e-2, "centi"}, {1e-2, "centi"},
Prefix{1e-1, "deci"}, {1e-1, "deci"},
Prefix{1e1, "deka"}, {1e1, "deka"},
Prefix{1e2, "hecta"}, {1e2, "hecta"},
Prefix{1e2, "hecto"}, {1e2, "hecto"},
Prefix{1e3, "kilo"}, {1e3, "kilo"},
Prefix{1e6, "mega"}, {1e6, "mega"},
Prefix{1e6, "meg"}, {1e6, "meg"},
Prefix{1e9, "giga"}, {1e9, "giga"},
Prefix{1e12, "tera"}, {1e12, "tera"},
Prefix{1e15, "peta"}, {1e15, "peta"},
Prefix{1e18, "exa"}, {1e18, "exa"},
Prefix{1e21, "zetta"}, {1e21, "zetta"},
Prefix{1e24, "yotta"}, {1e24, "yotta"},
} }
func pname() float64 { func pname() float64 {
...@@ -711,11 +715,11 @@ func fmul(a, b float64) float64 { ...@@ -711,11 +715,11 @@ func fmul(a, b float64) float64 {
} }
if l > Maxe { if l > Maxe {
Errorf("overflow in multiply") Error("overflow in multiply")
return 1 return 1
} }
if l < -Maxe { if l < -Maxe {
Errorf("underflow in multiply") Error("underflow in multiply")
return 0 return 0
} }
return a * b return a * b
...@@ -746,11 +750,11 @@ func fdiv(a, b float64) float64 { ...@@ -746,11 +750,11 @@ func fdiv(a, b float64) float64 {
} }
if l < -Maxe { if l < -Maxe {
Errorf("overflow in divide") Error("overflow in divide")
return 1 return 1
} }
if l > Maxe { if l > Maxe {
Errorf("underflow in divide") Error("underflow in divide")
return 0 return 0
} }
return a / b return a / b
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment