示例#1
0
文件: compose.go 项目: katydid/v0.1
func prep(expr *ast.Expr, expType types.Type) (uniq string, err error) {
	if expr.Function != nil {
		fnc := expr.GetFunction()
		uniq, err = WhichFunc(fnc)
		if err != nil {
			return "", err
		}
		typ, err := funcs.Out(uniq)
		if err != nil {
			return "", err
		}
		if typ != expType {
			return "", &errExpected{expType.String(), expr.String()}
		}
		return uniq, err
	}
	if expr.List != nil {
		if !types.IsList(expType) {
			return "", &errExpected{expType.String(), expr.String()}
		}
	}
	typ, err := Which(expr)
	if err != nil {
		return "", err
	}
	if typ != expType {
		return "", &errExpected{expType.String(), expr.String()}
	}
	return "", nil
}
示例#2
0
func composeFloat32s(expr *ast.Expr) (funcs.Float32s, error) {
	uniq, err := prep(expr, types.LIST_FLOAT)
	if err != nil {
		return nil, err
	}
	if expr.List != nil {
		vs, err := newValues(expr.GetList().GetElems())
		if err != nil {
			return nil, err
		}
		bs := make([]funcs.Float32, len(vs))
		var ok bool
		for i := range vs {
			bs[i], ok = vs[i].(funcs.Float32)
			if !ok {
				return nil, &errExpected{types.SINGLE_FLOAT.String(), expr.String()}
			}
		}
		return funcs.NewListOfFloat32(bs), nil
	}
	values, err := newValues(expr.GetFunction().GetParams())
	if err != nil {
		return nil, err
	}
	return funcs.NewFloat32sFunc(uniq, values...)
}
示例#3
0
文件: compose.go 项目: katydid/v0.1
func Which(expr *ast.Expr) (types.Type, error) {
	if expr.Terminal != nil {
		term := expr.GetTerminal()
		if term.DoubleValue != nil {
			return types.SINGLE_DOUBLE, nil
		}
		if term.FloatValue != nil {
			return types.SINGLE_FLOAT, nil
		}
		if term.Int64Value != nil {
			return types.SINGLE_INT64, nil
		}
		if term.Uint64Value != nil {
			return types.SINGLE_UINT64, nil
		}
		if term.Int32Value != nil {
			return types.SINGLE_INT32, nil
		}
		if term.BoolValue != nil {
			return types.SINGLE_BOOL, nil
		}
		if term.StringValue != nil {
			return types.SINGLE_STRING, nil
		}
		if term.BytesValue != nil {
			return types.SINGLE_BYTES, nil
		}
		if term.Uint32Value != nil {
			return types.SINGLE_UINT32, nil
		}
		if term.Variable != nil {
			return term.Variable.Type, nil
		}
	}
	if expr.List != nil {
		return expr.GetList().GetType(), nil
	}
	if expr.Function != nil {
		fnc := expr.GetFunction()
		uniq, err := WhichFunc(fnc)
		if err != nil {
			return 0, err
		}
		return funcs.Out(uniq)
	}
	return 0, &errUnknownType{expr}
}
示例#4
0
func composeFloat32(expr *ast.Expr) (funcs.Float32, error) {
	uniq, err := prep(expr, types.SINGLE_FLOAT)
	if err != nil {
		return nil, err
	}
	if expr.Terminal != nil {
		if expr.GetTerminal().Variable != nil {
			return funcs.NewFloat32Variable(), nil
		} else {
			return funcs.NewConstFloat32(expr.GetTerminal().GetFloatValue()), nil
		}
	}
	values, err := newValues(expr.GetFunction().GetParams())
	if err != nil {
		return nil, err
	}
	return funcs.NewFloat32Func(uniq, values...)
}
示例#5
0
func composeBytes(expr *ast.Expr) (funcs.Bytes, error) {
	uniq, err := prep(expr, types.SINGLE_BYTES)
	if err != nil {
		return nil, err
	}
	if expr.Terminal != nil {
		if expr.GetTerminal().Variable != nil {
			return funcs.NewBytesVariable(), nil
		} else {
			return funcs.NewConstBytes(expr.GetTerminal().GetBytesValue()), nil
		}
	}
	values, err := newValues(expr.GetFunction().GetParams())
	if err != nil {
		return nil, err
	}
	return funcs.NewBytesFunc(uniq, values...)
}
示例#6
0
func composeString(expr *ast.Expr) (funcs.String, error) {
	uniq, err := prep(expr, types.SINGLE_STRING)
	if err != nil {
		return nil, err
	}
	if expr.Terminal != nil {
		if expr.GetTerminal().Variable != nil {
			return funcs.NewStringVariable(), nil
		} else {
			return funcs.NewConstString(expr.GetTerminal().GetStringValue()), nil
		}
	}
	values, err := newValues(expr.GetFunction().GetParams())
	if err != nil {
		return nil, err
	}
	return funcs.NewStringFunc(uniq, values...)
}
示例#7
0
func composeUint64(expr *ast.Expr) (funcs.Uint64, error) {
	uniq, err := prep(expr, types.SINGLE_UINT64)
	if err != nil {
		return nil, err
	}
	if expr.Terminal != nil {
		if expr.GetTerminal().Variable != nil {
			return funcs.NewUint64Variable(), nil
		} else {
			return funcs.NewConstUint64(expr.GetTerminal().GetUint64Value()), nil
		}
	}
	values, err := newValues(expr.GetFunction().GetParams())
	if err != nil {
		return nil, err
	}
	return funcs.NewUint64Func(uniq, values...)
}