Skip to content

Commit ee6b89f

Browse files
author
Randall C. O'Reilly
committed
several improvements: go.nil, append fix, etc.
1 parent bbe64e4 commit ee6b89f

File tree

6 files changed

+107
-36
lines changed

6 files changed

+107
-36
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ We have modified the `printer` code in the `pyprint` package to instead print ou
88

99
# TODO
1010

11-
* imports
11+
* add gopy flag
12+
13+
* strings.Fields(x) -> x.split()
1214

1315
* class comments -> """
1416

1517
* switch -> ifs.. -- grab switch expr and put into each if
18+
19+
# gopy specific mode
20+
21+
* replace []string() -> go.Slice_string etc
22+
23+
# Check
24+
25+

format.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func format(
3636
if err != nil {
3737
return nil, err
3838
}
39-
pyfix := pyMove(buf.Bytes())
39+
pyfix := pyEdits(buf.Bytes(), true)
4040
return pyfix, nil
4141
// return buf.Bytes(), nil
4242
}
@@ -82,7 +82,7 @@ func format(
8282
return nil, err
8383
}
8484

85-
pyfix := pyMove(buf.Bytes())
85+
pyfix := pyEdits(buf.Bytes(), true)
8686

8787
out := sourceAdj(pyfix, cfg.Indent)
8888

pymove.go renamed to pyedits.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ func moveLines(lines *[][]byte, to, st, ed int) {
2222
*lines = nln
2323
}
2424

25-
// pyMove moves python segments around, e.g., methods
25+
// pyEdits performs post-generation edits for python
26+
// * moves python segments around, e.g., methods
2627
// into their proper classes
27-
func pyMove(src []byte) []byte {
28+
// * fixes printf, slice other common code
29+
func pyEdits(src []byte, gopy bool) []byte {
2830
type sted struct {
2931
st, ed int
3032
}
@@ -38,6 +40,20 @@ func pyMove(src []byte) []byte {
3840
fmtPrintf := []byte("fmt.Printf")
3941
fmtSprintf := []byte("fmt.Sprintf(")
4042
prints := []byte("print")
43+
eqappend := []byte("= append(")
44+
itoa := []byte("strconv.Itoa")
45+
float64p := []byte("float64(")
46+
float32p := []byte("float32(")
47+
floatp := []byte("float(")
48+
slicestr := []byte("[]string(")
49+
sliceint := []byte("[]int(")
50+
slicefloat64 := []byte("[]float64(")
51+
slicefloat32 := []byte("[]float32(")
52+
goslicestr := []byte("go.Slice_string([")
53+
gosliceint := []byte("go.Slice_int([")
54+
goslicefloat64 := []byte("go.Slice_float64([")
55+
goslicefloat32 := []byte("go.Slice_float32([")
56+
4157
endclass := "EndClass: "
4258
method := "Method: "
4359
endmethod := "EndMethod"
@@ -68,6 +84,10 @@ func pyMove(src []byte) []byte {
6884
curComSt = -1
6985
}
7086

87+
ln = bytes.Replace(ln, float64p, floatp, -1)
88+
ln = bytes.Replace(ln, float32p, floatp, -1)
89+
lines[li] = ln
90+
7191
switch {
7292
case bytes.Equal(ln, []byte(" :")) || bytes.Equal(ln, []byte(":")):
7393
lines = append(lines[:li], lines[li+1:]...) // delete marker
@@ -126,6 +146,33 @@ func pyMove(src []byte) []byte {
126146
}
127147
ln = bytes.Replace(ln, fmtPrintf, prints, -1)
128148
lines[li] = ln
149+
case bytes.Contains(ln, eqappend):
150+
idx := bytes.Index(ln, eqappend)
151+
comi := bytes.Index(ln[idx+len(eqappend):], []byte(","))
152+
nln := make([]byte, idx-1)
153+
copy(nln, ln[:idx-1])
154+
nln = append(nln, []byte(".append(")...)
155+
nln = append(nln, ln[idx+len(eqappend)+comi+1:]...)
156+
lines[li] = nln
157+
case bytes.Contains(ln, slicestr):
158+
ln = bytes.Replace(ln, slicestr, goslicestr, -1)
159+
ln = bytes.Replace(ln, []byte(")"), []byte("])"), 1)
160+
lines[li] = ln
161+
case bytes.Contains(ln, sliceint):
162+
ln = bytes.Replace(ln, sliceint, gosliceint, -1)
163+
ln = bytes.Replace(ln, []byte(")"), []byte("])"), 1)
164+
lines[li] = ln
165+
case bytes.Contains(ln, slicefloat64):
166+
ln = bytes.Replace(ln, slicefloat64, goslicefloat64, -1)
167+
ln = bytes.Replace(ln, []byte(")"), []byte("])"), 1)
168+
lines[li] = ln
169+
case bytes.Contains(ln, slicefloat32):
170+
ln = bytes.Replace(ln, slicefloat32, goslicefloat32, -1)
171+
ln = bytes.Replace(ln, []byte(")"), []byte("])"), 1)
172+
lines[li] = ln
173+
case bytes.Contains(ln, itoa):
174+
ln = bytes.Replace(ln, itoa, []byte(`str`), -1)
175+
lines[li] = ln
129176
}
130177
li++
131178
}

pyprint/nodes.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -813,8 +813,8 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
813813
// no parenthesis needed
814814
opstr := pyOpCvt(x.Op)
815815
if opstr != "" {
816-
p.print(opstr)
817-
} else {
816+
p.print(opstr, blank)
817+
} else if x.Op != token.AND {
818818
p.print(x.Op)
819819
}
820820
if x.Op == token.RANGE {
@@ -852,14 +852,14 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
852852
p.selectorExpr(x, depth, false)
853853

854854
case *ast.TypeAssertExpr:
855-
p.expr1(x.X, token.HighestPrec, depth)
856-
p.print(token.PERIOD, x.Lparen, token.LPAREN)
857855
if x.Type != nil {
858856
p.expr(x.Type)
857+
p.print(x.Lparen, token.LPAREN)
858+
p.expr1(x.X, token.HighestPrec, depth)
859+
p.print(x.Rparen, token.RPAREN)
859860
} else {
860-
p.print(token.TYPE)
861+
p.expr1(x.X, token.HighestPrec, depth)
861862
}
862-
p.print(x.Rparen, token.RPAREN)
863863

864864
case *ast.IndexExpr:
865865
// TODO(gri): should treat[] like parentheses and undo one level of depth
@@ -1308,7 +1308,14 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
13081308
case *ast.IncDecStmt:
13091309
const depth = 1
13101310
p.expr0(s.X, depth+1)
1311-
p.print(s.TokPos, s.Tok)
1311+
p.print(s.TokPos, blank)
1312+
if s.Tok == token.INC {
1313+
p.print(token.ADD_ASSIGN, blank, "1")
1314+
} else if s.Tok == token.DEC {
1315+
p.print(token.SUB_ASSIGN, blank, "1")
1316+
} else {
1317+
p.print("UnknownIncDec")
1318+
}
13121319

13131320
case *ast.AssignStmt:
13141321
var depth = 1
@@ -1431,20 +1438,24 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
14311438
}
14321439

14331440
case *ast.ForStmt:
1434-
p.print(token.FOR)
1435-
didRange := false
1436-
if x, ok := s.Cond.(*ast.BinaryExpr); ok {
1437-
if x.Op == token.LSS {
1438-
p.print(blank)
1439-
p.expr(x.X)
1440-
p.print(" in range", token.LPAREN)
1441-
p.expr(x.Y)
1442-
p.print(token.RPAREN)
1443-
didRange = true
1441+
if s.Init == nil && s.Cond == nil && s.Post == nil {
1442+
p.print("while True")
1443+
} else {
1444+
p.print(token.FOR)
1445+
didRange := false
1446+
if x, ok := s.Cond.(*ast.BinaryExpr); ok {
1447+
if x.Op == token.LSS {
1448+
p.print(blank)
1449+
p.expr(x.X)
1450+
p.print(" in range", token.LPAREN)
1451+
p.expr(x.Y)
1452+
p.print(token.RPAREN)
1453+
didRange = true
1454+
}
1455+
}
1456+
if !didRange {
1457+
p.controlClause(true, s.Init, s.Cond, s.Post)
14441458
}
1445-
}
1446-
if !didRange {
1447-
p.controlClause(true, s.Init, s.Cond, s.Post)
14481459
}
14491460
p.block(s.Body, 1)
14501461

@@ -1669,7 +1680,9 @@ func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
16691680

16701681
func (p *printer) genDecl(d *ast.GenDecl) {
16711682
p.setComment(d.Doc)
1672-
// p.print(d.Pos(), d.Tok, blank)
1683+
if d.Tok == token.IMPORT {
1684+
p.print(d.Pos(), d.Tok, blank)
1685+
}
16731686

16741687
if d.Lparen.IsValid() || len(d.Specs) > 1 {
16751688
// group of parenthesized declarations

pyprint/printer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,10 +1013,13 @@ func (p *printer) print(args ...interface{}) {
10131013
p.linePtr = nil
10141014
}
10151015

1016-
if data == "true" {
1016+
switch data {
1017+
case "true":
10171018
data = "True"
1018-
} else if data == "false" {
1019+
case "false":
10191020
data = "False"
1021+
case "nil":
1022+
data = "go.nil" // todo: gopy mode
10201023
}
10211024

10221025
p.writeString(next, data, isLit)

testdata/ra25.input

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,9 @@ func (ss *Sim) NewRndSeed() {
337337
// and add a few tabs at the end to allow for expansion..
338338
func (ss *Sim) Counters(train bool) string {
339339
if train {
340-
return fmt.Sprintf("Run:\t%d\tEpoch:\t%d\tTrial:\t%d\tCycle:\t%d\tName:\t%v\t\t\t", ss.TrainEnv.Run.Cur, ss.TrainEnv.Epoch.Cur, ss.TrainEnv.Trial.Cur, ss.Time.Cycle, ss.TrainEnv.TrialName)
340+
return fmt.Sprintf("Run:\t%d\tEpoch:\t%d\tTrial:\t%d\tCycle:\t%d\tName:\t%s\t\t\t", ss.TrainEnv.Run.Cur, ss.TrainEnv.Epoch.Cur, ss.TrainEnv.Trial.Cur, ss.Time.Cycle, ss.TrainEnv.TrialName.Cur)
341341
} else {
342-
return fmt.Sprintf("Run:\t%d\tEpoch:\t%d\tTrial:\t%d\tCycle:\t%d\tName:\t%v\t\t\t", ss.TrainEnv.Run.Cur, ss.TrainEnv.Epoch.Cur, ss.TestEnv.Trial.Cur, ss.Time.Cycle, ss.TestEnv.TrialName)
342+
return fmt.Sprintf("Run:\t%d\tEpoch:\t%d\tTrial:\t%d\tCycle:\t%d\tName:\t%s\t\t\t", ss.TrainEnv.Run.Cur, ss.TrainEnv.Epoch.Cur, ss.TestEnv.Trial.Cur, ss.Time.Cycle, ss.TestEnv.TrialName.Cur)
343343
}
344344
}
345345

@@ -481,7 +481,7 @@ func (ss *Sim) RunEnd() {
481481
ss.LogRun(ss.RunLog)
482482
if ss.SaveWts {
483483
fnm := ss.WeightsFileName()
484-
fmt.Printf("Saving Weights to: %v\n", fnm)
484+
fmt.Printf("Saving Weights to: %s\n", fnm)
485485
ss.Net.SaveWtsJSON(gi.FileName(fnm))
486486
}
487487
}
@@ -1195,8 +1195,6 @@ func (ss *Sim) ConfigGui() *gi.Window {
11951195
width := 1600
11961196
height := 1200
11971197

1198-
// gi.WinEventTrace = true
1199-
12001198
gi.SetAppName("ra25")
12011199
gi.SetAppAbout(`This demonstrates a basic Leabra model. See <a href="https://github.com/emer/emergent">emergent on GitHub</a>.</p>`)
12021200

@@ -1333,7 +1331,7 @@ func (ss *Sim) ConfigGui() *gi.Window {
13331331
} else {
13341332
if !ss.IsRunning {
13351333
ss.IsRunning = true
1336-
fmt.Printf("testing index: %v\n", idxs[0])
1334+
fmt.Printf("testing index: %d\n", idxs[0])
13371335
ss.TestItem(idxs[0])
13381336
ss.IsRunning = false
13391337
vp.SetNeedsFullRender()
@@ -1493,7 +1491,7 @@ func (ss *Sim) CmdArgs() {
14931491
log.Println(err)
14941492
ss.TrnEpcFile = nil
14951493
} else {
1496-
fmt.Printf("Saving epoch log to: %v\n", fnm)
1494+
fmt.Printf("Saving epoch log to: %s\n", fnm)
14971495
defer ss.TrnEpcFile.Close()
14981496
}
14991497
}
@@ -1505,7 +1503,7 @@ func (ss *Sim) CmdArgs() {
15051503
log.Println(err)
15061504
ss.RunFile = nil
15071505
} else {
1508-
fmt.Printf("Saving run log to: %v\n", fnm)
1506+
fmt.Printf("Saving run log to: %s\n", fnm)
15091507
defer ss.RunFile.Close()
15101508
}
15111509
}

0 commit comments

Comments
 (0)