Skip to content

Commit

Permalink
Golint fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Jul 15, 2020
1 parent cc8b59a commit a3c2ba2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
76 changes: 67 additions & 9 deletions compiler/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ func indent(w io.Writer, indent int) {
}
}

// Type specifies AST types.
type Type int

// AST types.
const (
TypeName Type = iota
TypeArray
Expand All @@ -52,6 +54,7 @@ const (
TypeAlias
)

// TypeInfo contains AST type information.
type TypeInfo struct {
Type Type
Name Identifier
Expand All @@ -62,6 +65,7 @@ type TypeInfo struct {
AliasType *TypeInfo
}

// StructField contains AST structure field information.
type StructField struct {
Name string
Type *TypeInfo
Expand All @@ -70,6 +74,7 @@ type StructField struct {
var reSizedType = regexp.MustCompilePOSIX(
`^(uint|int|float|string)([[:digit:]]*)$`)

// Resolve resolves the type information in the environment.
func (ti *TypeInfo) Resolve(env *Env, ctx *Codegen, gen *ssa.Generator) (
types.Info, error) {

Expand Down Expand Up @@ -106,17 +111,16 @@ func (ti *TypeInfo) Resolve(env *Env, ctx *Codegen, gen *ssa.Generator) (
Type: types.Bool,
Bits: 1,
}, nil
} else {
// Check dynamic types from the env.
b, ok := env.Get(ti.Name.Name)
if ok {
val, ok := b.Bound.(*ssa.Variable)
if ok && val.TypeRef {
return val.Type, nil
}
}
// Check dynamic types from the env.
b, ok := env.Get(ti.Name.Name)
if ok {
val, ok := b.Bound.(*ssa.Variable)
if ok && val.TypeRef {
return val.Type, nil
}
return result, fmt.Errorf("unknown type %s", ti)
}
return result, fmt.Errorf("unknown type %s", ti)

default:
return result, fmt.Errorf("unsupported type %s", ti)
Expand Down Expand Up @@ -152,6 +156,7 @@ func (ti *TypeInfo) String() string {
}
}

// Identifier implements an AST identifier.
type Identifier struct {
Package string
Name string
Expand All @@ -164,56 +169,69 @@ func (i Identifier) String() string {
return fmt.Sprintf("%s.%s", i.Package, i.Name)
}

// AST implements abstract syntax tree nodes.
type AST interface {
String() string
// Location returns the location information of the node.
Location() utils.Point
// SSA generates SSA code from the AST node. The code is appended
// into the basic block `block'. The function returns the next
// sequential basic. The `ssa.Dead' is set to `true' if the code
// terminates i.e. all following AST nodes are dead code.
SSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator) (
*ssa.Block, []ssa.Variable, error)
// Eval evaluates the AST node during constant propagation.
Eval(env *Env, ctx *Codegen, gen *ssa.Generator) (
interface{}, bool, error)
}

// NewEnv creates a new environment based on the current environment
// bindings in the block.
func NewEnv(block *ssa.Block) *Env {
return &Env{
Bindings: block.Bindings.Clone(),
}
}

// Env implements a variable bindings environment.
type Env struct {
Bindings ssa.Bindings
}

// Get gets the variable binding from the environment.
func (env *Env) Get(name string) (ssa.Binding, bool) {
return env.Bindings.Get(name)
}

// Set sets the variable binding to the environment.
func (env *Env) Set(v ssa.Variable, val *ssa.Variable) {
env.Bindings.Set(v, val)
}

// List implements an AST list statement.
type List []AST

func (ast List) String() string {
return fmt.Sprintf("%v", []AST(ast))
}

// Location implements the compiler.ast.AST.Location for list
// statements.
func (ast List) Location() utils.Point {
if len(ast) > 0 {
return ast[0].Location()
}
return utils.Point{}
}

// Variable implements an AST variable.
type Variable struct {
Loc utils.Point
Name string
Type *TypeInfo
}

// Func implements an AST function.
type Func struct {
Loc utils.Point
Name string
Expand All @@ -224,8 +242,10 @@ type Func struct {
Annotations Annotations
}

// Annotations specify function annotations.
type Annotations []string

// NewFunc creates a new function definition.
func NewFunc(loc utils.Point, name string, args []*Variable, ret []*Variable,
body List, annotations Annotations) *Func {
return &Func{
Expand All @@ -242,10 +262,13 @@ func (ast *Func) String() string {
return fmt.Sprintf("func %s()", ast.Name)
}

// Location implements the compiler.ast.AST.Location for function
// definitions.
func (ast *Func) Location() utils.Point {
return ast.Loc
}

// ConstantDef implements an AST constant definition.
type ConstantDef struct {
Loc utils.Point
Name string
Expand All @@ -264,10 +287,13 @@ func (ast *ConstantDef) String() string {
return result
}

// Location implements the compiler.ast.AST.Location for constant
// definitions.
func (ast *ConstantDef) Location() utils.Point {
return ast.Loc
}

// VariableDef implements an AST variable definition.
type VariableDef struct {
Loc utils.Point
Names []string
Expand All @@ -283,10 +309,13 @@ func (ast *VariableDef) String() string {
return result
}

// Location implements the compiler.ast.AST.Location for variable
// definitions.
func (ast *VariableDef) Location() utils.Point {
return ast.Loc
}

// Assign implements an AST assignment expression.
type Assign struct {
Loc utils.Point
LValues []AST
Expand All @@ -304,10 +333,13 @@ func (ast *Assign) String() string {
return fmt.Sprintf("%v %s %v", ast.LValues, op, ast.Exprs)
}

// Location implements the compiler.ast.AST.Location for assignment
// expressions.
func (ast *Assign) Location() utils.Point {
return ast.Loc
}

// If implements an AST if statement.
type If struct {
Loc utils.Point
Expr AST
Expand All @@ -319,10 +351,13 @@ func (ast *If) String() string {
return fmt.Sprintf("if %s", ast.Expr)
}

// Location implements the compiler.ast.AST.Location for if
// statements.
func (ast *If) Location() utils.Point {
return ast.Loc
}

// Call implements an AST call expression.
type Call struct {
Loc utils.Point
Name Identifier
Expand All @@ -333,10 +368,13 @@ func (ast *Call) String() string {
return fmt.Sprintf("%s()", ast.Name)
}

// Location implements the compiler.ast.AST.Location for call
// expressions.
func (ast *Call) Location() utils.Point {
return ast.Loc
}

// Return implements an AST return statement.
type Return struct {
Loc utils.Point
Exprs []AST
Expand All @@ -346,10 +384,13 @@ func (ast *Return) String() string {
return fmt.Sprintf("return %v", ast.Exprs)
}

// Location implements the compiler.ast.AST.Location for return
// statements.
func (ast *Return) Location() utils.Point {
return ast.Loc
}

// For implements an AST for statement.
type For struct {
Loc utils.Point
Init AST
Expand All @@ -363,12 +404,16 @@ func (ast *For) String() string {
ast.Init, ast.Cond, ast.Inc, ast.Body)
}

// Location implements the compiler.ast.AST.Location for for
// statements.
func (ast *For) Location() utils.Point {
return ast.Loc
}

// BinaryType defines binary expression types.
type BinaryType int

// Binary expression types.
const (
BinaryMult BinaryType = iota
BinaryDiv
Expand Down Expand Up @@ -421,6 +466,7 @@ func (t BinaryType) String() string {
return fmt.Sprintf("{BinaryType %d}", t)
}

// Binary implements an AST binary expression.
type Binary struct {
Loc utils.Point
Left AST
Expand All @@ -432,10 +478,13 @@ func (ast *Binary) String() string {
return fmt.Sprintf("%s %s %s", ast.Left, ast.Op, ast.Right)
}

// Location implements the compiler.ast.AST.Location for binary
// expressions.
func (ast *Binary) Location() utils.Point {
return ast.Loc
}

// Slice implements an AST slice expression.
type Slice struct {
Loc utils.Point
Expr AST
Expand All @@ -454,10 +503,13 @@ func (ast *Slice) String() string {
return fmt.Sprintf("%s[%s:%s]", ast.Expr, fromStr, toStr)
}

// Location implements the compiler.ast.AST.Location for slice
// expressions.
func (ast *Slice) Location() utils.Point {
return ast.Loc
}

// VariableRef implements an AST variable reference.
type VariableRef struct {
Loc utils.Point
Name Identifier
Expand All @@ -467,10 +519,13 @@ func (ast *VariableRef) String() string {
return ast.Name.String()
}

// Location implements the compiler.ast.AST.Location for variable
// references.
func (ast *VariableRef) Location() utils.Point {
return ast.Loc
}

// Constant implements an AST constant value.
type Constant struct {
Loc utils.Point
Value interface{}
Expand All @@ -480,6 +535,7 @@ func (ast *Constant) String() string {
return ConstantName(ast.Value)
}

// ConstantName returns the name of the constant value.
func ConstantName(value interface{}) string {
switch val := value.(type) {
case int, int32, uint64:
Expand All @@ -493,6 +549,8 @@ func ConstantName(value interface{}) string {
}
}

// Location implements the compiler.ast.AST.Location for constant
// values.
func (ast *Constant) Location() utils.Point {
return ast.Loc
}
2 changes: 1 addition & 1 deletion compiler/ast/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func bigInt(i interface{}, ctx *Codegen, loc utils.Point) (*big.Int, error) {
}
}

// Eval implements the compiler.ast.AST.Eval for slice values.
// Eval implements the compiler.ast.AST.Eval for slice expressions.
func (ast *Slice) Eval(env *Env, ctx *Codegen, gen *ssa.Generator) (
interface{}, bool, error) {

Expand Down
2 changes: 1 addition & 1 deletion compiler/ast/ssagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ func (ast *Binary) SSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator) (
return block, []ssa.Variable{t}, nil
}

// SSA implements the compiler.ast.AST.SSA for slices.
// SSA implements the compiler.ast.AST.SSA for slice expressions.
func (ast *Slice) SSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator) (
*ssa.Block, []ssa.Variable, error) {

Expand Down

0 comments on commit a3c2ba2

Please sign in to comment.