1
0
mirror of https://github.com/golang/go synced 2024-11-12 10:00:25 -07:00

Fix declared and not used errors and unused import errors in

the interpreter and update code to use ast.BasicDecl and
multi-type switch.  There are still a lot of "switch _ :=
x.(type)" that should make use of the new type switch syntax,
but those will be a different CL.

R=rsc
APPROVED=rsc
DELTA=58  (16 added, 23 deleted, 19 changed)
OCL=34853
CL=34963
This commit is contained in:
Austin Clements 2009-09-24 08:25:14 -07:00
parent 73bac0416b
commit da4a22993e
8 changed files with 35 additions and 42 deletions

View File

@ -6,7 +6,6 @@ package eval
import ( import (
"fmt"; "fmt";
"go/ast";
"go/scanner"; "go/scanner";
"go/token"; "go/token";
) )

View File

@ -7,10 +7,8 @@ package eval
import ( import (
"bignum"; "bignum";
"go/ast"; "go/ast";
"go/scanner";
"go/token"; "go/token";
"log"; "log";
"os";
"strconv"; "strconv";
"strings"; "strings";
) )
@ -191,7 +189,7 @@ func (a *expr) convertToInt(max int64, negErr string, errOp string) *expr {
// expression. // expression.
func (a *expr) derefArray() *expr { func (a *expr) derefArray() *expr {
if pt, ok := a.t.lit().(*PtrType); ok { if pt, ok := a.t.lit().(*PtrType); ok {
if at, ok := pt.Elem.lit().(*ArrayType); ok { if _, ok := pt.Elem.lit().(*ArrayType); ok {
deref := a.compileStarExpr(a); deref := a.compileStarExpr(a);
if deref == nil { if deref == nil {
log.Crashf("failed to dereference *array"); log.Crashf("failed to dereference *array");
@ -481,15 +479,23 @@ func (a *exprCompiler) compile(x ast.Expr, callCtx bool) *expr {
switch x := x.(type) { switch x := x.(type) {
// Literals // Literals
case *ast.CharLit: case *ast.BasicLit:
switch x.Kind {
case token.INT:
return ei.compileIntLit(string(x.Value));
case token.FLOAT:
return ei.compileFloatLit(string(x.Value));
case token.CHAR:
return ei.compileCharLit(string(x.Value)); return ei.compileCharLit(string(x.Value));
case token.STRING:
return ei.compileStringLit(string(x.Value));
default:
log.Crashf("unexpected basic literal type %v", x.Kind);
}
case *ast.CompositeLit: case *ast.CompositeLit:
goto notimpl; goto notimpl;
case *ast.FloatLit:
return ei.compileFloatLit(string(x.Value));
case *ast.FuncLit: case *ast.FuncLit:
decl := ei.compileFuncType(a.block, x.Type); decl := ei.compileFuncType(a.block, x.Type);
if decl == nil { if decl == nil {
@ -507,12 +513,6 @@ func (a *exprCompiler) compile(x ast.Expr, callCtx bool) *expr {
} }
return ei.compileFuncLit(decl, fn); return ei.compileFuncLit(decl, fn);
case *ast.IntLit:
return ei.compileIntLit(string(x.Value));
case *ast.StringLit:
return ei.compileStringLit(string(x.Value));
// Types // Types
case *ast.ArrayType: case *ast.ArrayType:
// TODO(austin) Use a multi-type case // TODO(austin) Use a multi-type case
@ -744,7 +744,7 @@ func (a *exprInfo) compileIdealInt(i *bignum.Integer, desc string) *expr {
} }
func (a *exprInfo) compileIntLit(lit string) *expr { func (a *exprInfo) compileIntLit(lit string) *expr {
i, _, _2 := bignum.IntFromString(lit, 0); i, _, _ := bignum.IntFromString(lit, 0);
return a.compileIdealInt(i, "integer literal"); return a.compileIdealInt(i, "integer literal");
} }
@ -754,7 +754,7 @@ func (a *exprInfo) compileCharLit(lit string) *expr {
a.silentErrors++; a.silentErrors++;
return nil; return nil;
} }
v, mb, tail, err := strconv.UnquoteChar(lit[1:len(lit)], '\''); v, _, tail, err := strconv.UnquoteChar(lit[1:len(lit)], '\'');
if err != nil || tail != "'" { if err != nil || tail != "'" {
// Caught by parser // Caught by parser
a.silentErrors++; a.silentErrors++;
@ -863,7 +863,7 @@ func (a *exprInfo) compileSelectorExpr(v *expr, name string) *expr {
// If it's a named type, look for methods // If it's a named type, look for methods
if ti, ok := t.(*NamedType); ok { if ti, ok := t.(*NamedType); ok {
method, ok := ti.methods[name]; _, ok := ti.methods[name];
if ok { if ok {
mark(depth, pathName + "." + name); mark(depth, pathName + "." + name);
log.Crash("Methods not implemented"); log.Crash("Methods not implemented");
@ -1638,12 +1638,8 @@ func (a *exprInfo) compileBinaryExpr(op token.Token, l, r *expr) *expr {
return nil; return nil;
} }
// Arrays and structs may not be compared to anything. // Arrays and structs may not be compared to anything.
// TODO(austin) Use a multi-type switch switch l.t.(type) {
if _, ok := l.t.(*ArrayType); ok { case *ArrayType, *StructType:
a.diagOpTypes(op, origlt, origrt);
return nil;
}
if _, ok := l.t.(*StructType); ok {
a.diagOpTypes(op, origlt, origrt); a.diagOpTypes(op, origlt, origrt);
return nil; return nil;
} }

View File

@ -5,7 +5,6 @@
package eval package eval
import ( import (
"fmt";
"go/token"; "go/token";
"log"; "log";
) )

View File

@ -7,11 +7,8 @@ package eval
import ( import (
"bignum"; "bignum";
"log"; "log";
"os";
"go/ast"; "go/ast";
"go/scanner";
"go/token"; "go/token";
"strconv";
) )
const ( const (
@ -1018,7 +1015,7 @@ func (a *stmtCompiler) compileSwitchStmt(s *ast.SwitchStmt) {
// Count cases // Count cases
ncases := 0; ncases := 0;
hasDefault := false; hasDefault := false;
for i, c := range s.Body.List { for _, c := range s.Body.List {
clause, ok := c.(*ast.CaseClause); clause, ok := c.(*ast.CaseClause);
if !ok { if !ok {
a.diagAt(clause, "switch statement must contain case clauses"); a.diagAt(clause, "switch statement must contain case clauses");
@ -1090,7 +1087,7 @@ func (a *stmtCompiler) compileSwitchStmt(s *ast.SwitchStmt) {
// Save jump PC's // Save jump PC's
pc := a.nextPC(); pc := a.nextPC();
if clause.Values != nil { if clause.Values != nil {
for _, v := range clause.Values { for _ = range clause.Values {
casePCs[i] = &pc; casePCs[i] = &pc;
i++; i++;
} }
@ -1215,7 +1212,7 @@ func (a *blockCompiler) compileStmt(s ast.Stmt) {
} }
func (a *blockCompiler) compileStmts(block *ast.BlockStmt) { func (a *blockCompiler) compileStmts(block *ast.BlockStmt) {
for i, sub := range block.List { for _, sub := range block.List {
a.compileStmt(sub); a.compileStmt(sub);
} }
} }

View File

@ -165,7 +165,7 @@ type boolType struct {
var BoolType = universe.DefineType("bool", universePos, &boolType{}) var BoolType = universe.DefineType("bool", universePos, &boolType{})
func (t *boolType) compat(o Type, conv bool) bool { func (t *boolType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*boolType); _, ok := o.lit().(*boolType);
return ok; return ok;
} }
@ -364,7 +364,7 @@ type idealIntType struct {
var IdealIntType Type = &idealIntType{} var IdealIntType Type = &idealIntType{}
func (t *idealIntType) compat(o Type, conv bool) bool { func (t *idealIntType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*idealIntType); _, ok := o.lit().(*idealIntType);
return ok; return ok;
} }
@ -485,7 +485,7 @@ type idealFloatType struct {
var IdealFloatType Type = &idealFloatType{}; var IdealFloatType Type = &idealFloatType{};
func (t *idealFloatType) compat(o Type, conv bool) bool { func (t *idealFloatType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*idealFloatType); _, ok := o.lit().(*idealFloatType);
return ok; return ok;
} }
@ -520,7 +520,7 @@ type stringType struct {
var StringType = universe.DefineType("string", universePos, &stringType{}) var StringType = universe.DefineType("string", universePos, &stringType{})
func (t *stringType) compat(o Type, conv bool) bool { func (t *stringType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*stringType); _, ok := o.lit().(*stringType);
return ok; return ok;
} }

View File

@ -26,7 +26,7 @@ type typeCompiler struct {
} }
func (a *typeCompiler) compileIdent(x *ast.Ident, allowRec bool) Type { func (a *typeCompiler) compileIdent(x *ast.Ident, allowRec bool) Type {
_bl, _index, def := a.block.Lookup(x.Value); _, _, def := a.block.Lookup(x.Value);
if def == nil { if def == nil {
a.diagAt(x, "%s: undefined", x.Value); a.diagAt(x, "%s: undefined", x.Value);
return nil; return nil;
@ -106,7 +106,7 @@ func (a *typeCompiler) compileFields(fs []*ast.Field, allowRec bool) ([]Type, []
bad := false; bad := false;
i := 0; i := 0;
for fi, f := range fs { for _, f := range fs {
t := a.compileType(f.Type, allowRec); t := a.compileType(f.Type, allowRec);
if t == nil { if t == nil {
bad = true; bad = true;

View File

@ -6,8 +6,6 @@ package eval
import ( import (
"bignum"; "bignum";
"fmt";
"go/token";
) )
// TODO(austin): Maybe add to bignum in more general form // TODO(austin): Maybe add to bignum in more general form

View File

@ -5,7 +5,6 @@
package eval package eval
import ( import (
"fmt";
"go/ast"; "go/ast";
"go/parser"; "go/parser";
"go/scanner"; "go/scanner";
@ -64,7 +63,7 @@ func (w *World) compileStmts(stmts []ast.Stmt) (Code, os.Error) {
block: w.scope.block, block: w.scope.block,
}; };
nerr := cc.numError(); nerr := cc.numError();
for i, stmt := range stmts { for _, stmt := range stmts {
bc.compileStmt(stmt); bc.compileStmt(stmt);
} }
fc.checkLabels(); fc.checkLabels();
@ -107,11 +106,16 @@ func (w *World) compileExpr(e ast.Expr) (Code, os.Error) {
return nil, errors.GetError(scanner.Sorted); return nil, errors.GetError(scanner.Sorted);
} }
var eval func(Value, *Thread); var eval func(Value, *Thread);
switch _ := ec.t.(type) { switch t := ec.t.(type) {
case *idealIntType: case *idealIntType:
// nothing // nothing
case *idealFloatType: case *idealFloatType:
// nothing // nothing
case *MultiType:
if len(t.Elems) == 0 {
return &stmtCode{w, code{ec.exec}}, nil;
}
fallthrough;
default: default:
eval = genAssign(ec.t, ec); eval = genAssign(ec.t, ec);
} }