go readability fixes
- don't use _.
- use raw string literal, %q.
- error strings should not be capitalized.
diff --git a/ast.go b/ast.go
index 07b01da..aadb89f 100644
--- a/ast.go
+++ b/ast.go
@@ -1,12 +1,14 @@
package main
+type ASTType int
+
const (
- AST_ASSIGN = iota
- AST_RULE
+ ASTAssign ASTType = iota
+ ASTRule
)
type AST interface {
- typ() int
+ typ() ASTType
show()
}
@@ -20,8 +22,8 @@
rhs string
}
-func (ast *AssignAST) typ() int {
- return AST_ASSIGN
+func (ast *AssignAST) typ() ASTType {
+ return ASTAssign
}
func (ast *AssignAST) show() {
@@ -35,8 +37,8 @@
cmds []string
}
-func (ast *RuleAST) typ() int {
- return AST_RULE
+func (ast *RuleAST) typ() ASTType {
+ return ASTRule
}
func (ast *RuleAST) show() {
diff --git a/eval.go b/eval.go
index a676171..dd920fc 100644
--- a/eval.go
+++ b/eval.go
@@ -21,18 +21,18 @@
}
type Evaluator struct {
- out_vars map[string]string
- out_rules []*Rule
- refs map[string]bool
- vars map[string]string
- cur_rule *Rule
+ outVars map[string]string
+ outRules []*Rule
+ refs map[string]bool
+ vars map[string]string
+ curRule *Rule
}
func newEvaluator() *Evaluator {
return &Evaluator{
- out_vars: make(map[string]string),
- refs: make(map[string]bool),
- vars: make(map[string]string),
+ outVars: make(map[string]string),
+ refs: make(map[string]bool),
+ vars: make(map[string]string),
}
}
@@ -57,7 +57,7 @@
if err != nil {
panic(err)
}
- re, err := regexp.Compile("\\s")
+ re, err := regexp.Compile(`\s`)
if err != nil {
panic(err)
}
@@ -81,7 +81,7 @@
var varname string
switch ex[i] {
case '@':
- buf.WriteString(ev.cur_rule.output)
+ buf.WriteString(ev.curRule.output)
i++
continue
case '(':
@@ -101,7 +101,7 @@
value, present := ev.vars[varname]
if !present {
ev.refs[varname] = true
- value = ev.out_vars[varname]
+ value = ev.outVars[varname]
}
buf.WriteString(value)
@@ -124,32 +124,32 @@
lhs := ev.evalExpr(ast.lhs)
rhs := ev.evalExpr(ast.rhs)
Log("ASSIGN: %s=%s", lhs, rhs)
- ev.out_vars[lhs] = rhs
+ ev.outVars[lhs] = rhs
}
func (ev *Evaluator) evalRule(ast *RuleAST) {
- ev.cur_rule = &Rule{}
+ ev.curRule = &Rule{}
lhs := ev.evalExpr(ast.lhs)
- ev.cur_rule.output = lhs
+ ev.curRule.output = lhs
rhs := ev.evalExpr(ast.rhs)
if rhs != "" {
- ev.cur_rule.inputs = strings.Split(rhs, " ")
+ ev.curRule.inputs = strings.Split(rhs, " ")
}
var cmds []string
for _, cmd := range ast.cmds {
cmds = append(cmds, ev.evalExpr(cmd))
}
Log("RULE: %s=%s", lhs, rhs)
- ev.cur_rule.cmds = cmds
- ev.out_rules = append(ev.out_rules, ev.cur_rule)
- ev.cur_rule = nil
+ ev.curRule.cmds = cmds
+ ev.outRules = append(ev.outRules, ev.curRule)
+ ev.curRule = nil
}
func (ev *Evaluator) eval(ast AST) {
switch ast.typ() {
- case AST_ASSIGN:
+ case ASTAssign:
ev.evalAssign(ast.(*AssignAST))
- case AST_RULE:
+ case ASTRule:
ev.evalRule(ast.(*RuleAST))
}
}
@@ -160,8 +160,8 @@
ev.eval(stmt)
}
return &EvalResult{
- vars: ev.out_vars,
- rules: ev.out_rules,
+ vars: ev.outVars,
+ rules: ev.outRules,
refs: ev.refs,
}
}
diff --git a/exec.go b/exec.go
index 11b3653..158eefb 100644
--- a/exec.go
+++ b/exec.go
@@ -17,6 +17,7 @@
}
}
+// TODO(ukai): use time.Time?
func getTimestamp(filename string) int64 {
st, err := os.Stat(filename)
if err != nil {
@@ -52,14 +53,14 @@
func (ex *Executor) build(output string) int64 {
Log("Building: %s", output)
- output_ts := getTimestamp(output)
+ outputTs := getTimestamp(output)
rule, present := ex.rules[output]
if !present {
- if output_ts >= 0 {
- return output_ts
+ if outputTs >= 0 {
+ return outputTs
}
- Error("No rule to make target '%s'", output)
+ Error("No rule to make target %q", output)
}
latest := int64(-1)
@@ -70,17 +71,17 @@
}
}
- if output_ts >= latest {
- return output_ts
+ if outputTs >= latest {
+ return outputTs
}
ex.runCommands(rule.cmds)
- output_ts = getTimestamp(output)
- if output_ts < 0 {
- output_ts = time.Now().Unix()
+ outputTs = getTimestamp(output)
+ if outputTs < 0 {
+ outputTs = time.Now().Unix()
}
- return output_ts
+ return outputTs
}
func (ex *Executor) exec(er *EvalResult) {
diff --git a/parser.go b/parser.go
index b4ecaab..8c4e455 100644
--- a/parser.go
+++ b/parser.go
@@ -14,13 +14,13 @@
}
type parser struct {
- rd *bufio.Reader
- mk Makefile
- lineno int
- elineno int // lineno == elineno unless there is trailing '\'.
- un_buf []byte
- has_un_buf bool
- done bool
+ rd *bufio.Reader
+ mk Makefile
+ lineno int
+ elineno int // lineno == elineno unless there is trailing '\'.
+ unBuf []byte
+ hasUnBuf bool
+ done bool
}
func exists(filename string) bool {
@@ -47,9 +47,9 @@
}
func (p *parser) readLine() []byte {
- if p.has_un_buf {
- p.has_un_buf = false
- return p.un_buf
+ if p.hasUnBuf {
+ p.hasUnBuf = false
+ return p.unBuf
}
p.lineno = p.elineno
@@ -82,11 +82,11 @@
}
func (p *parser) unreadLine(line []byte) {
- if p.has_un_buf {
+ if p.hasUnBuf {
panic("unreadLine twice!")
}
- p.un_buf = line
- p.has_un_buf = true
+ p.unBuf = line
+ p.hasUnBuf = true
}
func (p *parser) readByte() (byte, error) {
@@ -283,6 +283,7 @@
if err != nil {
return Makefile{}, err
}
+ defer f.Close()
parser := newParser(f)
return parser.parse()
}
@@ -294,5 +295,5 @@
return ParseMakefile(filename)
}
}
- return Makefile{}, errors.New("No targets specified and no makefile found.")
+ return Makefile{}, errors.New("no targets specified and no makefile found.")
}