1
0
mirror of https://github.com/golang/go synced 2024-11-25 16:47:56 -07:00

goyacc: make more gofmt-compliant

Adjust goyacc.go to produce code that is gofmt-compliant whenever it is easy to do so. Also changed two lines in cpyact that appeared to be bugs.

Also updated units.y to remove a few other errors.

After this change, units.go has only two style errors: an extra newline at the top of the file, and yys misaligned in yySymType.

R=rsc
CC=golang-dev
https://golang.org/cl/5265047
This commit is contained in:
Chris Hundt 2011-10-14 13:10:02 -04:00 committed by Russ Cox
parent b9faf5ff7d
commit 550fa3fa2d
2 changed files with 54 additions and 49 deletions

View File

@ -506,19 +506,20 @@ outer:
// non-literals // non-literals
c := tokset[i].name[0] c := tokset[i].name[0]
if c != ' ' && c != '$' { if c != ' ' && c != '$' {
fmt.Fprintf(ftable, "const\t%v\t= %v\n", tokset[i].name, tokset[i].value) fmt.Fprintf(ftable, "const %v = %v\n", tokset[i].name, tokset[i].value)
} }
} }
// put out names of token names // put out names of token names
fmt.Fprintf(ftable, "var\t%sToknames\t =[]string {\n", prefix) ftable.WriteRune('\n')
fmt.Fprintf(ftable, "var %sToknames = []string{\n", prefix)
for i := TOKSTART; i <= ntokens; i++ { for i := TOKSTART; i <= ntokens; i++ {
fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name) fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name)
} }
fmt.Fprintf(ftable, "}\n") fmt.Fprintf(ftable, "}\n")
// put out names of state names // put out names of state names
fmt.Fprintf(ftable, "var\t%sStatenames\t =[]string {\n", prefix) fmt.Fprintf(ftable, "var %sStatenames = []string{", prefix)
// for i:=TOKSTART; i<=ntokens; i++ { // for i:=TOKSTART; i<=ntokens; i++ {
// fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name); // fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name);
// } // }
@ -595,7 +596,7 @@ outer:
break break
} }
levprd[nprod] |= ACTFLAG levprd[nprod] |= ACTFLAG
fmt.Fprintf(fcode, "\ncase %v:", nprod) fmt.Fprintf(fcode, "\n\tcase %v:", nprod)
cpyact(curprod, mem) cpyact(curprod, mem)
// action within rule... // action within rule...
@ -652,8 +653,8 @@ outer:
if tempty != nontrst[curprod[0]-NTBASE].value { if tempty != nontrst[curprod[0]-NTBASE].value {
errorf("default action causes potential type clash") errorf("default action causes potential type clash")
} }
fmt.Fprintf(fcode, "\ncase %v:", nprod) fmt.Fprintf(fcode, "\n\tcase %v:", nprod)
fmt.Fprintf(fcode, "\n\t%sVAL.%v = %sS[%spt-0].%v;", fmt.Fprintf(fcode, "\n\t\t%sVAL.%v = %sS[%spt-0].%v",
prefix, typeset[tempty], prefix, prefix, typeset[tempty]) prefix, typeset[tempty], prefix, prefix, typeset[tempty])
} }
moreprod() moreprod()
@ -671,6 +672,7 @@ outer:
fmt.Fprintf(fcode, "\n\t}") fmt.Fprintf(fcode, "\n\t}")
ftable.WriteRune('\n')
fmt.Fprintf(ftable, "const %sEofCode = 1\n", prefix) fmt.Fprintf(ftable, "const %sEofCode = 1\n", prefix)
fmt.Fprintf(ftable, "const %sErrCode = 2\n", prefix) fmt.Fprintf(ftable, "const %sErrCode = 2\n", prefix)
fmt.Fprintf(ftable, "const %sMaxDepth = %v\n", prefix, stacksize) fmt.Fprintf(ftable, "const %sMaxDepth = %v\n", prefix, stacksize)
@ -1039,7 +1041,7 @@ func cpyunion() {
if !lflag { if !lflag {
fmt.Fprintf(ftable, "\n//line %v:%v\n", infile, lineno) fmt.Fprintf(ftable, "\n//line %v:%v\n", infile, lineno)
} }
fmt.Fprintf(ftable, "type\t%sSymType\tstruct", prefix) fmt.Fprintf(ftable, "type %sSymType struct", prefix)
level := 0 level := 0
@ -1055,7 +1057,7 @@ out:
lineno++ lineno++
case '{': case '{':
if level == 0 { if level == 0 {
fmt.Fprintf(ftable, "\n\tyys\tint;") fmt.Fprintf(ftable, "\n\tyys int")
} }
level++ level++
case '}': case '}':
@ -1065,7 +1067,7 @@ out:
} }
} }
} }
fmt.Fprintf(ftable, "\n") fmt.Fprintf(ftable, "\n\n")
} }
// //
@ -1163,7 +1165,7 @@ func dumpprod(curprod []int, max int) {
func cpyact(curprod []int, max int) { func cpyact(curprod []int, max int) {
if !lflag { if !lflag {
fmt.Fprintf(fcode, "\n//line %v:%v\n", infile, lineno) fmt.Fprintf(fcode, "\n\t\t//line %v:%v\n\t\t", infile, lineno)
} }
lno := lineno lno := lineno
@ -1177,14 +1179,13 @@ loop:
switch c { switch c {
case ';': case ';':
if brac == 0 { if brac == 0 {
ftable.WriteRune(c) fcode.WriteRune(c)
return return
} }
case '{': case '{':
if brac == 0 { if brac == 0 {
} }
ftable.WriteRune('\t')
brac++ brac++
case '$': case '$':
@ -1345,7 +1346,9 @@ loop:
errorf("action does not terminate") errorf("action does not terminate")
case '\n': case '\n':
fmt.Fprint(fcode, "\n\t")
lineno++ lineno++
continue loop
} }
fcode.WriteRune(c) fcode.WriteRune(c)
@ -2072,7 +2075,7 @@ func output() {
var c, u, v int var c, u, v int
fmt.Fprintf(ftable, "\n//line yacctab:1\n") fmt.Fprintf(ftable, "\n//line yacctab:1\n")
fmt.Fprintf(ftable, "var\t%sExca = []int {\n", prefix) fmt.Fprintf(ftable, "var %sExca = []int{\n", prefix)
noset := mkset() noset := mkset()
@ -2145,10 +2148,12 @@ func output() {
} }
fmt.Fprintf(ftable, "}\n") fmt.Fprintf(ftable, "}\n")
fmt.Fprintf(ftable, "const\t%sNprod\t= %v\n", prefix, nprod) ftable.WriteRune('\n')
fmt.Fprintf(ftable, "const\t%sPrivate\t= %v\n", prefix, PRIVATE) fmt.Fprintf(ftable, "const %sNprod = %v\n", prefix, nprod)
fmt.Fprintf(ftable, "var\t%sTokenNames []string\n", prefix) fmt.Fprintf(ftable, "const %sPrivate = %v\n", prefix, PRIVATE)
fmt.Fprintf(ftable, "var\t%sStates []string\n", prefix) ftable.WriteRune('\n')
fmt.Fprintf(ftable, "var %sTokenNames []string\n", prefix)
fmt.Fprintf(ftable, "var %sStates []string\n", prefix)
} }
// //
@ -2264,7 +2269,7 @@ func wract(i int) {
continue continue
} }
if flag == 0 { if flag == 0 {
fmt.Fprintf(ftable, "-1, %v,\n", i) fmt.Fprintf(ftable, "\t-1, %v,\n", i)
} }
flag++ flag++
fmt.Fprintf(ftable, "\t%v, %v,\n", p, p1) fmt.Fprintf(ftable, "\t%v, %v,\n", p, p1)
@ -2723,7 +2728,8 @@ nextn:
// write out the optimized parser // write out the optimized parser
// //
func aoutput() { func aoutput() {
fmt.Fprintf(ftable, "const\t%sLast\t= %v\n", prefix, maxa+1) ftable.WriteRune('\n')
fmt.Fprintf(ftable, "const %sLast = %v\n\n", prefix, maxa+1)
arout("Act", amem, maxa+1) arout("Act", amem, maxa+1)
arout("Pact", indgo, nstate) arout("Pact", indgo, nstate)
arout("Pgo", pgo, nnonter+1) arout("Pgo", pgo, nnonter+1)
@ -2805,7 +2811,7 @@ func others() {
arout("Tok2", temp1, c+1) arout("Tok2", temp1, c+1)
// table 3 has everything else // table 3 has everything else
fmt.Fprintf(ftable, "var\t%sTok3\t= []int {\n", prefix) fmt.Fprintf(ftable, "var %sTok3 = []int{\n\t", prefix)
c = 0 c = 0
for i = 1; i <= ntokens; i++ { for i = 1; i <= ntokens; i++ {
j = tokset[i].value j = tokset[i].value
@ -2816,13 +2822,19 @@ func others() {
continue continue
} }
fmt.Fprintf(ftable, "%4d,%4d,", j, i) if c%5 != 0 {
ftable.WriteRune(' ')
}
fmt.Fprintf(ftable, "%d, %d,", j, i)
c++ c++
if c%5 == 0 { if c%5 == 0 {
ftable.WriteRune('\n') fmt.Fprint(ftable, "\n\t")
} }
} }
fmt.Fprintf(ftable, "%4d,\n };\n", 0) if c%5 != 0 {
ftable.WriteRune(' ')
}
fmt.Fprintf(ftable, "%d,\n}\n", 0)
// copy parser text // copy parser text
c = getrune(finput) c = getrune(finput)
@ -2842,15 +2854,16 @@ func others() {
func arout(s string, v []int, n int) { func arout(s string, v []int, n int) {
s = prefix + s s = prefix + s
fmt.Fprintf(ftable, "var\t%v\t= []int {\n", s) fmt.Fprintf(ftable, "var %v = []int{\n", s)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
if i%10 == 0 { if i%10 == 0 {
ftable.WriteRune('\n') fmt.Fprintf(ftable, "\n\t")
} else {
ftable.WriteRune(' ')
} }
fmt.Fprintf(ftable, "%4d", v[i]) fmt.Fprintf(ftable, "%d,", v[i])
ftable.WriteRune(',')
} }
fmt.Fprintf(ftable, "\n};\n") fmt.Fprintf(ftable, "\n}\n")
} }
// //

View File

@ -64,11 +64,9 @@ var retnode2 Node
var retnode Node var retnode Node
var sym string var sym string
var vflag bool var vflag bool
%} %}
%union %union {
{
node Node node Node
vvar *Var vvar *Var
numb int numb int
@ -85,7 +83,6 @@ 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
@ -98,7 +95,6 @@ prog:
| ':' 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
@ -109,15 +105,13 @@ prog:
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)
} }
} }
@ -171,7 +165,6 @@ expr2:
| 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 {
Error("exponent has units") Error("exponent has units")
@ -690,7 +683,6 @@ func pname() float64 {
return 0 return 0
} }
// careful multiplication // careful multiplication
// exponents (log) are checked before multiply // exponents (log) are checked before multiply
func fmul(a, b float64) float64 { func fmul(a, b float64) float64 {